diff --git a/src/main.rs b/src/main.rs index bc55e94..aa402ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,7 @@ mod key_value; use key_value::KeyValueStore as KV; mod config_spec; use config_spec::{CfgField, CfgGroup, CfgSpec}; +mod template; pub mod variables_and_settings; diff --git a/src/template.rs b/src/template.rs new file mode 100644 index 0000000..d41de22 --- /dev/null +++ b/src/template.rs @@ -0,0 +1,20 @@ +use regex::{Captures, Regex}; + +pub fn replace( 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(), + } + } + } +}