ok/jj
1
0
Fork 0
forked from mirrors/jj

matchers: add method to create visit sets from Dirs, remove separate getters

We might also want to eliminate .collect()/clone() here, but that's not
considered yet.
This commit is contained in:
Yuya Nishihara 2022-12-28 17:44:01 +09:00
parent ba632e6ef6
commit 2fb0363c03

View file

@ -117,9 +117,7 @@ impl Matcher for FilesMatcher {
} }
fn visit(&self, dir: &RepoPath) -> Visit { fn visit(&self, dir: &RepoPath) -> Visit {
let dirs = self.dirs.get_dirs(dir); self.dirs.get_visit_sets(dir)
let files = self.dirs.get_files(dir);
Visit::sets(dirs, files)
} }
} }
@ -159,9 +157,7 @@ impl Matcher for PrefixMatcher {
if self.matches(dir) { if self.matches(dir) {
Visit::AllRecursively Visit::AllRecursively
} else { } else {
let dirs = self.dirs.get_dirs(dir); self.dirs.get_visit_sets(dir)
let files = self.dirs.get_files(dir);
Visit::sets(dirs, files)
} }
} }
} }
@ -310,26 +306,24 @@ impl Dirs {
.try_fold(self, |sub, name| sub.entries.get(name)) .try_fold(self, |sub, name| sub.entries.get(name))
} }
fn get_dirs(&self, dir: &RepoPath) -> HashSet<RepoPathComponent> { fn get_visit_sets(&self, dir: &RepoPath) -> Visit {
self.get(dir) self.get(dir)
.map(|sub| { .map(Dirs::to_visit_sets)
sub.entries .unwrap_or(Visit::Nothing)
.iter()
.filter_map(|(name, sub)| sub.is_dir.then(|| name.clone()))
.collect()
})
.unwrap_or_default()
} }
fn get_files(&self, dir: &RepoPath) -> HashSet<RepoPathComponent> { fn to_visit_sets(&self) -> Visit {
self.get(dir) let mut dirs = HashSet::new();
.map(|sub| { let mut files = HashSet::new();
sub.entries for (name, sub) in &self.entries {
.iter() if sub.is_dir {
.filter_map(|(name, sub)| sub.is_file.then(|| name.clone())) dirs.insert(name.clone());
.collect() }
}) if sub.is_file {
.unwrap_or_default() files.insert(name.clone());
}
}
Visit::sets(dirs, files)
} }
} }
@ -343,14 +337,14 @@ mod tests {
#[test] #[test]
fn test_dirs_empty() { fn test_dirs_empty() {
let dirs = Dirs::new(); let dirs = Dirs::new();
assert_eq!(dirs.get_dirs(&RepoPath::root()), hashset! {}); assert_eq!(dirs.get_visit_sets(&RepoPath::root()), Visit::Nothing);
} }
#[test] #[test]
fn test_dirs_root() { fn test_dirs_root() {
let mut dirs = Dirs::new(); let mut dirs = Dirs::new();
dirs.add_dir(&RepoPath::root()); dirs.add_dir(&RepoPath::root());
assert_eq!(dirs.get_dirs(&RepoPath::root()), hashset! {}); assert_eq!(dirs.get_visit_sets(&RepoPath::root()), Visit::Nothing);
} }
#[test] #[test]
@ -358,17 +352,13 @@ mod tests {
let mut dirs = Dirs::new(); let mut dirs = Dirs::new();
dirs.add_dir(&RepoPath::from_internal_string("dir")); dirs.add_dir(&RepoPath::from_internal_string("dir"));
assert_eq!( assert_eq!(
dirs.get_dirs(&RepoPath::root()), dirs.get_visit_sets(&RepoPath::root()),
hashset! {RepoPathComponent::from("dir")} Visit::sets(hashset! {RepoPathComponent::from("dir")}, hashset! {}),
); );
dirs.add_dir(&RepoPath::from_internal_string("dir/sub")); dirs.add_dir(&RepoPath::from_internal_string("dir/sub"));
assert_eq!( assert_eq!(
dirs.get_dirs(&RepoPath::from_internal_string("dir")), dirs.get_visit_sets(&RepoPath::from_internal_string("dir")),
hashset! {RepoPathComponent::from("sub")} Visit::sets(hashset! {RepoPathComponent::from("sub")}, hashset! {}),
);
assert_eq!(
dirs.get_files(&RepoPath::from_internal_string("dir")),
hashset! {}
); );
} }
@ -377,17 +367,12 @@ mod tests {
let mut dirs = Dirs::new(); let mut dirs = Dirs::new();
dirs.add_file(&RepoPath::from_internal_string("dir/file")); dirs.add_file(&RepoPath::from_internal_string("dir/file"));
assert_eq!( assert_eq!(
dirs.get_dirs(&RepoPath::root()), dirs.get_visit_sets(&RepoPath::root()),
hashset! {RepoPathComponent::from("dir")} Visit::sets(hashset! {RepoPathComponent::from("dir")}, hashset! {}),
);
assert_eq!(dirs.get_files(&RepoPath::root()), hashset! {});
assert_eq!(
dirs.get_files(&RepoPath::from_internal_string("dir")),
hashset! {RepoPathComponent::from("file")}
); );
assert_eq!( assert_eq!(
dirs.get_dirs(&RepoPath::from_internal_string("dir")), dirs.get_visit_sets(&RepoPath::from_internal_string("dir")),
hashset! {} Visit::sets(hashset! {}, hashset! {RepoPathComponent::from("file")}),
); );
} }