Add a function to get the shared portion of a path.
This commit is contained in:
parent
11bfb6836f
commit
68cae57f16
@ -1,3 +1,4 @@
|
|||||||
|
use std::path::Component;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
@ -92,6 +93,17 @@ fn count_shared_steps<A: AsRef<Path>, B: AsRef<Path>>(left: A, right: B) -> usiz
|
|||||||
.unwrap_or_else(|| left.components().count())
|
.unwrap_or_else(|| left.components().count())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn get_shared_steps<'a>(left: &'a Path, right: &'a Path) -> impl Iterator<Item = Component<'a>> {
|
||||||
|
let foo: Vec<Component<'_>> = left
|
||||||
|
.components()
|
||||||
|
.zip(right.components())
|
||||||
|
.take_while(|(l, r)| l == r)
|
||||||
|
.map(|(l, _r)| l)
|
||||||
|
.collect();
|
||||||
|
foo.into_iter()
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -106,4 +118,29 @@ mod tests {
|
|||||||
2
|
2
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_shared_steps() {
|
||||||
|
assert_eq!(
|
||||||
|
get_shared_steps(Path::new(""), Path::new("")).collect::<PathBuf>(),
|
||||||
|
PathBuf::from("")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
get_shared_steps(Path::new("foo.txt"), Path::new("foo.txt")).collect::<PathBuf>(),
|
||||||
|
PathBuf::from("foo.txt")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
get_shared_steps(Path::new("cat/foo.txt"), Path::new("dog/foo.txt"))
|
||||||
|
.collect::<PathBuf>(),
|
||||||
|
PathBuf::from("")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
get_shared_steps(
|
||||||
|
Path::new("foo/bar/baz/lorem.txt"),
|
||||||
|
Path::new("foo/bar/ipsum/dolar.txt")
|
||||||
|
)
|
||||||
|
.collect::<PathBuf>(),
|
||||||
|
PathBuf::from("foo/bar")
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user