Skip to main content

ocy_core/
models.rs

1use std::{path::PathBuf, sync::Arc};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4pub enum SimpleFileKind {
5    File,
6    Directory,
7    /// A symbolic link, never resolved to its target.
8    ///
9    /// Links are tracked separately from [`SimpleFileKind::Directory`] so that neither the
10    /// walk nor the size estimate ever leaves the tree being scanned.
11    Symlink,
12}
13
14#[derive(Debug, Clone, PartialEq, Eq, Hash)]
15pub struct FileInfo {
16    pub path: PathBuf,
17    /// The entry name, lossily decoded.
18    ///
19    /// Names that are not valid UTF-8 still produce a usable [`FileInfo`]; `path` remains
20    /// the authoritative value for any filesystem operation.
21    pub name: String,
22    pub kind: SimpleFileKind,
23}
24
25impl FileInfo {
26    pub(crate) fn new(path: PathBuf, name: String, kind: SimpleFileKind) -> Self {
27        Self { path, name, kind }
28    }
29}
30
31#[derive(Debug)]
32pub enum RemovalAction {
33    Delete {
34        file_info: FileInfo,
35        file_size: Option<u64>,
36    },
37    RunCommand {
38        work_dir: FileInfo,
39        command: Arc<str>,
40    },
41}
42
43#[derive(Debug)]
44pub struct RemovalCandidate {
45    pub matcher_name: Arc<str>,
46    pub action: RemovalAction,
47}
48
49impl RemovalCandidate {
50    pub fn new(matcher_name: Arc<str>, file_info: FileInfo, file_size: Option<u64>) -> Self {
51        let action = RemovalAction::Delete {
52            file_info,
53            file_size,
54        };
55        Self {
56            matcher_name,
57            action,
58        }
59    }
60
61    pub fn new_cmd(matcher_name: Arc<str>, work_dir: FileInfo, command: Arc<str>) -> Self {
62        let action = RemovalAction::RunCommand { work_dir, command };
63        Self {
64            matcher_name,
65            action,
66        }
67    }
68
69    pub fn estimate_file_size(&self) -> u64 {
70        match &self.action {
71            RemovalAction::Delete { file_size, .. } => file_size.unwrap_or(0),
72            RemovalAction::RunCommand { .. } => 0,
73        }
74    }
75
76    pub fn file_size(&self) -> Option<u64> {
77        match &self.action {
78            RemovalAction::Delete { file_size, .. } => *file_size,
79            RemovalAction::RunCommand { .. } => None,
80        }
81    }
82}