Expand description
Shell-like variable substitution for strings and byte strings.
Features
- Perform substitution in
&str
or in&[u8]
. - Provide a custom map of variables or use environment variables.
- Short format:
"Hello $name!"
- Long format:
"Hello ${name}!"
- Default values:
"Hello ${name:person}!"
- Recursive substitution in default values:
"${XDG_CONFIG_HOME:$HOME/.config}/my-app/config.toml"
- Perform substitution on all string values in YAML data (optional, requires the
yaml
feature).
Variable names can consist of alphanumeric characters and underscores. They are allowed to start with numbers.
Examples
The substitute()
function can be used to perform substitution on a &str
.
The variables can either be a HashMap
or a BTreeMap
.
let mut variables = HashMap::new();
variables.insert("name", "world");
assert_eq!(subst::substitute("Hello $name!", &variables)?, "Hello world!");
The variables can also be taken directly from the environment with the Env
map.
assert_eq!(
subst::substitute("$XDG_CONFIG_HOME/my-app/config.toml", &subst::Env)?,
"/home/user/.config/my-app/config.toml",
);
Substitution can also be done on byte strings using the substitute_bytes()
function.
let mut variables = HashMap::new();
variables.insert("name", b"world");
assert_eq!(subst::substitute_bytes(b"Hello $name!", &variables)?, b"Hello world!");
Re-exports
pub use error::Error;
Modules
- Module containing error details.
- Support for variable substitution in YAML data.
Structs
- A map that gives strings from the environment.
- A map that gives byte strings from the environment.
- A “map” that never returns any values.
Enums
- Value returned by the
NoSubstitution
map.
Traits
- Trait for types that can be used as a variable map.
Functions
- Substitute variables in a string.
- Substitute variables in a byte string.