#format-string #string-interpolation #variables #hash-map #safe #output #resulting

svi

a function to interpolate variables in a hashmap into a format string

5 releases (2 stable)

1.0.1 May 18, 2024
0.1.4 Mar 12, 2023

#798 in Algorithms

Download history 32/week @ 2024-07-28 36/week @ 2024-08-04 67/week @ 2024-08-11 36/week @ 2024-08-18 26/week @ 2024-08-25 10/week @ 2024-09-01 37/week @ 2024-09-08 6/week @ 2024-09-15 47/week @ 2024-09-22 91/week @ 2024-09-29 68/week @ 2024-10-06 57/week @ 2024-10-13 49/week @ 2024-10-20 37/week @ 2024-10-27 30/week @ 2024-11-03

179 downloads per month

MIT license

8KB
83 lines

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]]'

Dependencies

~240–700KB
~16K SLoC