mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 04:58:42 +00:00
Add new trait PathExt
& impl for Path
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
0162f5f462
commit
cb2be5a882
2 changed files with 37 additions and 0 deletions
|
@ -24,6 +24,9 @@ pub use extracter::TarEntriesVisitor;
|
|||
|
||||
mod readable_rx;
|
||||
|
||||
mod path_ext;
|
||||
pub use path_ext::*;
|
||||
|
||||
/// Load binstall metadata from the crate `Cargo.toml` at the provided path
|
||||
pub fn load_manifest_path<P: AsRef<Path>>(
|
||||
manifest_path: P,
|
||||
|
|
34
src/helpers/path_ext.rs
Normal file
34
src/helpers/path_ext.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
use std::path::{Component, Path, PathBuf};
|
||||
|
||||
trait PathExt {
|
||||
fn normalize_path(&self) -> PathBuf;
|
||||
}
|
||||
|
||||
impl PathExt for Path {
|
||||
fn normalize_path(&self) -> PathBuf {
|
||||
let mut components = self.components().peekable();
|
||||
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
|
||||
components.next();
|
||||
PathBuf::from(c.as_os_str())
|
||||
} else {
|
||||
PathBuf::new()
|
||||
};
|
||||
|
||||
for component in components {
|
||||
match component {
|
||||
Component::Prefix(..) => unreachable!(),
|
||||
Component::RootDir => {
|
||||
ret.push(component.as_os_str());
|
||||
}
|
||||
Component::CurDir => {}
|
||||
Component::ParentDir => {
|
||||
ret.pop();
|
||||
}
|
||||
Component::Normal(c) => {
|
||||
ret.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue