use serde::{Deserialize, Serialize}; use std::path::PathBuf; use super::theme::Theme; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Config { pub auto_hide_toolbar: bool, pub show_line_numbers: bool, pub word_wrap: bool, pub theme: Theme, pub line_side: bool, pub font_family: String, pub font_size: f32, // pub vim_mode: bool, } impl Default for Config { fn default() -> Self { Self { auto_hide_toolbar: false, show_line_numbers: false, word_wrap: true, theme: Theme::default(), line_side: false, font_family: "Proportional".to_string(), font_size: 14.0, // vim_mode: false, } } } impl Config { pub fn config_path() -> Option { let config_dir = if let Some(config_dir) = dirs::config_dir() { config_dir.join("ced") } else if let Some(home_dir) = dirs::home_dir() { home_dir.join(".config").join("ced") } else { return None; }; Some(config_dir.join("config.toml")) } pub fn load() -> Self { let config_path = match Self::config_path() { Some(path) => path, None => return Self::default(), }; if !config_path.exists() { let default_config = Self::default(); if let Err(e) = default_config.save() { eprintln!("Failed to create default config file: {}", e); } return default_config; } match std::fs::read_to_string(&config_path) { Ok(content) => match toml::from_str::(&content) { Ok(config) => config, Err(e) => { eprintln!( "Failed to parse config file {}: {}", config_path.display(), e ); Self::default() } }, Err(e) => { eprintln!( "Failed to read config file {}: {}", config_path.display(), e ); Self::default() } } } pub fn save(&self) -> Result<(), Box> { let config_path = Self::config_path().ok_or("Cannot determine config directory")?; if let Some(parent) = config_path.parent() { std::fs::create_dir_all(parent)?; } let content = toml::to_string_pretty(self)?; std::fs::write(&config_path, content)?; Ok(()) } }