33 lines
1.1 KiB
Rust
33 lines
1.1 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use tauri::{AppHandle, Manager};
|
|
|
|
use super::types::RuntimeConfig;
|
|
|
|
pub fn config_path(app: &AppHandle) -> Result<PathBuf, String> {
|
|
let data_dir = app
|
|
.path()
|
|
.app_data_dir()
|
|
.map_err(|e| format!("Cannot resolve app data dir: {e}"))?;
|
|
std::fs::create_dir_all(&data_dir).map_err(|e| format!("Cannot create app data dir: {e}"))?;
|
|
Ok(data_dir.join("config.json"))
|
|
}
|
|
|
|
pub fn load_runtime_config(app: &AppHandle) -> Result<RuntimeConfig, String> {
|
|
let path = config_path(app)?;
|
|
if !path.exists() {
|
|
return Ok(RuntimeConfig::default());
|
|
}
|
|
|
|
let json = std::fs::read_to_string(&path).map_err(|e| format!("Read error: {e}"))?;
|
|
serde_json::from_str(&json).map_err(|e| format!("Parse error: {e}"))
|
|
}
|
|
|
|
pub fn save_runtime_config(app: &AppHandle, config: &RuntimeConfig) -> Result<(), String> {
|
|
let path = config_path(app)?;
|
|
let json =
|
|
serde_json::to_string_pretty(config).map_err(|e| format!("Serialization error: {e}"))?;
|
|
std::fs::write(&path, json).map_err(|e| format!("Write error: {e}"))?;
|
|
Ok(())
|
|
}
|