Skip to main content

cleanux/
model.rs

1use serde::Serialize;
2use std::path::PathBuf;
3use std::time::UNIX_EPOCH;
4
5#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
6#[serde(rename_all = "snake_case")]
7pub enum CleanerGroup {
8    Cache,
9    Browser,
10    History,
11    Logs,
12    System,
13    Other,
14}
15
16impl CleanerGroup {
17    pub fn as_str(self) -> &'static str {
18        match self {
19            CleanerGroup::Cache => "cache",
20            CleanerGroup::Browser => "browser",
21            CleanerGroup::History => "history",
22            CleanerGroup::Logs => "logs",
23            CleanerGroup::System => "system",
24            CleanerGroup::Other => "other",
25        }
26    }
27}
28
29#[derive(Debug, Clone, Serialize)]
30pub struct Item {
31    pub path: PathBuf,
32    pub bytes: u64,
33    pub cleaner_id: String,
34    pub group: CleanerGroup,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub reason: Option<String>,
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub modified_epoch: Option<u64>,
39    pub is_dir: bool,
40}
41
42#[derive(Debug, Clone, Serialize)]
43pub struct Plan {
44    pub items: Vec<Item>,
45    pub total_bytes: u64,
46}
47
48impl Plan {
49    pub fn new(items: Vec<Item>) -> Self {
50        let total_bytes = items.iter().map(|i| i.bytes).sum();
51        Self { items, total_bytes }
52    }
53}
54
55#[derive(Debug, Clone, Serialize)]
56pub struct CleanerSummary {
57    pub cleaner_id: String,
58    pub group: CleanerGroup,
59    pub items: usize,
60    pub total_bytes: u64,
61}
62
63#[derive(Debug, Clone, Serialize)]
64pub struct RunReport {
65    pub action: String,
66    pub plan: Plan,
67    pub summaries: Vec<CleanerSummary>,
68    pub applied: bool,
69    pub moved: usize,
70}
71
72pub fn modified_epoch_seconds(meta: &std::fs::Metadata) -> Option<u64> {
73    meta.modified()
74        .ok()
75        .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
76        .map(|d| d.as_secs())
77}