Function shellexpand::env_with_context_no_errors
source · [−]pub fn env_with_context_no_errors<SI: ?Sized, CO, C>(
input: &SI,
context: C
) -> Cow<'_, str> where
SI: AsRef<str>,
CO: AsRef<str>,
C: FnMut(&str) -> Option<CO>,
Expand description
Same as env_with_context()
, but forbids the variable lookup function to return errors.
This function also performs environment expansion, but it requires context function of type
FnMut(&str) -> Option<CO>
instead of FnMut(&str) -> Result<Option<CO>, E>
. This simplifies
the API when you know in advance that the context lookups may not fail.
Because of the above, instead of Result<Cow<str>, LookupError<E>>
this function returns just
Cow<str>
.
Note that if the context function returns None
, the behavior remains the same as that of
env_with_context()
: the variable reference will remain in the output string unexpanded.
Examples
fn context(s: &str) -> Option<&'static str> {
match s {
"A" => Some("a value"),
"B" => Some("b value"),
_ => None
}
}
// Known variables are expanded
assert_eq!(
shellexpand::env_with_context_no_errors("begin/$A/${B}s/end", context),
"begin/a value/b values/end"
);
// Unknown variables are left as is
assert_eq!(
shellexpand::env_with_context_no_errors("begin/$U/end", context),
"begin/$U/end"
);