diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs new file mode 100644 index 0000000..188770b --- /dev/null +++ b/src-tauri/src/config.rs @@ -0,0 +1,118 @@ +use serde::{Deserialize, Serialize}; +use std::fs; +use std::path::PathBuf; +use log::info; + +/// User configuration structure +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Config { + /// Whether to show the disclaimer on startup + pub show_disclaimer: bool, +} + +impl Default for Config { + fn default() -> Self { + Self { + show_disclaimer: true, + } + } +} + +/// Get the config directory path (~/.config/creamlinux) +fn get_config_dir() -> Result { + let home = std::env::var("HOME") + .map_err(|_| "Failed to get HOME directory".to_string())?; + + let config_dir = PathBuf::from(home).join(".config").join("creamlinux"); + Ok(config_dir) +} + +/// Get the config file path +fn get_config_path() -> Result { + let config_dir = get_config_dir()?; + Ok(config_dir.join("config.json")) +} + +/// Ensure the config directory exists +fn ensure_config_dir() -> Result<(), String> { + let config_dir = get_config_dir()?; + + if !config_dir.exists() { + fs::create_dir_all(&config_dir) + .map_err(|e| format!("Failed to create config directory: {}", e))?; + info!("Created config directory at {:?}", config_dir); + } + + Ok(()) +} + +/// Load configuration from disk +pub fn load_config() -> Result { + ensure_config_dir()?; + + let config_path = get_config_path()?; + + // If config file doesn't exist, create default config + if !config_path.exists() { + let default_config = Config::default(); + save_config(&default_config)?; + info!("Created default config file at {:?}", config_path); + return Ok(default_config); + } + + // Read and parse config file + let config_str = fs::read_to_string(&config_path) + .map_err(|e| format!("Failed to read config file: {}", e))?; + + let config: Config = serde_json::from_str(&config_str) + .map_err(|e| format!("Failed to parse config file: {}", e))?; + + info!("Loaded config from {:?}", config_path); + Ok(config) +} + +/// Save configuration to disk +pub fn save_config(config: &Config) -> Result<(), String> { + ensure_config_dir()?; + + let config_path = get_config_path()?; + + let config_str = serde_json::to_string_pretty(config) + .map_err(|e| format!("Failed to serialize config: {}", e))?; + + fs::write(&config_path, config_str) + .map_err(|e| format!("Failed to write config file: {}", e))?; + + info!("Saved config to {:?}", config_path); + Ok(()) +} + +/// Update a specific config value +pub fn update_config(updater: F) -> Result +where + F: FnOnce(&mut Config), +{ + let mut config = load_config()?; + updater(&mut config); + save_config(&config)?; + Ok(config) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_default_config() { + let config = Config::default(); + assert!(config.show_disclaimer); + } + + #[test] + fn test_config_serialization() { + let config = Config::default(); + let json = serde_json::to_string(&config).unwrap(); + let parsed: Config = serde_json::from_str(&json).unwrap(); + assert_eq!(config.show_disclaimer, parsed.show_disclaimer); + } +} \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 19b8128..01a990f 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -9,7 +9,9 @@ mod installer; mod searcher; mod unlockers; mod smokeapi_config; +mod config; +use crate::config::Config; use crate::unlockers::{CreamLinux, SmokeAPI, Unlocker}; use dlc_manager::DlcInfoWithState; use installer::{Game, InstallerAction, InstallerType}; @@ -46,6 +48,19 @@ pub struct AppState { fetch_cancellation: Arc, } +// Load the current configuration +#[tauri::command] +fn load_config() -> Result { + config::load_config() +} + +// Update configuration +#[tauri::command] +fn update_config(config_data: Config) -> Result { + config::save_config(&config_data)?; + Ok(config_data) +} + #[tauri::command] fn get_all_dlcs_command(game_path: String) -> Result, String> { info!("Getting all DLCs (enabled and disabled) for: {}", game_path); @@ -658,6 +673,8 @@ fn main() { write_smokeapi_config, delete_smokeapi_config, resolve_platform_conflict, + load_config, + update_config, ]) .setup(|app| { info!("Tauri application setup");