diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs new file mode 100644 index 0000000..60edc31 --- /dev/null +++ b/src-tauri/src/commands.rs @@ -0,0 +1,51 @@ +use serde::{Deserialize, Serialize}; +use std::fs; +use tauri::{AppHandle, Manager}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct KnownServer { + pub name: String, + pub description: String, + pub public_key: String, + pub hostname: String, + pub is_wss: bool, +} + +fn servers_file_path(app: &AppHandle) -> Result { + 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: Vec) -> 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, 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(Vec::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}")) +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4a277ef..d2095bf 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,14 +1,13 @@ -// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/ -#[tauri::command] -fn greet(name: &str) -> String { - format!("Hello, {}! You've been greeted from Rust!", name) -} +pub mod commands; #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_opener::init()) - .invoke_handler(tauri::generate_handler![greet]) + .invoke_handler(tauri::generate_handler![ + commands::save_server_list, + commands::get_server_list + ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/src/lib/serverList.ts b/src/lib/serverList.ts new file mode 100644 index 0000000..63fc40b --- /dev/null +++ b/src/lib/serverList.ts @@ -0,0 +1,17 @@ +import { invoke } from "@tauri-apps/api/core"; + +type KnownServer = { + name: string; + description: string; + publicKey: string; + hostname: string; + isWss: boolean; +}; + +export async function saveServerList(servers: KnownServer[]): Promise { + await invoke("save_server_list", { servers }); +} + +export async function getServerList(): Promise { + return await invoke("get_server_list"); +}