Expand description
Variables substitution in string templates.
This library provide helper functions for string manipulation, taking values from a context environment map and substituting all matching placeholders.
Its name and logic is similar to the envsubst
GNU utility, but
this only supports braces-delimited variables (i.e. ${foo}
) and
takes replacement values from an explicit map of variables.
§Example
let base_url = "${protocol}://${hostname}/${endpoint}";
assert!(envsubst::is_templated(base_url));
let mut context = std::collections::HashMap::new();
context.insert("protocol".to_string(), "https".to_string());
context.insert("hostname".to_string(), "example.com".to_string());
context.insert("endpoint".to_string(), "login".to_string());
assert!(envsubst::validate_vars(&context).is_ok());
let final_url = envsubst::substitute(base_url, &context).unwrap();
assert!(!envsubst::is_templated(&final_url));
assert_eq!(final_url, "https://example.com/login");
Structs§
- Error
- Library errors.
Functions§
- is_
templated - Check whether input string contains templated variables.
- substitute
- Substitute variables in a template string.
- validate_
vars - Validate variables for substitution.