svi

Crates.iosvi
lib.rssvi
version1.0.1
sourcesrc
created_at2023-03-11 22:35:49.074585
updated_at2024-05-18 22:45:45.581063
descriptiona function to interpolate variables in a hashmap into a format string
homepage
repositoryhttps://github.com/mbecker20/svi
max_upload_size
id807582
size8,434
(MoghTech)

documentation

README

SVI

string variable interpolator

this crate contains a function that interpolates variables in a hashmap into an input string, producing both the resulting string, plus a vec of 'replacers' to make output safe to store or print without compromising secrets.

variables can be matched with either [[variable_name]] (DoubleBrackets) or {{variable_name}} (DoubleCurlyBrackets).

Usage

let variables: HashMap<String, String> = [
	("mongo_username", "root"),
	("mongo_password", "mng233985725"),
]
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();

let to_fmt = "mongodb://[[mongo_username]]:[[mongo_password]]@127.0.0.1:27017";

let (formatted, replacers) = svi::interpolate_variables(to_fmt, &variables, svi::Interpolator::DoubleBrackets)?;

println!("{formatted}"); // prints 'mongodb://root:mng233985725@127.0.0.1:27017'

// makes output involving replaced variables safe to print / store
let readable = svi::replace_in_string(&formatted, &replacers);

println!("{readable}"); // prints 'mongodb://<mongo_username>:<mongo_password>@127.0.0.1:27017'

Escaping Interpolation

let variables = HashMap::new();

let to_fmt = "the interpolator will escape interpolation with 3 openers: [[[not a variable]]]";

let (formatted, _) = svi::interpolate_variables(to_fmt, &variables, svi::Interpolator::DoubleBrackets)?;

println!("{formatted}"); // prints 'the interpolator will escape interpolation with 3 openers: [[not a variable]]'
Commit count: 12

cargo fmt