Skip to main content

sonos_cli/
config.rs

1//! Configuration system for the sonos-cli application.
2
3use serde::Deserialize;
4use std::path::PathBuf;
5
6/// User configuration loaded from config file with environment variable overrides.
7#[derive(Debug, Clone, Deserialize)]
8#[serde(default)]
9pub struct Config {
10    /// Default group to target when --speaker/--group not specified
11    pub default_group: Option<String>,
12    /// TUI color theme: "dark" or "light"
13    pub theme: String,
14}
15
16impl Default for Config {
17    fn default() -> Self {
18        Self {
19            default_group: None,
20            theme: "dark".to_string(),
21        }
22    }
23}
24
25impl Config {
26    /// Load from config file with environment variable overrides.
27    pub fn load() -> Self {
28        let config_dir = std::env::var("SONOS_CONFIG_DIR")
29            .ok()
30            .filter(|s| !s.is_empty())
31            .map(PathBuf::from)
32            .filter(|p| p.is_absolute())
33            .or_else(|| dirs::config_dir().map(|p| p.join("sonos")));
34
35        let mut config: Config = config_dir
36            .map(|d| d.join("config.toml"))
37            .and_then(|p| std::fs::read_to_string(p).ok())
38            .and_then(|s| toml::from_str(&s).ok())
39            .unwrap_or_default();
40
41        // Environment variable overrides
42        if let Ok(group) = std::env::var("SONOS_DEFAULT_GROUP") {
43            config.default_group = Some(group);
44        }
45
46        config
47    }
48}