mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-24 14:28:42 +00:00
Fix crate normalize-path
(#508)
* Impl new function `NormalizePath::is_normalized` * Always normalize and ret `PathBuf` in `NormalizePath::normalize` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
25a4ceb050
commit
c48bc2a264
2 changed files with 27 additions and 25 deletions
|
@ -113,11 +113,11 @@ impl BinFile {
|
|||
|
||||
if !is_valid_path(&path_normalized) {
|
||||
return Err(BinstallError::InvalidSourceFilePath {
|
||||
path: path_normalized.into_owned(),
|
||||
path: path_normalized,
|
||||
});
|
||||
}
|
||||
|
||||
data.bin_path.join(path_normalized.as_ref())
|
||||
data.bin_path.join(&path_normalized)
|
||||
};
|
||||
|
||||
// Destination at install dir + base-name{.extension}
|
||||
|
|
|
@ -18,10 +18,7 @@
|
|||
//! );
|
||||
//! ```
|
||||
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
path::{Component, Path, PathBuf},
|
||||
};
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
|
||||
/// Extension trait to add `normalize_path` to std's [`Path`].
|
||||
pub trait NormalizePath {
|
||||
|
@ -30,28 +27,19 @@ pub trait NormalizePath {
|
|||
/// All redundant separator and up-level references are collapsed.
|
||||
///
|
||||
/// However, this does not resolve links.
|
||||
fn normalize(&self) -> Cow<'_, Path>;
|
||||
}
|
||||
fn normalize(&self) -> PathBuf;
|
||||
|
||||
fn is_normalized(path: &Path) -> bool {
|
||||
for component in path.components() {
|
||||
match component {
|
||||
Component::CurDir | Component::ParentDir => {
|
||||
return false;
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
/// Return `true` if the path is normalized.
|
||||
///
|
||||
/// # Quirk
|
||||
///
|
||||
/// If the path does not start with `./` but contains `./` in the middle,
|
||||
/// then this function might returns `true`.
|
||||
fn is_normalized(&self) -> bool;
|
||||
}
|
||||
|
||||
impl NormalizePath for Path {
|
||||
fn normalize(&self) -> Cow<'_, Path> {
|
||||
if is_normalized(self) {
|
||||
return Cow::Borrowed(self);
|
||||
}
|
||||
|
||||
fn normalize(&self) -> PathBuf {
|
||||
let mut components = self.components().peekable();
|
||||
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek() {
|
||||
let buf = PathBuf::from(c.as_os_str());
|
||||
|
@ -76,6 +64,20 @@ impl NormalizePath for Path {
|
|||
}
|
||||
}
|
||||
}
|
||||
Cow::Owned(ret)
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
fn is_normalized(&self) -> bool {
|
||||
for component in self.components() {
|
||||
match component {
|
||||
Component::CurDir | Component::ParentDir => {
|
||||
return false;
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue