matchers: add a matcher not matching anything (#52)

It can be useful for at least testing purposes to have a matcher that
doesn't match any paths.
This commit is contained in:
Martin von Zweigbergk 2022-02-12 09:39:03 -08:00 committed by Martin von Zweigbergk
parent d4732574f4
commit 4c2c1fedff

View file

@ -16,6 +16,8 @@
use std::collections::{BTreeSet, HashMap, HashSet}; use std::collections::{BTreeSet, HashMap, HashSet};
use maplit::hashset;
use crate::repo_path::{RepoPath, RepoPathComponent}; use crate::repo_path::{RepoPath, RepoPathComponent};
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]
@ -29,6 +31,15 @@ pub enum Visit {
}, },
} }
impl Visit {
pub fn nothing() -> Self {
Self::Specific {
dirs: VisitDirs::Set(hashset! {}),
files: VisitFiles::Set(hashset! {}),
}
}
}
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]
pub enum VisitDirs { pub enum VisitDirs {
All, All,
@ -46,6 +57,19 @@ pub trait Matcher {
fn visit(&self, dir: &RepoPath) -> Visit; fn visit(&self, dir: &RepoPath) -> Visit;
} }
#[derive(PartialEq, Eq, Debug)]
pub struct NothingMatcher;
impl Matcher for NothingMatcher {
fn matches(&self, _file: &RepoPath) -> bool {
false
}
fn visit(&self, _dir: &RepoPath) -> Visit {
Visit::nothing()
}
}
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]
pub struct EverythingMatcher; pub struct EverythingMatcher;
@ -230,18 +254,20 @@ mod tests {
assert_eq!(dirs.get_files(&RepoPath::root()), hashset! {}); assert_eq!(dirs.get_files(&RepoPath::root()), hashset! {});
} }
#[test]
fn test_nothingmatcher() {
let m = NothingMatcher;
assert!(!m.matches(&RepoPath::from_internal_string("file")));
assert!(!m.matches(&RepoPath::from_internal_string("dir/file")));
assert_eq!(m.visit(&RepoPath::root()), Visit::nothing());
}
#[test] #[test]
fn test_filesmatcher_empty() { fn test_filesmatcher_empty() {
let m = FilesMatcher::new(hashset! {}); let m = FilesMatcher::new(hashset! {});
assert!(!m.matches(&RepoPath::from_internal_string("file"))); assert!(!m.matches(&RepoPath::from_internal_string("file")));
assert!(!m.matches(&RepoPath::from_internal_string("dir/file"))); assert!(!m.matches(&RepoPath::from_internal_string("dir/file")));
assert_eq!( assert_eq!(m.visit(&RepoPath::root()), Visit::nothing());
m.visit(&RepoPath::root()),
Visit::Specific {
dirs: VisitDirs::Set(hashset! {}),
files: VisitFiles::Set(hashset! {}),
}
);
} }
#[test] #[test]
@ -292,13 +318,7 @@ mod tests {
let m = PrefixMatcher::new(&[]); let m = PrefixMatcher::new(&[]);
assert!(!m.matches(&RepoPath::from_internal_string("file"))); assert!(!m.matches(&RepoPath::from_internal_string("file")));
assert!(!m.matches(&RepoPath::from_internal_string("dir/file"))); assert!(!m.matches(&RepoPath::from_internal_string("dir/file")));
assert_eq!( assert_eq!(m.visit(&RepoPath::root()), Visit::nothing());
m.visit(&RepoPath::root()),
Visit::Specific {
dirs: VisitDirs::Set(hashset! {}),
files: VisitFiles::Set(hashset! {}),
}
);
} }
#[test] #[test]
@ -364,10 +384,7 @@ mod tests {
// visit // visit
assert_eq!( assert_eq!(
m.visit(&RepoPath::from_internal_string("bar")), m.visit(&RepoPath::from_internal_string("bar")),
Visit::Specific { Visit::nothing()
dirs: VisitDirs::Set(hashset! {}),
files: VisitFiles::Set(hashset! {}),
}
); );
} }