use crate::cleaners::{Cleaner, ScanContext, item_from_path};
use crate::model::{CleanerGroup, Plan};
use anyhow::Result;
use std::path::Path;
pub struct XdgCacheCleaner;
impl XdgCacheCleaner {
pub fn new() -> Self {
Self
}
}
impl Cleaner for XdgCacheCleaner {
fn id(&self) -> &'static str {
"xdg_cache"
}
fn description(&self) -> &'static str {
"Top-level entries under the XDG cache directory"
}
fn group(&self) -> CleanerGroup {
CleanerGroup::Cache
}
fn scan(&self, ctx: &ScanContext) -> Result<Plan> {
let cache_dir = ctx.base_dirs.cache_dir();
if !cache_dir.exists() {
return Ok(Plan::new(vec![]));
}
let mut items = Vec::new();
for entry in std::fs::read_dir(cache_dir)?.flatten() {
let p = entry.path();
if is_known_cache_subdir(&p) {
continue;
}
if let Some(item) = item_from_path(
&p,
self.id(),
self.group(),
Some("XDG cache entry"),
&ctx.filter,
)? {
items.push(item);
}
}
Ok(Plan::new(items))
}
}
fn is_known_cache_subdir(path: &Path) -> bool {
let name = match path.file_name().and_then(|n| n.to_str()) {
Some(name) => name,
None => return false,
};
matches!(
name,
"thumbnails"
| "mozilla"
| "chromium"
| "google-chrome"
| "google-chrome-beta"
| "google-chrome-unstable"
| "brave"
| "brave-browser"
| "vivaldi"
| "opera"
| "microsoft-edge"
| "microsoft-edge-beta"
| "microsoft-edge-dev"
)
}