pub trait PathResolveExt {
// Required method
fn try_resolve_in<P: AsRef<Path>>(
&self,
base: P,
) -> Result<Cow<'_, Path>, Error>;
// Provided methods
fn resolve(&self) -> Cow<'_, Path> { ... }
fn try_resolve(&self) -> Result<Cow<'_, Path>, Error> { ... }
fn resolve_in<P: AsRef<Path>>(&self, base: P) -> Cow<'_, Path> { ... }
}
Expand description
Extension trait for resolving paths against a base path.
§Example
use std::path::Path;
use resolve_path::PathResolveExt as _;
assert_eq!(Path::new("./config.yml").resolve_in("/home/user/.app"), Path::new("/home/user/.app/config.yml"));
Required Methods§
Provided Methods§
Sourcefn resolve(&self) -> Cow<'_, Path>
fn resolve(&self) -> Cow<'_, Path>
Resolves the path in the process’s current directory
§Example
use std::path::Path;
use resolve_path::PathResolveExt;
std::env::set_current_dir("/home/user/.config/alacritty").unwrap();
let resolved = "./alacritty.yml".resolve();
assert_eq!(resolved, Path::new("/home/user/.config/alacritty"));
§Panics
This function panics if:
- It is unable to detect the current working directory
- It is unable to resolve the home directory for a tilde (
~
)
See try_resolve
for a non-panicking API.
Sourcefn try_resolve(&self) -> Result<Cow<'_, Path>, Error>
fn try_resolve(&self) -> Result<Cow<'_, Path>, Error>
Attempts to resolve the path in the process’s current directory
Returns an error if:
- It is unable to detect the current working directory
- It is unable to resolve the home directory for a tilde (
~
)
Sourcefn resolve_in<P: AsRef<Path>>(&self, base: P) -> Cow<'_, Path>
fn resolve_in<P: AsRef<Path>>(&self, base: P) -> Cow<'_, Path>
Resolves this path against a given base path.
§Example
use std::path::{Path, PathBuf};
use resolve_path::PathResolveExt as _;
assert_eq!("./config.yml".resolve_in("/home/user/.app"), Path::new("/home/user/.app/config.yml"));
assert_eq!(String::from("./config.yml").resolve_in("/home/user/.app"), Path::new("/home/user/.app/config.yml"));
assert_eq!(Path::new("./config.yml").resolve_in("/home/user/.app"), Path::new("/home/user/.app/config.yml"));
assert_eq!(PathBuf::from("./config.yml").resolve_in("/home/user/.app"), Path::new("/home/user/.app/config.yml"));
§Panics
Panics if we attempt to resolve a ~
in either path and are
unable to determine the home directory from the environment
(using the dirs
crate). See try_resolve_in
for a non-panicking option.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.