pub struct Templates(/* private fields */);Expand description
Structure holding string templates to replace in rules. Templating mechanism allow to define once complex regex and use them at multiple places in rules, making rule maintenance easier.
§Example
use gene::Compiler;
let mut c = Compiler::new();
/// loading template from string
c.load_templates_from_str(
r#"
some_template: hello world
"#,
)
.unwrap();
c.load_rules_from_str(
r#"
name: test
matches:
$m: .data.path == '{{some_template}}'
"#,
).unwrap();
/// we verify our template has been replaced
assert_eq!(
c.rules()
.unwrap()
.first()
.unwrap()
.matches
.as_ref()
.unwrap()
.get("$m")
.unwrap(),
&String::from(".data.path == 'hello world'")
);Implementations§
Source§impl Templates
impl Templates
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty template collection.
Returns a Templates instance with no template variables defined.
This is equivalent to calling Templates::default().
Sourcepub fn insert(&mut self, name: String, template: String) -> Result<(), Error>
pub fn insert(&mut self, name: String, template: String) -> Result<(), Error>
Inserts a new template variable into this collection.
Adds a name-template pair that can be used for substitution in rule match
expressions. Template placeholders in the format {{name}} will be replaced
with the provided template value when the rule is processed.
§Errors
Returns Error::Duplicate if a template with the same name already exists
in this collection. This prevents accidental overwrites of existing templates.
Sourcepub fn extend(&mut self, o: &Self) -> Result<(), Error>
pub fn extend(&mut self, o: &Self) -> Result<(), Error>
Extends this template collection with templates from another collection.
Merges all templates from the provided collection into this one. If any template names conflict, an error is returned and no templates are added.
This method is useful for composing template collections from multiple sources or applying base templates with overrides.
§Errors
Returns Error::Duplicate if any template name in the source collection
already exists in this collection. The operation is atomic - if any
duplicate is found, no templates are added.
Sourcepub fn replace(&self, r: &mut Rule)
pub fn replace(&self, r: &mut Rule)
Replaces template placeholders in a rule’s match expressions.
Iterates through all match expressions in the rule and replaces any template
placeholders (in the format {{name}}) with their corresponding values from
this collection. This allows for dynamic rule configuration through templates.
§Behavior
- Only affects the
matchessection of the rule - Placeholders are replaced in-place in the match expressions
- If a template name is not found, the placeholder remains unchanged
- Multiple occurrences of the same template are all replaced
§Examples
use gene::Templates;
use gene::rules::Rule;
let mut rule: Rule = serde_yaml::from_str(
"name: test\nmatches:\n $a: .path == \"{{path}}\"\ncondition: $a"
).unwrap();
let mut templates = Templates::new();
templates.insert("path".to_string(), "/var/log/".to_string()).unwrap();
templates.replace(&mut rule);
// rule.matches now contains ".path == \"/var/log/\""