Skip to content

Commit 541e42c

Browse files
committed
repo_path: impl Extend for RepoPathBuf
1 parent a2c44ab commit 541e42c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

lib/src/repo_path.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,17 @@ impl PartialOrd for RepoPathBuf {
503503
}
504504
}
505505

506+
impl<P: AsRef<RepoPathComponent>> Extend<P> for RepoPathBuf {
507+
fn extend<T: IntoIterator<Item = P>>(&mut self, iter: T) {
508+
for component in iter {
509+
if !self.value.is_empty() {
510+
self.value.push('/');
511+
}
512+
self.value.push_str(component.as_ref().as_internal_str());
513+
}
514+
}
515+
}
516+
506517
/// `RepoPath` contained invalid file/directory component such as `..`.
507518
#[derive(Clone, Debug, Eq, Error, PartialEq)]
508519
#[error(r#"Invalid repository path "{}""#, path.as_internal_file_string())]
@@ -814,6 +825,19 @@ mod tests {
814825
);
815826
}
816827

828+
#[test]
829+
fn test_extend() {
830+
let mut path = RepoPathBuf::root();
831+
path.extend(std::iter::empty::<RepoPathComponentBuf>());
832+
assert_eq!(path.as_ref(), RepoPath::root());
833+
path.extend([repo_path_component("dir")]);
834+
assert_eq!(path.as_ref(), repo_path("dir"));
835+
path.extend(std::iter::repeat_n(repo_path_component("subdir"), 3));
836+
assert_eq!(path.as_ref(), repo_path("dir/subdir/subdir/subdir"));
837+
path.extend(std::iter::empty::<RepoPathComponentBuf>());
838+
assert_eq!(path.as_ref(), repo_path("dir/subdir/subdir/subdir"));
839+
}
840+
817841
#[test]
818842
fn test_parent() {
819843
let root = RepoPath::root();

0 commit comments

Comments
 (0)