pub trait PathExtension {
// Required methods
fn file_name_str(&self) -> Result<&str>;
fn ensure_exists(&self) -> Result<()>;
fn simplified(&self) -> &Path;
fn normalize(&self) -> Result<PathBuf>;
fn normalize_executable(&self) -> Result<PathBuf>;
fn parent_abs_times(&self, x: usize) -> Result<PathBuf>;
fn try_to_rel_from(&self, path: impl AsRef<Path>) -> Cow<'_, Path>;
fn command(&self) -> CommandBuilder;
// Provided methods
fn normalize_exists(&self) -> Result<PathBuf> { ... }
fn parent_abs(&self) -> Result<PathBuf> { ... }
fn try_to_rel(&self) -> Cow<'_, Path> { ... }
}fs only.Expand description
§File System Paths and Strings
Rust works with Strings, which are UTF-8 encoded bytes.
However, not all operating systems work with UTF-8. That’s why Rust has
OsString, which has platform-specific implementations.
And PathBufs are wrappers for OsString.
However, often when writing platform-independent code, we want to stay
in the UTF-8 realm, but conversion can be painful because you must handle
the error when the OsString is not valid UTF-8.
use std::ffi::{OsString, OsStr};
use cu::pre::*;
fn take_os_string(s: &OsStr) -> cu::Result<()> {
match s.to_str() {
Some(s) => {
cu::info!("valid utf-8: {s}");
Ok(())
}
None => {
cu::bail!("not valid utf-8!");
}
}
}cu provides extension traits that integrates with cu::Result,
so you can have the error handling by simply propagate with ?.
There are 4 traits, all will be included into scope with use cu::pre::*;.
The path extensions also have utilities for working with file system specifically,
(such as normalizing it), which is why they require the fs feature.
OsStrExtensionOsStrExtensionOwnedPathExtension- requiresfsfeature
Required Methods§
Sourcefn file_name_str(&self) -> Result<&str>
fn file_name_str(&self) -> Result<&str>
Get file name. Error if the file name is not UTF-8 or other error occurs
Sourcefn ensure_exists(&self) -> Result<()>
fn ensure_exists(&self) -> Result<()>
Check that the path exists, or fail with an error
Sourcefn simplified(&self) -> &Path
fn simplified(&self) -> &Path
Return the simplified path if the path has a Windows UNC prefix
The behavior is the same cross-platform.
Sourcefn normalize(&self) -> Result<PathBuf>
fn normalize(&self) -> Result<PathBuf>
Get absolute path for a path.
This is different from canonicalize() since that fails for paths that don’t
exist. Here, we do a fallback that normalizes the path manually if canonicalize()
fails:
- If the path is absolute, then empty,
., and..path segments are normalized and removed, and error if it tries to get the parent of root at some point. - If the path is relative, then the path is normalized the same way as above, and then appended to the absolute path of the current working directory.
On Windows only, it returns the most compatible form using the dunce crate instead of
UNC, and the drive letter is also normalized to upper case
Sourcefn normalize_executable(&self) -> Result<PathBuf>
fn normalize_executable(&self) -> Result<PathBuf>
Like normalize, but with the additional guarantee that:
- The file name of the output will be the same as the input. This is because the executable can be a multicall binary that behaves differently depending on the executable name.
- The path exists and is not a directory
Sourcefn parent_abs_times(&self, x: usize) -> Result<PathBuf>
fn parent_abs_times(&self, x: usize) -> Result<PathBuf>
Effecitvely chaining parent_abs x times
Sourcefn try_to_rel_from(&self, path: impl AsRef<Path>) -> Cow<'_, Path>
fn try_to_rel_from(&self, path: impl AsRef<Path>) -> Cow<'_, Path>
Try converting self to a relative path from a base path
Sourcefn command(&self) -> CommandBuilder
Available on crate feature process only.
fn command(&self) -> CommandBuilder
process only.Start building a child process with the path as the executable
See Spawn Commands
Provided Methods§
Sourcefn normalize_exists(&self) -> Result<PathBuf>
fn normalize_exists(&self) -> Result<PathBuf>
Like normalize, but with the additional guarantee that the path exists
Sourcefn parent_abs(&self) -> Result<PathBuf>
fn parent_abs(&self) -> Result<PathBuf>
Get the parent path as an absolute path
Path navigation is very complex and that’s why we are paying a little performance
cost and always returning PathBuf, and always converting the path to absolute.
For path manipulation (i.e. as a OsStr), instead of navigation, use std parent()
instead
Sourcefn try_to_rel(&self) -> Cow<'_, Path>
fn try_to_rel(&self) -> Cow<'_, Path>
Try converting self to a relative path from current working directory.
Return the path itself unmodified if conversion failed
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl PathExtension for Path
impl PathExtension for Path
fn file_name_str(&self) -> Result<&str>
fn simplified(&self) -> &Path
fn ensure_exists(&self) -> Result<()>
fn normalize(&self) -> Result<PathBuf>
fn normalize_executable(&self) -> Result<PathBuf>
fn parent_abs_times(&self, x: usize) -> Result<PathBuf>
fn try_to_rel_from(&self, path: impl AsRef<Path>) -> Cow<'_, Path>
Source§fn command(&self) -> CommandBuilder
fn command(&self) -> CommandBuilder
process only.Source§impl PathExtension for PathBuf
impl PathExtension for PathBuf
fn file_name_str(&self) -> Result<&str>
fn simplified(&self) -> &Path
fn normalize(&self) -> Result<PathBuf>
fn normalize_executable(&self) -> Result<PathBuf>
fn ensure_exists(&self) -> Result<()>
fn parent_abs_times(&self, x: usize) -> Result<PathBuf>
fn try_to_rel_from(&self, path: impl AsRef<Path>) -> Cow<'_, Path>
Source§fn command(&self) -> CommandBuilder
fn command(&self) -> CommandBuilder
process only.