plenum-bot/src/template.rs

26 lines
709 B
Rust
Raw Normal View History

2024-08-05 16:45:05 +02:00
use regex::{Captures, Regex};
2024-08-06 18:05:26 +02:00
pub fn replace<F>(text: &str, replacer: F) -> String
where
F: Fn(&Captures) -> String,
{
let regex = Regex::new(r"\{\{([\w_-]+)\}\}").unwrap();
2024-08-05 16:45:05 +02:00
regex.replace_all(text, replacer).into_owned()
}
2024-08-06 18:05:26 +02:00
pub fn config_replacer<'a>(
config: &'a crate::KV, blacklist_substrings: &'a [&'a str],
) -> impl Fn(&Captures) -> String + 'a {
2024-08-05 16:45:05 +02:00
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(),
}
}
}
}