use serde::Serialize;
use std::path::PathBuf;
use std::time::UNIX_EPOCH;
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum CleanerGroup {
Cache,
Browser,
History,
Logs,
System,
Other,
}
impl CleanerGroup {
pub fn as_str(self) -> &'static str {
match self {
CleanerGroup::Cache => "cache",
CleanerGroup::Browser => "browser",
CleanerGroup::History => "history",
CleanerGroup::Logs => "logs",
CleanerGroup::System => "system",
CleanerGroup::Other => "other",
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct Item {
pub path: PathBuf,
pub bytes: u64,
pub cleaner_id: String,
pub group: CleanerGroup,
#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub modified_epoch: Option<u64>,
pub is_dir: bool,
}
#[derive(Debug, Clone, Serialize)]
pub struct Plan {
pub items: Vec<Item>,
pub total_bytes: u64,
}
impl Plan {
pub fn new(items: Vec<Item>) -> Self {
let total_bytes = items.iter().map(|i| i.bytes).sum();
Self { items, total_bytes }
}
}
#[derive(Debug, Clone, Serialize)]
pub struct CleanerSummary {
pub cleaner_id: String,
pub group: CleanerGroup,
pub items: usize,
pub total_bytes: u64,
}
#[derive(Debug, Clone, Serialize)]
pub struct RunReport {
pub action: String,
pub plan: Plan,
pub summaries: Vec<CleanerSummary>,
pub applied: bool,
pub moved: usize,
}
pub fn modified_epoch_seconds(meta: &std::fs::Metadata) -> Option<u64> {
meta.modified()
.ok()
.and_then(|t| t.duration_since(UNIX_EPOCH).ok())
.map(|d| d.as_secs())
}