Server list

This commit is contained in:
2026-08-18 14:29:55 +02:00
parent 15a2836043
commit f115a86291
3 changed files with 73 additions and 6 deletions
+51
View File
@@ -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<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: Vec<KnownServer>) -> 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<Vec<KnownServer>, 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}"))
}