5 个版本 (2 个稳定版)
1.0.1 | 2024年5月18日 |
---|---|
0.1.4 | 2023年3月12日 |
539 在 算法 中
每月39次 下载
8KB
83 行
SVI
字符串变量插入器
此包包含一个函数,该函数将 hashmap 中的变量插入到输入字符串中,生成结果字符串,以及一个 'replacers' 向量,以确保输出安全存储或打印,而不会泄露秘密。
变量可以与以下两种方式匹配:[[variable_name]]
(双括号)或 {{variable_name}}
(双花括号)。
使用方法
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:[email protected]: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'
转义插入
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]]'
依赖关系
~0.4–0.8MB
~19K SLoC