New profile
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Account {
|
||||
pub display_name: String,
|
||||
pub private_key: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AccountsFile {
|
||||
pub active_account: Option<usize>,
|
||||
pub accounts: Vec<Account>,
|
||||
}
|
||||
|
||||
fn accounts_file_path(app: &AppHandle) -> Result<std::path::PathBuf, String> {
|
||||
let dir = app
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.map_err(|e| format!("Failed to resolve app data dir: {e}"))?;
|
||||
fs::create_dir_all(&dir).map_err(|e| format!("Failed to create app data dir: {e}"))?;
|
||||
Ok(dir.join("accounts.json"))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn save_accounts(app: AppHandle, data: AccountsFile) -> Result<(), String> {
|
||||
let path = accounts_file_path(&app)?;
|
||||
let json = serde_json::to_string_pretty(&data)
|
||||
.map_err(|e| format!("Failed to serialize accounts: {e}"))?;
|
||||
fs::write(&path, json).map_err(|e| format!("Failed to write accounts: {e}"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_accounts(app: AppHandle) -> Result<AccountsFile, String> {
|
||||
let path = accounts_file_path(&app)?;
|
||||
if !path.exists() {
|
||||
return Ok(AccountsFile {
|
||||
active_account: None,
|
||||
accounts: Vec::new(),
|
||||
});
|
||||
}
|
||||
let json = fs::read_to_string(&path).map_err(|e| format!("Failed to read accounts: {e}"))?;
|
||||
serde_json::from_str(&json).map_err(|e| format!("Failed to parse accounts: {e}"))
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod accounts;
|
||||
pub mod server_list;
|
||||
@@ -0,0 +1,52 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{collections::HashMap, fs};
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct KnownServer {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub public_key: String,
|
||||
pub is_secure: bool,
|
||||
}
|
||||
|
||||
type ServerList = HashMap<String, KnownServer>;
|
||||
|
||||
fn servers_file_path(app: &AppHandle) -> Result<std::path::PathBuf, String> {
|
||||
let dir = app
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.map_err(|e| format!("Failed to resolve app data dir: {e}"))?;
|
||||
|
||||
// ensure the directory exists before we ever try to read/write into it
|
||||
fs::create_dir_all(&dir).map_err(|e| format!("Failed to create app data dir: {e}"))?;
|
||||
|
||||
Ok(dir.join("servers.json"))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn save_server_list(app: AppHandle, servers: ServerList) -> Result<(), String> {
|
||||
let path = servers_file_path(&app)?;
|
||||
|
||||
let json = serde_json::to_string_pretty(&servers)
|
||||
.map_err(|e| format!("Failed to serialize server list: {e}"))?;
|
||||
|
||||
fs::write(&path, json).map_err(|e| format!("Failed to write server list: {e}"))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_server_list(app: AppHandle) -> Result<ServerList, String> {
|
||||
let path = servers_file_path(&app)?;
|
||||
|
||||
if !path.exists() {
|
||||
fs::write(&path, "[]").map_err(|e| format!("Failed to write first server list: {e}"))?;
|
||||
|
||||
return Ok(ServerList::new());
|
||||
}
|
||||
|
||||
let json = fs::read_to_string(&path).map_err(|e| format!("Failed to read server list: {e}"))?;
|
||||
|
||||
serde_json::from_str(&json).map_err(|e| format!("Failed to parse server list: {e}"))
|
||||
}
|
||||
Reference in New Issue
Block a user