ocy_core/
matcher.rs

1use std::sync::Arc;
2
3use glob::Pattern;
4
5use crate::models::FileInfo;
6
7pub struct Matcher {
8    pub name: Arc<str>,
9    pub to_match: Pattern,
10    pub clean_strategy: CleanStrategy,
11}
12pub enum CleanStrategy {
13    Remove(RemovalPattern),
14    RunCommand(Arc<str>),
15}
16
17pub struct RemovalPattern(Pattern);
18
19impl Matcher {
20    pub fn with_remove_strategy(name: Arc<str>, to_match: Pattern, to_remove: Pattern) -> Self {
21        let clean_strategy = CleanStrategy::Remove(RemovalPattern(to_remove));
22        Self {
23            name,
24            to_match,
25            clean_strategy,
26        }
27    }
28
29    pub fn with_command_strategy(name: Arc<str>, to_match: Pattern, command: String) -> Self {
30        let clean_strategy = CleanStrategy::RunCommand(command.into());
31        Self {
32            name,
33            to_match,
34            clean_strategy,
35        }
36    }
37
38    pub fn any_entry_match(&self, entries: &[FileInfo]) -> bool {
39        entries.iter().any(|e| self.to_match.matches(&e.name))
40    }
41}
42
43impl RemovalPattern {
44    pub fn find_files_to_remove(&self, entries: Vec<FileInfo>) -> (Vec<FileInfo>, Vec<FileInfo>) {
45        entries.into_iter().partition(|e| self.0.matches(&e.name))
46    }
47}