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/// Album art rendering mode.
7#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
8#[serde(rename_all = "lowercase")]
9pub enum AlbumArtMode {
10    #[default]
11    Auto,
12    Off,
13    /// Catch-all for unrecognized values — behaves like Auto.
14    #[serde(other)]
15    Other,
16}
17
18impl AlbumArtMode {
19    /// Returns true when album art should be disabled.
20    pub fn is_off(&self) -> bool {
21        *self == Self::Off
22    }
23}
24
25/// User configuration loaded from config file with environment variable overrides.
26#[derive(Debug, Clone, Deserialize)]
27#[serde(default)]
28pub struct Config {
29    /// Default group to target when --speaker/--group not specified
30    pub default_group: Option<String>,
31    /// TUI color theme: "dark" or "light"
32    pub theme: String,
33    /// Album art rendering mode: "auto" or "off"
34    pub album_art_mode: AlbumArtMode,
35}
36
37impl Default for Config {
38    fn default() -> Self {
39        Self {
40            default_group: None,
41            theme: "dark".to_string(),
42            album_art_mode: AlbumArtMode::default(),
43        }
44    }
45}
46
47impl Config {
48    /// Load from config file with environment variable overrides.
49    pub fn load() -> Self {
50        let config_dir = std::env::var("SONOS_CONFIG_DIR")
51            .ok()
52            .filter(|s| !s.is_empty())
53            .map(PathBuf::from)
54            .filter(|p| p.is_absolute())
55            .or_else(|| dirs::config_dir().map(|p| p.join("sonos")));
56
57        let mut config: Config = config_dir
58            .map(|d| d.join("config.toml"))
59            .and_then(|p| std::fs::read_to_string(p).ok())
60            .and_then(|s| toml::from_str(&s).ok())
61            .unwrap_or_default();
62
63        // Environment variable overrides
64        if let Ok(group) = std::env::var("SONOS_DEFAULT_GROUP") {
65            config.default_group = Some(group);
66        }
67
68        config
69    }
70}