Skip to main content

amber/
ignore.rs

1use std::path::Path;
2
3pub use ignore::gitignore::Gitignore;
4
5// ---------------------------------------------------------------------------------------------------------------------
6// Ignore
7// ---------------------------------------------------------------------------------------------------------------------
8
9pub trait Ignore {
10    fn is_ignore(&self, path: &Path, is_dir: bool) -> bool;
11}
12
13// ---------------------------------------------------------------------------------------------------------------------
14// IgnoreVcs
15// ---------------------------------------------------------------------------------------------------------------------
16
17pub struct IgnoreVcs {
18    vcs_dirs: Vec<String>,
19}
20
21impl Default for IgnoreVcs {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl IgnoreVcs {
28    pub fn new() -> Self {
29        IgnoreVcs {
30            vcs_dirs: vec![
31                ".svn".to_string(),
32                ".hg".to_string(),
33                ".git".to_string(),
34                ".bzr".to_string(),
35            ],
36        }
37    }
38}
39
40impl Ignore for IgnoreVcs {
41    fn is_ignore(&self, path: &Path, is_dir: bool) -> bool {
42        if is_dir {
43            for d in &self.vcs_dirs {
44                if path.ends_with(d) {
45                    return true;
46                }
47            }
48        }
49        false
50    }
51}
52
53// ---------------------------------------------------------------------------------------------------------------------
54// IgnoreGit
55// ---------------------------------------------------------------------------------------------------------------------
56
57impl Ignore for Gitignore {
58    fn is_ignore(&self, path: &Path, is_dir: bool) -> bool {
59        match self.matched(path, is_dir) {
60            ignore::Match::None | ignore::Match::Whitelist(_) => false,
61            ignore::Match::Ignore(_) => true,
62        }
63    }
64}
65
66// ---------------------------------------------------------------------------------------------------------------------
67// Test
68// ---------------------------------------------------------------------------------------------------------------------
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73    use std::path::PathBuf;
74
75    #[test]
76    fn ignore_git() {
77        let ignore = Gitignore::new(PathBuf::from("./test/.gitignore")).0;
78
79        assert!(!ignore.is_ignore(&PathBuf::from("./test/ao"), false));
80        assert!(ignore.is_ignore(&PathBuf::from("./test/a.o"), false));
81        assert!(ignore.is_ignore(&PathBuf::from("./test/abc.o"), false));
82        assert!(ignore.is_ignore(&PathBuf::from("./test/a.s"), false));
83        assert!(!ignore.is_ignore(&PathBuf::from("./test/abc.s"), false));
84        assert!(ignore.is_ignore(&PathBuf::from("./test/d0.t"), false));
85        assert!(!ignore.is_ignore(&PathBuf::from("./test/d00.t"), false));
86        assert!(ignore.is_ignore(&PathBuf::from("./test/file"), false));
87        assert!(ignore.is_ignore(&PathBuf::from("./test/dir0/file"), false));
88        assert!(ignore.is_ignore(&PathBuf::from("./test/dir1/file"), false));
89        assert!(!ignore.is_ignore(&PathBuf::from("./test/x/file"), false));
90        assert!(!ignore.is_ignore(&PathBuf::from("./test/x/dir0/file"), false));
91        assert!(!ignore.is_ignore(&PathBuf::from("./test/x/dir1/file"), false));
92        assert!(ignore.is_ignore(&PathBuf::from("./test/dir2"), true));
93        assert!(ignore.is_ignore(&PathBuf::from("./test/dir3/dir4"), true));
94        assert!(ignore.is_ignore(&PathBuf::from("./test/dir5/dir6"), true));
95        assert!(ignore.is_ignore(&PathBuf::from("./test/dir7"), true));
96        assert!(ignore.is_ignore(&PathBuf::from("./test/dir3/dir7"), true));
97        assert!(ignore.is_ignore(&PathBuf::from("./test/dir8"), true));
98        assert!(ignore.is_ignore(&PathBuf::from("./test/dir9/dir10"), true));
99        assert!(ignore.is_ignore(&PathBuf::from("./test/dir11/dir12"), true));
100    }
101}