plenum-bot/src/template.rs

21 lines
704 B
Rust
Raw Normal View History

2024-08-05 16:45:05 +02:00
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(),
}
}
}
}