Skip to main content

dev_sweep/config/
mod.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use crate::scanner::ProjectKind;
6
7/// Persistent configuration for dev-sweep.
8#[derive(Debug, Clone, Default, Serialize, Deserialize)]
9pub struct DevSweepConfig {
10    /// Directories to always ignore during scanning.
11    #[serde(default)]
12    pub ignore_paths: Vec<PathBuf>,
13
14    /// Project kinds to exclude from scanning.
15    #[serde(default)]
16    pub exclude_kinds: Vec<ProjectKind>,
17
18    /// Default scan roots.
19    #[serde(default)]
20    pub default_roots: Vec<PathBuf>,
21
22    /// Maximum directory depth to scan.
23    #[serde(default)]
24    pub max_depth: Option<usize>,
25}
26
27impl DevSweepConfig {
28    /// Load config from the default location (~/.config/dev-sweep/config.json).
29    pub fn load() -> Self {
30        let config_path = Self::config_path();
31        if config_path.exists() {
32            match std::fs::read_to_string(&config_path) {
33                Ok(contents) => serde_json::from_str(&contents).unwrap_or_default(),
34                Err(_) => Self::default(),
35            }
36        } else {
37            Self::default()
38        }
39    }
40
41    /// Save config to the default location.
42    pub fn save(&self) -> anyhow::Result<()> {
43        let config_path = Self::config_path();
44        if let Some(parent) = config_path.parent() {
45            std::fs::create_dir_all(parent)?;
46        }
47        let json = serde_json::to_string_pretty(self)?;
48        std::fs::write(&config_path, json)?;
49        Ok(())
50    }
51
52    /// Get the default config file path.
53    pub fn config_path() -> PathBuf {
54        dirs::config_dir()
55            .unwrap_or_else(|| PathBuf::from("~/.config"))
56            .join("dev-sweep")
57            .join("config.json")
58    }
59}