plenum-bot/src/template.rs
2024-12-10 21:37:57 +01:00

26 lines
709 B
Rust

use regex::{Captures, Regex};
pub fn replace<F>(text: &str, replacer: F) -> String
where
F: Fn(&Captures) -> String,
{
let regex = Regex::new(r"\{\{([\w_-]+)\}\}").unwrap();
regex.replace_all(text, replacer).into_owned()
}
pub fn config_replacer<'a>(
config: &'a crate::KV, blacklist_substrings: &'a [&'a str],
) -> impl Fn(&Captures) -> String + 'a {
move |caps: &Captures| {
let key = &caps[1];
if blacklist_substrings.iter().any(|&substr| key.contains(substr)) {
caps[0].to_string()
} else {
match config.get(key).ok() {
Some(value) => value,
None => caps[0].to_string(),
}
}
}
}