templating code

This commit is contained in:
nobody 2024-08-05 16:45:05 +02:00 committed by murmeldin
parent 15fcb18422
commit 8f09feb0fc
2 changed files with 21 additions and 0 deletions

View file

@ -37,6 +37,7 @@ mod key_value;
use key_value::KeyValueStore as KV; use key_value::KeyValueStore as KV;
mod config_spec; mod config_spec;
use config_spec::{CfgField, CfgGroup, CfgSpec}; use config_spec::{CfgField, CfgGroup, CfgSpec};
mod template;
pub mod variables_and_settings; pub mod variables_and_settings;

20
src/template.rs Normal file
View file

@ -0,0 +1,20 @@
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(),
}
}
}
}