Expand description
This crate provides methods to normalize paths in the recommended way for the operating system.
It was made to fix a recurring bug caused by using fs::canonicalize
on
Windows: #45067, #48249, #52440, #55812, #58613, #59107,
#74327. Normalization is usually a better choice unless you specifically
need a canonical path.
Using these replacement methods will usually fix those issues, but see their documentation for more information:
PathExt::normalize
(usually replacesPath::canonicalize
)BasePath::join
(replacesPath::join
)BasePath::parent
(replacesPath::parent
)BasePathBuf::pop
(replacesPathBuf::pop
)BasePathBuf::push
(replacesPathBuf::push
)
Additionally, these methods can be used for other enhancements:
Features
These features are optional and can be enabled or disabled in a “Cargo.toml” file.
Optional Features
-
localization - Provides
PathExt::localize_name
andBasePath::localize_name
. -
print_bytes - Provides implementations of
print_bytes::ToBytes
forBasePath
andBasePathBuf
. -
serde - Provides implementations of
serde::Deserialize
and/orserde::Serialize
forBasePath
andBasePathBuf
. -
uniquote - Provides implementations of
uniquote::Quote
forBasePath
andBasePathBuf
.
Implementation
All traits are sealed, meaning that they can only be implemented by this crate. Otherwise, backward compatibility would be more difficult to maintain for new features.
Sponsorship
If this crate has been useful for your project, let me know with a sponsorship! Sponsorships help me create and maintain my open source libraries, and they are always very appreciated.
Examples
use std::io;
use std::path::Path;
use normpath::BasePathBuf;
use normpath::PathExt;
fn find_target_dir(path: &Path) -> io::Result<Option<BasePathBuf>> {
let mut path = path.normalize()?;
while !path.ends_with("target") {
match path.pop() {
Ok(true) => continue,
Ok(false) => {}
Err(_) => {
eprintln!("Some components could not be normalized.");
}
}
return Ok(None);
}
Ok(Some(path))
}
Modules
- The error types defined by this crate.
Structs
Traits
- Additional methods added to
Path
.