Server list
This commit is contained in:
@@ -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}"))
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user