path_matchers/
path_matcher_glob.rs

1use crate::PathMatcher;
2use glob::Pattern;
3use std::path::Path;
4
5pub use glob::PatternError;
6
7/// Matches file paths against Unix shell style patterns.
8pub fn glob(pattern: &str) -> Result<impl PathMatcher, PatternError> {
9    Pattern::new(pattern).map(PathMatcherGlob)
10}
11
12struct PathMatcherGlob(Pattern);
13
14impl PathMatcher for PathMatcherGlob {
15    fn matches(&self, path: &Path) -> bool {
16        self.0.matches_path(path)
17    }
18}
19
20#[cfg(test)]
21mod tests {
22
23    use super::glob;
24    use crate::PathMatcher;
25    use std::path::PathBuf;
26
27    #[test]
28    fn path_matcher_glob() {
29        let path = PathBuf::from("abc");
30        assert_eq!(glob("abc").unwrap().matches(&path), true);
31        assert_eq!(glob("a*").unwrap().matches(&path), true);
32        assert_eq!(glob("a*e").unwrap().matches(&path), false);
33    }
34}