shellexpand

Function full

source
pub fn full<SI>(input: &SI) -> Result<Cow<'_, str>, LookupError<VarError>>
where SI: AsRef<str> + ?Sized,
Expand description

Performs both tilde and environment expansions in the default system context.

This function delegates to full_with_context(), using the default system sources for both home directory and environment, namely dirs::home_dir() and std::env::var().

Note that variable lookup of unknown variables will fail with an error instead of, for example, replacing the unknown variable with an empty string. The author thinks that this behavior is more useful than the other ones. If you need to change it, use full_with_context() or full_with_context_no_errors() with an appropriate context function instead.

This function behaves exactly like full_with_context() in regard to tilde-containing variables in the beginning of the input string.

ยงExamples

use std::env;

env::set_var("A", "a value");
env::set_var("B", "b value");

let home_dir = dirs::home_dir()
    .map(|p| p.display().to_string())
    .unwrap_or_else(|| "~".to_owned());

// Performs both tilde and environment expansions using the system contexts
assert_eq!(
    shellexpand::full("~/$A/${B}s").unwrap(),
    format!("{}/a value/b values", home_dir)
);

// Unknown variables cause expansion errors
assert_eq!(
    shellexpand::full("~/$UNKNOWN/$B"),
    Err(shellexpand::LookupError {
        var_name: "UNKNOWN".into(),
        cause: env::VarError::NotPresent
    })
);