Skip to content

Commit a92651e

Browse files
committed
repo_path: add ancestors() iterator
This adds a method `ancestors()` to `RepoPath` which work analogously to `std::path::Path::ancestors()`.
1 parent 541e42c commit a92651e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lib/src/repo_path.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ impl RepoPath {
427427
RepoPathComponentsIter { value: &self.value }
428428
}
429429

430+
pub fn ancestors(&self) -> impl Iterator<Item = &RepoPath> {
431+
std::iter::successors(Some(self), |path| path.parent())
432+
}
433+
430434
pub fn join(&self, entry: &RepoPathComponent) -> RepoPathBuf {
431435
let value = if self.value.is_empty() {
432436
entry.as_internal_str().to_owned()
@@ -890,6 +894,22 @@ mod tests {
890894
);
891895
}
892896

897+
#[test]
898+
fn test_ancestors() {
899+
assert_eq!(
900+
RepoPath::root().ancestors().collect_vec(),
901+
vec![RepoPath::root()]
902+
);
903+
assert_eq!(
904+
repo_path("dir").ancestors().collect_vec(),
905+
vec![repo_path("dir"), RepoPath::root()]
906+
);
907+
assert_eq!(
908+
repo_path("dir/subdir").ancestors().collect_vec(),
909+
vec![repo_path("dir/subdir"), repo_path("dir"), RepoPath::root()]
910+
);
911+
}
912+
893913
#[test]
894914
fn test_to_fs_path() {
895915
assert_eq!(

0 commit comments

Comments
 (0)