matchers: clean up .walk_to(dir) to yield &RepoPath instead of iterator

This commit is contained in:
Yuya Nishihara 2023-11-27 12:56:08 +09:00
parent bc9725c73c
commit 73690ed54e

View file

@ -19,7 +19,7 @@ use std::iter;
use tracing::instrument; use tracing::instrument;
use crate::repo_path::{RepoPath, RepoPathComponentBuf, RepoPathComponentsIter}; use crate::repo_path::{RepoPath, RepoPathComponentBuf};
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]
pub enum Visit { pub enum Visit {
@ -146,13 +146,13 @@ impl Matcher for PrefixMatcher {
} }
fn visit(&self, dir: &RepoPath) -> Visit { fn visit(&self, dir: &RepoPath) -> Visit {
for (sub, mut tail_components) in self.tree.walk_to(dir) { for (sub, tail_path) in self.tree.walk_to(dir) {
// 'is_file' means the current path matches prefix paths // 'is_file' means the current path matches prefix paths
if sub.is_file { if sub.is_file {
return Visit::AllRecursively; return Visit::AllRecursively;
} }
// 'dir' found, and is an ancestor of prefix paths // 'dir' found, and is an ancestor of prefix paths
if tail_components.next().is_none() { if tail_path.is_root() {
return sub.to_visit_sets(); return sub.to_visit_sets();
} }
} }
@ -309,16 +309,16 @@ impl RepoPathTree {
.unwrap_or(Visit::Nothing) .unwrap_or(Visit::Nothing)
} }
/// Walks the tree from the root to the given `dir`, yielding each sub tree
/// and remaining path.
fn walk_to<'a, 'b: 'a>( fn walk_to<'a, 'b: 'a>(
&'a self, &'a self,
dir: &'b RepoPath, dir: &'b RepoPath,
) -> impl Iterator<Item = (&'a RepoPathTree, RepoPathComponentsIter<'b>)> + 'a { ) -> impl Iterator<Item = (&'a RepoPathTree, &'b RepoPath)> + 'a {
iter::successors(Some((self, dir.components())), |(sub, components)| { iter::successors(Some((self, dir)), |(sub, dir)| {
// TODO: Add cheap as_path() method to the components iterator. let mut components = dir.components();
// Cloning iterator should be cheap, but looks a bit weird.
let mut components = components.clone();
let name = components.next()?; let name = components.next()?;
Some((sub.entries.get(name)?, components)) Some((sub.entries.get(name)?, components.as_path()))
}) })
} }