cleanux 0.1.0

Linux cleaner CLI (CleanMyMac-like): scan & safely trash cache, find duplicates, list large files
Documentation
use crate::{
    fsutil,
    model::{Item, Plan},
};
use anyhow::{Context, Result};
use directories::BaseDirs;
use std::path::{Path, PathBuf};

fn is_excluded(p: &Path, excludes: &[PathBuf]) -> bool {
    excludes.iter().any(|ex| p.starts_with(ex))
}

/// Scan XDG user cache (usually ~/.cache) and return a plan consisting of
/// top-level entries. The plan is intended for "safe" deletion via moving to Trash.
pub fn scan_cache(excludes: &[PathBuf]) -> Result<Plan> {
    let base = BaseDirs::new().context("cannot resolve home dirs")?;
    let cache_dir: PathBuf = base.cache_dir().to_path_buf();

    if !cache_dir.exists() {
        return Ok(Plan::new(vec![]));
    }

    let mut items: Vec<Item> = Vec::new();
    for entry in std::fs::read_dir(&cache_dir)? {
        let entry = entry?;
        let p = entry.path();

        if is_excluded(&p, excludes) {
            continue;
        }

        // Size: file length, or recursive folder size
        let bytes = if p.is_file() {
            entry.metadata()?.len()
        } else {
            fsutil::dir_size_bytes(&p)?
        };

        items.push(Item { path: p, bytes });
    }

    Ok(Plan::new(items))
}