From ee427dddbaf5ce03cb6b7175241ff65b4c3d23eb Mon Sep 17 00:00:00 2001 From: m1ngsama Date: Sat, 29 Nov 2025 10:30:00 +0800 Subject: [PATCH] feat: Implement configuration management module - Create Config struct with serde support for JSON parsing - Define DisplayConfig for UI toggle options - Define AlertThresholds for monitoring alerts - Implement Default trait for sensible defaults - Add load() method to read config from file - Support same config format as Python version for compatibility --- src/config.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/config.rs diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..5eac9b5 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,59 @@ +use serde::{Deserialize, Serialize}; +use std::fs; +use std::path::Path; +use anyhow::Result; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Config { + pub update_interval: u64, + pub display: DisplayConfig, + pub process_limit: usize, + pub alert_thresholds: AlertThresholds, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DisplayConfig { + pub show_cpu: bool, + pub show_memory: bool, + pub show_disk: bool, + pub show_network: bool, + pub show_processes: bool, + pub show_temperatures: bool, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct AlertThresholds { + pub cpu_percent: f32, + pub memory_percent: f32, + pub disk_percent: f32, +} + +impl Default for Config { + fn default() -> Self { + Config { + update_interval: 5, + display: DisplayConfig { + show_cpu: true, + show_memory: true, + show_disk: true, + show_network: true, + show_processes: true, + show_temperatures: true, + }, + process_limit: 5, + alert_thresholds: AlertThresholds { + cpu_percent: 80.0, + memory_percent: 85.0, + disk_percent: 90.0, + }, + } + } +} + +impl Config { + pub fn load>(path: P) -> Result { + let contents = fs::read_to_string(path)?; + let config: Config = serde_json::from_str(&contents)?; + Ok(config) + } +}