Crate normpath[−][src]
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
)
Features
These features are optional and can be enabled or disabled in a “Cargo.toml” file.
Optional Features
-
print_bytes - Provides implementations of [
print_bytes::ToBytes
] for types defined by this crate. -
serde - Provides implementations of [
serde::Deserialize
] and [serde::Serialize
] for types defined by this crate.
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
An owned BasePath
.