cleanux 0.1.1

Linux cleaner CLI (CleanMyMac-like): scan & safely trash cache, find duplicates, list large files
Documentation
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 {
    /// Exclude these path prefixes from scans.
    #[serde(default)]
    pub exclude_prefixes: Vec<PathBuf>,
    /// Allow only paths that match these globs. Empty means allow all.
    #[serde(default)]
    pub allow_globs: Vec<String>,
    /// Deny paths that match these globs.
    #[serde(default)]
    pub deny_globs: Vec<String>,
    /// Enable/disable cleaners by id.
    #[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()?))
    }
}