Function shellexpand::full_with_context_no_errors
source · pub fn full_with_context_no_errors<SI, CO, C, P, HD>(
input: &SI,
home_dir: HD,
context: C,
) -> Cow<'_, str>
Expand description
Same as full_with_context()
, but forbids the variable lookup function to return errors.
This function also performs full shell-like expansion, but it uses
env_with_context_no_errors()
for environment expansion whose context lookup function returns
just Option<CO>
instead of Result<Option<CO>, E>
. Therefore, the function itself also
returns just Cow<str>
instead of Result<Cow<str>, LookupError<E>>
. Otherwise it is
identical to full_with_context()
.
§Examples
use std::path::{PathBuf, Path};
use std::borrow::Cow;
fn home_dir() -> Option<PathBuf> { Some(Path::new("/home/user").into()) }
fn get_env(name: &str) -> Option<&'static str> {
match name {
"A" => Some("a value"),
"B" => Some("b value"),
"T" => Some("~"),
_ => None
}
}
// Performs both tilde and environment expansions
assert_eq!(
shellexpand::full_with_context_no_errors("~/$A/$B", home_dir, get_env),
"/home/user/a value/b value"
);
// Input without starting tilde and without variables does not cause allocations
let s = shellexpand::full_with_context_no_errors("some/path", home_dir, get_env);
match s {
Cow::Borrowed(s) => assert_eq!(s, "some/path"),
_ => unreachable!("the above variant is always valid")
}
// Input with a tilde inside a variable in the beginning of the string does not cause tilde
// expansion
assert_eq!(
shellexpand::full_with_context_no_errors("$T/$A/$B", home_dir, get_env),
"~/a value/b value"
);