|
| 1 | +//! Settings Module Foundation |
| 2 | +//! |
| 3 | +//! Centralized configuration management with extensible architecture. |
| 4 | +//! Provides clean separation of concerns and prepares for future feature expansion. |
| 5 | +
|
| 6 | +use crate::theme::{Theme, ThemeVariant}; |
| 7 | + |
| 8 | +/// Core settings structure with extensible design |
| 9 | +#[derive(Debug, Clone)] |
| 10 | +pub struct Settings { |
| 11 | + /// Current theme variant selection |
| 12 | + pub theme_variant: ThemeVariant, |
| 13 | + // Future extensions: |
| 14 | + // pub api_keys: ApiKeyConfig, |
| 15 | + // pub model_configs: ModelConfig, |
| 16 | + // pub keybinds: KeyBindConfig, |
| 17 | + // pub advanced: AdvancedConfig, |
| 18 | +} |
| 19 | + |
| 20 | +impl Settings { |
| 21 | + /// Create new settings instance with sensible defaults |
| 22 | + pub fn new() -> Self { |
| 23 | + Settings { |
| 24 | + theme_variant: ThemeVariant::EverforestDark, |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /// Apply current settings to theme instance |
| 29 | + pub fn apply_theme(&self, theme: &mut Theme) { |
| 30 | + theme.set_variant(self.theme_variant); |
| 31 | + } |
| 32 | + |
| 33 | + /// Handle settings action and update state |
| 34 | + pub fn handle_action(&mut self, action: SettingsAction) { |
| 35 | + match action { |
| 36 | + SettingsAction::ChangeTheme(variant) => { |
| 37 | + self.theme_variant = variant; |
| 38 | + } // Future actions will be handled here |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// Get current theme variant |
| 43 | + pub fn theme_variant(&self) -> ThemeVariant { |
| 44 | + self.theme_variant |
| 45 | + } |
| 46 | + |
| 47 | + /// Toggle between available theme variants |
| 48 | + pub fn toggle_theme(&mut self) { |
| 49 | + self.theme_variant = match self.theme_variant { |
| 50 | + ThemeVariant::EverforestDark => ThemeVariant::EverforestLight, |
| 51 | + ThemeVariant::EverforestLight => ThemeVariant::EverforestDark, |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + /// Validate current settings configuration |
| 56 | + pub fn validate(&self) -> Result<(), SettingsError> { |
| 57 | + // Future validation logic will go here |
| 58 | + // For now, all theme variants are valid |
| 59 | + Ok(()) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl Default for Settings { |
| 64 | + fn default() -> Self { |
| 65 | + Self::new() |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// Actions that can be performed on settings |
| 70 | +#[derive(Debug, Clone)] |
| 71 | +pub enum SettingsAction { |
| 72 | + /// Change the active theme variant |
| 73 | + ChangeTheme(ThemeVariant), |
| 74 | + // Future actions: |
| 75 | + // UpdateApiKey(String, String), |
| 76 | + // ChangeModel(ModelConfig), |
| 77 | + // UpdateKeybind(String, KeyCode), |
| 78 | + // ToggleDebugMode(bool), |
| 79 | +} |
| 80 | + |
| 81 | +/// Settings-related errors |
| 82 | +#[derive(Debug, Clone, PartialEq)] |
| 83 | +pub enum SettingsError { |
| 84 | + /// Invalid theme variant |
| 85 | + InvalidTheme(String), |
| 86 | + /// Configuration validation failed |
| 87 | + ValidationFailed(String), |
| 88 | + // Future error types: |
| 89 | + // InvalidApiKey(String), |
| 90 | + // InvalidModelConfig(String), |
| 91 | + // KeybindConflict(String), |
| 92 | +} |
| 93 | + |
| 94 | +impl std::fmt::Display for SettingsError { |
| 95 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 96 | + match self { |
| 97 | + SettingsError::InvalidTheme(theme) => { |
| 98 | + write!(f, "Invalid theme variant: {}", theme) |
| 99 | + } |
| 100 | + SettingsError::ValidationFailed(reason) => { |
| 101 | + write!(f, "Settings validation failed: {}", reason) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl std::error::Error for SettingsError {} |
| 108 | + |
| 109 | +/// Future-ready settings categories for extensibility |
| 110 | +/// These will be implemented as the application grows |
| 111 | +/// Appearance-related settings |
| 112 | +#[derive(Debug, Clone)] |
| 113 | +pub struct AppearanceSettings { |
| 114 | + pub theme_variant: ThemeVariant, |
| 115 | + pub animation_speed: f32, |
| 116 | + pub show_borders: bool, |
| 117 | + pub font_size: u16, |
| 118 | +} |
| 119 | + |
| 120 | +/// Model configuration settings |
| 121 | +#[derive(Debug, Clone)] |
| 122 | +pub struct ModelSettings { |
| 123 | + pub default_model: String, |
| 124 | + pub temperature: f32, |
| 125 | + pub max_tokens: u32, |
| 126 | + pub timeout_seconds: u32, |
| 127 | +} |
| 128 | + |
| 129 | +/// Keybinding configuration |
| 130 | +#[derive(Debug, Clone)] |
| 131 | +pub struct KeybindSettings { |
| 132 | + pub quit_keys: Vec<String>, |
| 133 | + pub theme_toggle_key: String, |
| 134 | + pub help_key: String, |
| 135 | +} |
| 136 | + |
| 137 | +/// Advanced configuration options |
| 138 | +#[derive(Debug, Clone)] |
| 139 | +pub struct AdvancedSettings { |
| 140 | + pub debug_mode: bool, |
| 141 | + pub performance_mode: bool, |
| 142 | + pub log_level: String, |
| 143 | + pub auto_save: bool, |
| 144 | +} |
| 145 | + |
| 146 | +/// Settings manager for handling persistence and validation |
| 147 | +/// Future implementation will include file-based configuration |
| 148 | +#[derive(Debug)] |
| 149 | +pub struct SettingsManager { |
| 150 | + settings: Settings, |
| 151 | + // config_path: PathBuf, |
| 152 | + // auto_save: bool, |
| 153 | +} |
| 154 | + |
| 155 | +impl SettingsManager { |
| 156 | + /// Create new settings manager |
| 157 | + pub fn new() -> Self { |
| 158 | + Self { |
| 159 | + settings: Settings::new(), |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /// Get immutable reference to current settings |
| 164 | + pub fn get(&self) -> &Settings { |
| 165 | + &self.settings |
| 166 | + } |
| 167 | + |
| 168 | + /// Get mutable reference to current settings |
| 169 | + pub fn get_mut(&mut self) -> &mut Settings { |
| 170 | + &mut self.settings |
| 171 | + } |
| 172 | + |
| 173 | + /// Apply action to settings |
| 174 | + pub fn apply_action(&mut self, action: SettingsAction) -> Result<(), SettingsError> { |
| 175 | + self.settings.handle_action(action); |
| 176 | + self.settings.validate()?; |
| 177 | + // Future: auto-save if enabled |
| 178 | + Ok(()) |
| 179 | + } |
| 180 | + |
| 181 | + /// Reset settings to defaults |
| 182 | + pub fn reset_to_defaults(&mut self) { |
| 183 | + self.settings = Settings::new(); |
| 184 | + } |
| 185 | + |
| 186 | + // Future methods: |
| 187 | + // pub fn load_from_file(&mut self, path: &Path) -> Result<(), SettingsError> |
| 188 | + // pub fn save_to_file(&self, path: &Path) -> Result<(), SettingsError> |
| 189 | + // pub fn auto_save(&self) -> Result<(), SettingsError> |
| 190 | +} |
| 191 | + |
| 192 | +impl Default for SettingsManager { |
| 193 | + fn default() -> Self { |
| 194 | + Self::new() |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +#[cfg(test)] |
| 199 | +mod tests { |
| 200 | + use super::*; |
| 201 | + |
| 202 | + #[test] |
| 203 | + fn test_settings_creation() { |
| 204 | + let settings = Settings::new(); |
| 205 | + assert_eq!(settings.theme_variant, ThemeVariant::EverforestDark); |
| 206 | + } |
| 207 | + |
| 208 | + #[test] |
| 209 | + fn test_theme_toggle() { |
| 210 | + let mut settings = Settings::new(); |
| 211 | + assert_eq!(settings.theme_variant, ThemeVariant::EverforestDark); |
| 212 | + |
| 213 | + settings.toggle_theme(); |
| 214 | + assert_eq!(settings.theme_variant, ThemeVariant::EverforestLight); |
| 215 | + |
| 216 | + settings.toggle_theme(); |
| 217 | + assert_eq!(settings.theme_variant, ThemeVariant::EverforestDark); |
| 218 | + } |
| 219 | + |
| 220 | + #[test] |
| 221 | + fn test_settings_action() { |
| 222 | + let mut settings = Settings::new(); |
| 223 | + let action = SettingsAction::ChangeTheme(ThemeVariant::EverforestLight); |
| 224 | + |
| 225 | + settings.handle_action(action); |
| 226 | + assert_eq!(settings.theme_variant, ThemeVariant::EverforestLight); |
| 227 | + } |
| 228 | + |
| 229 | + #[test] |
| 230 | + fn test_settings_validation() { |
| 231 | + let settings = Settings::new(); |
| 232 | + assert!(settings.validate().is_ok()); |
| 233 | + } |
| 234 | + |
| 235 | + #[test] |
| 236 | + fn test_settings_manager() { |
| 237 | + let mut manager = SettingsManager::new(); |
| 238 | + let action = SettingsAction::ChangeTheme(ThemeVariant::EverforestLight); |
| 239 | + |
| 240 | + assert!(manager.apply_action(action).is_ok()); |
| 241 | + assert_eq!(manager.get().theme_variant, ThemeVariant::EverforestLight); |
| 242 | + } |
| 243 | +} |
0 commit comments