templates.rs

 1use crate::PreprocessorContext;
 2use regex::Regex;
 3use std::collections::HashMap;
 4
 5mod action;
 6mod keybinding;
 7
 8pub use action::*;
 9pub use keybinding::*;
10
11pub trait Template {
12    fn key(&self) -> &'static str;
13    fn regex(&self) -> Regex;
14    fn parse_args(&self, args: &str) -> HashMap<String, String>;
15    fn render(&self, context: &PreprocessorContext, args: &HashMap<String, String>) -> String;
16
17    fn process(&self, context: &PreprocessorContext, content: &str) -> String {
18        self.regex()
19            .replace_all(content, |caps: &regex::Captures| {
20                let args = self.parse_args(&caps[1]);
21                self.render(context, &args)
22            })
23            .into_owned()
24    }
25}