Function ergo_fs::expand [] [src]

pub fn expand<SI: ?Sized>(input: &SI) -> Result<Cow<str>, ExpandError> where
    SI: AsRef<str>, 

Performs both tilde and environment shell expansions in the default system context. This is the same as shellexpand::full and is the "typical use case" for expanding strings.

Note that non-existant variables will result in an Err (in sh they are silently replaced with an empty string). Also, environment lookup is only done as needed so this function is performant on strings that do not expand.

For more options and information, see the exported shellexpand.

Examples

use std::env;
use ergo_fs::*;

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

let home_dir = env::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!(
    expand("~/$A/${B}s")?,
    format!("{}/a value/b values", home_dir)
);

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