|
| 1 | +//! Configuration management for contextd |
| 2 | +//! |
| 3 | +//! Handles loading and parsing the /etc/contextd/config.toml file. |
| 4 | +
|
| 5 | +use once_cell::sync::Lazy; |
| 6 | +use serde::Deserialize; |
| 7 | +use std::fs; |
| 8 | + |
| 9 | +/// Global configuration instance |
| 10 | +pub static CONFIG: Lazy<Config> = Lazy::new(Config::load); |
| 11 | + |
| 12 | +/// Main configuration structure |
| 13 | +#[derive(Debug, Deserialize, Clone, Default)] |
| 14 | +pub struct Config { |
| 15 | + #[serde(default)] |
| 16 | + pub ttls: TtlConfig, |
| 17 | + #[serde(default)] |
| 18 | + pub blacklist: BlacklistConfig, |
| 19 | +} |
| 20 | + |
| 21 | +/// TTL settings for various detectors (in seconds) |
| 22 | +#[derive(Debug, Deserialize, Clone)] |
| 23 | +pub struct TtlConfig { |
| 24 | + /// TTL for active game detection (default 5s) |
| 25 | + pub games: u64, |
| 26 | + /// TTL for hardware inventory (default 10s) |
| 27 | + pub hardware: u64, |
| 28 | + /// TTL for system diagnostics (default 300s / 5m) |
| 29 | + pub diagnostics: u64, |
| 30 | +} |
| 31 | + |
| 32 | +impl Default for TtlConfig { |
| 33 | + fn default() -> Self { |
| 34 | + Self { |
| 35 | + games: 5, |
| 36 | + hardware: 10, |
| 37 | + diagnostics: 300, |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// Blacklist settings for ignoring specific items |
| 43 | +#[derive(Debug, Deserialize, Clone, Default)] |
| 44 | +pub struct BlacklistConfig { |
| 45 | + /// Process names to ignore in game detection |
| 46 | + pub processes: Vec<String>, |
| 47 | + /// Hardware paths (udev paths) to ignore |
| 48 | + pub devices: Vec<String>, |
| 49 | +} |
| 50 | + |
| 51 | +impl Config { |
| 52 | + /// Loads the configuration from /etc/contextd/config.toml, |
| 53 | + /// falling back to defaults if the file is missing or invalid. |
| 54 | + fn load() -> Self { |
| 55 | + let paths = ["/etc/contextd/config.toml", "config.toml"]; |
| 56 | + |
| 57 | + for path in paths { |
| 58 | + if let Ok(content) = fs::read_to_string(path) { |
| 59 | + if let Ok(config) = toml::from_str(&content) { |
| 60 | + log::info!("Loaded configuration from {}", path); |
| 61 | + return config; |
| 62 | + } else { |
| 63 | + log::warn!("Failed to parse configuration at {}, using defaults", path); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + log::debug!("No configuration file found, using defaults"); |
| 69 | + Self::default() |
| 70 | + } |
| 71 | +} |
0 commit comments