use anyhow::Result;
use directories::ProjectDirs;
use globset::{Glob, GlobSet, GlobSetBuilder};
use serde::Deserialize;
use std::{collections::HashMap, fs, path::PathBuf};
#[derive(Debug, Deserialize, Default, Clone)]
pub struct Config {
#[serde(default)]
pub exclude_prefixes: Vec<PathBuf>,
#[serde(default)]
pub allow_globs: Vec<String>,
#[serde(default)]
pub deny_globs: Vec<String>,
#[serde(default)]
pub cleaners: HashMap<String, bool>,
}
pub fn load_config() -> Result<Config> {
let proj = ProjectDirs::from("dev", "cleanux", "cleanux")
.expect("could not determine config directory");
let cfg_path = proj.config_dir().join("config.toml");
if !cfg_path.exists() {
return Ok(Config::default());
}
let txt = fs::read_to_string(cfg_path)?;
Ok(toml::from_str(&txt)?)
}
impl Config {
pub fn is_cleaner_enabled(&self, id: &str) -> bool {
self.cleaners.get(id).copied().unwrap_or(true)
}
pub fn build_globset(globs: &[String]) -> Result<Option<GlobSet>> {
if globs.is_empty() {
return Ok(None);
}
let mut builder = GlobSetBuilder::new();
for g in globs {
builder.add(Glob::new(g)?);
}
Ok(Some(builder.build()?))
}
}