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 maplit::hashset;
use crate::repo_path::{RepoPath, RepoPathComponent};
#[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)]
pub enum VisitDirs {
All,
@ -46,6 +57,19 @@ pub trait Matcher {
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)]
pub struct EverythingMatcher;
@ -230,18 +254,20 @@ mod tests {
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]
fn test_filesmatcher_empty() {
let m = FilesMatcher::new(hashset! {});
assert!(!m.matches(&RepoPath::from_internal_string("file")));
assert!(!m.matches(&RepoPath::from_internal_string("dir/file")));
assert_eq!(
m.visit(&RepoPath::root()),
Visit::Specific {
dirs: VisitDirs::Set(hashset! {}),
files: VisitFiles::Set(hashset! {}),
}
);
assert_eq!(m.visit(&RepoPath::root()), Visit::nothing());
}
#[test]
@ -292,13 +318,7 @@ mod tests {
let m = PrefixMatcher::new(&[]);
assert!(!m.matches(&RepoPath::from_internal_string("file")));
assert!(!m.matches(&RepoPath::from_internal_string("dir/file")));
assert_eq!(
m.visit(&RepoPath::root()),
Visit::Specific {
dirs: VisitDirs::Set(hashset! {}),
files: VisitFiles::Set(hashset! {}),
}
);
assert_eq!(m.visit(&RepoPath::root()), Visit::nothing());
}
#[test]
@ -364,10 +384,7 @@ mod tests {
// visit
assert_eq!(
m.visit(&RepoPath::from_internal_string("bar")),
Visit::Specific {
dirs: VisitDirs::Set(hashset! {}),
files: VisitFiles::Set(hashset! {}),
}
Visit::nothing()
);
}