summaryrefslogtreecommitdiff
path: root/src/srcdir.rs
blob: b1a9317ef5f43374b8dc6ba51d7bfb6afb7929d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use log::trace;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

#[derive(Debug, thiserror::Error)]
pub enum SourceDirError {
    #[error("failed to list files in source directory: {0}")]
    WalkDir(PathBuf, #[source] walkdir::Error),
}

pub struct SourceDir {
    path: PathBuf,
    files: Vec<PathBuf>,
    artificial: bool,
}

impl SourceDir {
    pub fn new(path: &Path) -> Self {
        Self {
            path: path.into(),
            files: vec![],
            artificial: false,
        }
    }

    pub fn insert_for_tests<P>(&mut self, path: P)
    where
        P: AsRef<Path>,
    {
        self.artificial = true;
        self.insert(path.as_ref());
    }

    pub fn insert(&mut self, path: &Path) {
        trace!("Source Dir::insert: path={}", path.display());
        self.files.push(path.into());
    }

    pub fn scan(&mut self) -> Result<(), SourceDirError> {
        if self.artificial {
            trace!("SourceDir::scan: artificial mode, not actually scanning");
        } else {
            trace!("SourceDir::scan: find files in {}", self.path.display());
            for e in WalkDir::new(&self.path) {
                let e = e.map_err(|err| SourceDirError::WalkDir(self.path.clone(), err))?;
                let path = e.path();
                trace!("SourceDir::scan: found {}", path.display());
                self.insert(path);
            }
        }
        Ok(())
    }

    pub fn files(&self) -> &[PathBuf] {
        &self.files
    }
}

#[derive(Default)]
pub struct PathFilter {
    excluded_substrings: Vec<&'static str>,
    excluded_suffixes: Vec<&'static str>,
}

impl PathFilter {
    pub fn new(subs: &[&'static str], suffixes: &[&'static str]) -> Self {
        Self {
            excluded_substrings: subs.to_vec(),
            excluded_suffixes: suffixes.to_vec(),
        }
    }

    pub fn exclude_substring(&mut self, s: &'static str) {
        self.excluded_substrings.push(s);
    }

    pub fn exclude_suffix(&mut self, s: &'static str) {
        self.excluded_suffixes.push(s);
    }

    pub fn is_included<P>(&self, path: P) -> bool
    where
        P: AsRef<Path>,
    {
        let path = path.as_ref();
        let include = {
            let path = path.to_string_lossy();
            for pat in self.excluded_suffixes.iter() {
                if path.ends_with(pat) {
                    return false;
                }
            }
            for pat in self.excluded_substrings.iter() {
                if path.contains(pat) {
                    return false;
                }
            }
            true
        };
        if include {
            trace!("include {}", path.display());
        } else {
            trace!("exclude {}", path.display());
        }
        include
    }
}

#[cfg(test)]
mod test {
    use super::PathFilter;

    #[test]
    fn includes_dotgit_by_default() {
        let filter = PathFilter::default();
        assert!(filter.is_included(".git"));
    }

    #[test]
    fn excludes_dotgit_if_requested() {
        let mut filter = PathFilter::default();
        filter.exclude_substring(".git");
        assert!(!filter.is_included(".git"));
    }

    #[test]
    fn includes_footilde_by_default() {
        let filter = PathFilter::default();
        assert!(filter.is_included("foo~"));
    }

    #[test]
    fn includes_footildebar_if_tilde_suffix_is_excluded() {
        let mut filter = PathFilter::default();
        filter.exclude_suffix("~");
        assert!(filter.is_included("foo~bar"));
    }

    #[test]
    fn excludes_footilde_if_tilde_suffix_is_excluded() {
        let mut filter = PathFilter::default();
        filter.exclude_suffix("~");
        assert!(!filter.is_included("foo~"));
    }
}