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
This commit is contained in:
m1ngsama 2025-11-29 10:30:00 +08:00
parent 66a2f517c3
commit ee427dddba

59
src/config.rs Normal file
View file

@ -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<P: AsRef<Path>>(path: P) -> Result<Self> {
let contents = fs::read_to_string(path)?;
let config: Config = serde_json::from_str(&contents)?;
Ok(config)
}
}