pub struct CfgPathResolver { /* private fields */ }
Expand description
A variable resolver for paths in a configuration file.
Typically there should be one resolver per application, and the application should share the
resolver throughout the application to have consistent path variable expansions. Typically the
application would create its own resolver with its application-specific variables, but note that
TorClientConfig
is an exception which does not accept a resolver from the application and
instead generates its own. This is done for backwards compatibility reasons.
Once constructed, they are used during calls to CfgPath::path
to expand variables in the
path.
Implementations§
Source§impl CfgPathResolver
impl CfgPathResolver
Sourcepub fn set_var(
&mut self,
var: impl Into<String>,
val: Result<Cow<'static, Path>, CfgPathError>,
)
pub fn set_var( &mut self, var: impl Into<String>, val: Result<Cow<'static, Path>, CfgPathError>, )
Set a variable var
that will be replaced with val
when a CfgPath
is expanded.
Setting an Err
is useful when a variable is supported, but for whatever reason it can’t be
expanded, and you’d like to return a more-specific error. An example might be a USER_HOME
variable for a user that doesn’t have a HOME
environment variable set.
use std::path::Path;
use tor_config_path::{CfgPath, CfgPathResolver};
let mut path_resolver = CfgPathResolver::default();
path_resolver.set_var("FOO", Ok(Path::new("/foo").to_owned().into()));
let path = CfgPath::new("${FOO}/bar".into());
#[cfg(feature = "expand-paths")]
assert_eq!(path.path(&path_resolver).unwrap(), Path::new("/foo/bar"));
#[cfg(not(feature = "expand-paths"))]
assert!(path.path(&path_resolver).is_err());
Trait Implementations§
Source§impl Clone for CfgPathResolver
impl Clone for CfgPathResolver
Source§fn clone(&self) -> CfgPathResolver
fn clone(&self) -> CfgPathResolver
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more