use crate::error::{ConfigError, Result};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct Config {
pub monitor: MonitorConfig,
pub cleaner: CleanerConfig,
pub scanner: ScannerConfig,
pub tui: TuiConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct MonitorConfig {
pub interval: u64,
pub warn_threshold: u8,
pub critical_threshold: u8,
pub mount_points: Vec<PathBuf>,
pub notification_backend: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct CleanerConfig {
pub project_types: Vec<String>,
pub exclude_patterns: Vec<String>,
pub min_age_days: u32,
pub max_depth: usize,
pub parallel_jobs: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct ScannerConfig {
pub parallel_threads: usize,
pub cross_filesystems: bool,
pub use_cache: bool,
pub cache_ttl: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct TuiConfig {
pub color_scheme: String,
pub show_hidden: bool,
pub default_sort: String,
pub large_dir_threshold: u64,
}
impl Default for MonitorConfig {
fn default() -> Self {
Self {
interval: 300,
warn_threshold: 80,
critical_threshold: 90,
mount_points: vec![],
notification_backend: "auto".to_string(),
}
}
}
impl Default for CleanerConfig {
fn default() -> Self {
Self {
project_types: vec![
"cargo".to_string(),
"gradle".to_string(),
"npm".to_string(),
"maven".to_string(),
],
exclude_patterns: vec!["**/.git".to_string(), "**/vendor".to_string()],
min_age_days: 7,
max_depth: 10,
parallel_jobs: 4,
}
}
}
impl Default for ScannerConfig {
fn default() -> Self {
Self {
parallel_threads: 0,
cross_filesystems: false,
use_cache: true,
cache_ttl: 3600,
}
}
}
impl Default for TuiConfig {
fn default() -> Self {
Self {
color_scheme: "auto".to_string(),
show_hidden: false,
default_sort: "size".to_string(),
large_dir_threshold: 1024 * 1024 * 1024, }
}
}
impl Config {
pub fn load(config_path: Option<&Path>) -> Result<Self> {
if let Some(path) = config_path {
return Self::load_from_file(path);
}
if let Some(path) = Self::find_config_file() {
return Self::load_from_file(&path);
}
Ok(Self::default())
}
fn find_config_file() -> Option<PathBuf> {
if let Some(config_dir) = dirs::config_dir() {
let path = config_dir.join("rusty-sweeper").join("config.toml");
if path.exists() {
return Some(path);
}
}
let system_path = PathBuf::from("/etc/rusty-sweeper/config.toml");
if system_path.exists() {
return Some(system_path);
}
None
}
fn load_from_file(path: &Path) -> Result<Self> {
let contents = std::fs::read_to_string(path).map_err(|e| ConfigError::ReadError {
path: path.to_path_buf(),
source: e,
})?;
let config: Config = toml::from_str(&contents).map_err(|e| ConfigError::ParseError {
path: path.to_path_buf(),
source: e,
})?;
config.validate()?;
Ok(config)
}
pub fn validate(&self) -> Result<()> {
if self.monitor.warn_threshold > 100 {
return Err(ConfigError::Invalid("warn_threshold must be 0-100".to_string()).into());
}
if self.monitor.critical_threshold > 100 {
return Err(
ConfigError::Invalid("critical_threshold must be 0-100".to_string()).into(),
);
}
if self.monitor.warn_threshold >= self.monitor.critical_threshold {
return Err(ConfigError::Invalid(
"warn_threshold must be less than critical_threshold".to_string(),
)
.into());
}
Ok(())
}
pub fn default_path() -> Option<PathBuf> {
dirs::config_dir().map(|d| d.join("rusty-sweeper").join("config.toml"))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_config_is_valid() {
let config = Config::default();
assert_eq!(config.monitor.warn_threshold, 80);
assert_eq!(config.monitor.critical_threshold, 90);
}
#[test]
fn config_serializes_to_toml() {
let config = Config::default();
let toml_str = toml::to_string_pretty(&config).unwrap();
assert!(toml_str.contains("[monitor]"));
}
#[test]
fn default_thresholds_are_valid() {
let config = MonitorConfig::default();
assert!(config.warn_threshold < config.critical_threshold);
assert!(config.critical_threshold <= 100);
}
#[test]
fn default_cleaner_has_common_project_types() {
let config = CleanerConfig::default();
assert!(config.project_types.contains(&"cargo".to_string()));
assert!(config.project_types.contains(&"npm".to_string()));
}
#[test]
fn load_returns_defaults_when_no_file() {
let config = Config::load(None).unwrap();
assert_eq!(config.monitor.interval, 300);
}
#[test]
fn load_fails_on_invalid_explicit_path() {
let result = Config::load(Some(Path::new("/nonexistent/config.toml")));
assert!(result.is_err());
}
#[test]
fn validate_catches_invalid_thresholds() {
let mut config = Config::default();
config.monitor.warn_threshold = 95;
config.monitor.critical_threshold = 90;
assert!(config.validate().is_err());
}
}