1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use std::{
collections::{HashMap, HashSet},
io,
};
use serde::Deserialize;
use thiserror::Error;
use crate::{
rules::{self, CompiledRule},
template, Rule, Templates,
};
#[derive(Error, Debug)]
pub enum Error {
#[error("duplicate rule={0}")]
DuplicateRule(String),
#[error("unknown rule dependency in rule={0}")]
UnknownRuleDependency(String),
#[error("rule error: {0}")]
Rule(#[from] rules::Error),
#[error("template: error {0}")]
Template(#[from] template::Error),
#[error("yaml error: {0}")]
Serde(#[from] serde_yaml::Error),
}
/// Rule compiler
#[derive(Default, Clone)]
pub struct Compiler {
templates: Templates,
names: HashMap<String, usize>,
loaded: HashSet<String>,
rules: Vec<Rule>,
pub(crate) compiled: Vec<CompiledRule>,
}
impl Compiler {
/// Creates a new `Compiler`
pub fn new() -> Self {
Self::default()
}
/// Loads templates from a reader implementing [io::Read] trait. The data within the
/// reader must a `HashMap<String, String>` YAML formatted.
#[inline]
pub fn load_templates_from_reader<R: io::Read>(&mut self, r: R) -> Result<(), Error> {
for document in serde_yaml::Deserializer::from_reader(r) {
self.load_templates(Templates::deserialize(document)?)?;
}
Ok(())
}
/// Wrapper around [Compiler::load_templates_from_reader] loading a rules
/// from a struct implementing [`AsRef<str>`]
pub fn load_templates_from_str<S: AsRef<str>>(&mut self, s: S) -> Result<(), Error> {
let c = io::Cursor::new(s.as_ref());
self.load_templates_from_reader(c)
}
/// Loads a set of string [`Templates`] into the compiler so that it
/// can replace the appropriate strings into the rules before compiling them
pub fn load_templates(&mut self, t: Templates) -> Result<(), Error> {
self.templates.extend(&t)?;
Ok(())
}
/// Load a rule from a reader implementing [io::Read] trait. The data must be formatted
/// in YAML following the YAML documents format otherwise this function will fail.
#[inline]
pub fn load_rules_from_reader<R: io::Read>(&mut self, r: R) -> Result<(), Error> {
for document in serde_yaml::Deserializer::from_reader(r) {
self.load(Rule::deserialize(document)?)?;
}
Ok(())
}
/// Wrapper around [Compiler::load_rules_from_reader] loading a rules
/// from a struct implementing [`AsRef<str>`]
pub fn load_rules_from_str<S: AsRef<str>>(&mut self, s: S) -> Result<(), Error> {
let c = io::Cursor::new(s.as_ref());
self.load_rules_from_reader(c)
}
/// Load a rule into the `Compiler`.
#[inline]
pub fn load(&mut self, mut r: Rule) -> Result<(), Error> {
if r.is_disabled() {
return Ok(());
}
if self.loaded.contains(&r.name) {
return Err(Error::DuplicateRule(r.name));
}
// we replace template strings used in rule
self.templates.replace(&mut r);
self.loaded.insert(r.name.clone());
self.rules.push(r);
Ok(())
}
/// Compile all the [`Rule`] loaded via [Compiler::load] which
/// have not been compiled yet.
#[inline]
pub fn compile(&mut self) -> Result<(), Error> {
// no need to do the job again
if self.is_ready() {
return Ok(());
}
// we must compile in the order of insertion to check
// for dependencies
for (i, r) in self.rules.iter().enumerate() {
// we do not re-compile rules already compiled
if self.names.contains_key(&r.name) {
continue;
}
let compiled: CompiledRule = r.clone().try_into()?;
// We verify that all rules we depend on are known.
// The fact that rule dependencies must be known makes
// circular references impossible
for dep in compiled.depends.iter() {
self.names
.get(dep)
.ok_or(Error::UnknownRuleDependency(dep.clone()))?;
}
// we need to be sure nothing can fail beyond this point not
// to create inconsistencies in compiled and sources members
// this is the index the rule is going to be inserted at
self.names.insert(compiled.name.clone(), i);
self.compiled.push(compiled);
}
Ok(())
}
/// Returns whether compiler is ready (i.e. all the rules have been compiled)
#[inline(always)]
fn is_ready(&self) -> bool {
self.rules.len() == self.compiled.len()
}
/// Retrieves the rules loaded in the compiler after all of them have been checked
/// against potential compilation errors.
pub fn rules(&mut self) -> Result<&Vec<Rule>, Error> {
// we need to re-compile rules as some are missing
if !self.is_ready() {
self.compile()?;
}
Ok(&self.rules)
}
/// Retrieves all compiled rules
pub fn compiled(&mut self) -> Result<&Vec<CompiledRule>, Error> {
// we need to re-compile rules as some are missing
if !self.is_ready() {
self.compile()?;
}
Ok(&self.compiled)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_load_from_str() {
let mut c = Compiler::new();
c.load_rules_from_str(
r#"
name: test
"#,
)
.unwrap();
assert_eq!(c.rules.len(), 1);
}
#[test]
fn test_load_duplicate_rule() {
let mut c = Compiler::new();
let res = c.load_rules_from_str(
r#"
---
name: test
---
name: test
"#,
);
assert!(matches!(res, Err(Error::DuplicateRule(_))));
}
#[test]
fn test_load_rule_unk_dep() {
let mut c = Compiler::new();
c.load_rules_from_str(
r#"
name: test
matches:
$d: rule(unknown.dep)
condition: any of them
"#,
)
.unwrap();
// Unknown RuleDependency is checked at compile time
assert!(matches!(c.compile(), Err(Error::UnknownRuleDependency(_))));
}
#[test]
fn test_load_templates() {
let mut c = Compiler::new();
c.load_templates_from_str(
r#"
crazy_re: '(this|is|some|re|template)'
str_template: hello world template
"#,
)
.unwrap();
assert_eq!(c.templates.len(), 2)
}
#[test]
fn test_load_dup_templates() {
let mut c = Compiler::new();
let res = c.load_templates_from_str(
r#"
str_template: hello world template
str_template: duplicate
"#,
);
// when the duplicate is within the same file this is going
// to be an error raised by the deserialize that doesn't allow it
assert!(matches!(res, Err(Error::Serde(_))));
let res = c.load_templates_from_str(
r#"
str_template: hello world template
---
str_template: duplicate
"#,
);
// when the duplicate is in different yaml documents it
// should raise a template error
assert!(matches!(res, Err(Error::Template(_))));
}
#[test]
fn test_templated_rule() {
let mut c = Compiler::new();
c.load_templates_from_str(
r#"
tpl_string: hello world template
"#,
)
.unwrap();
c.load_rules_from_str(
r#"
name: test
matches:
$m: .data.path == '{{tpl_string}}'
"#,
)
.unwrap();
assert_eq!(
c.rules()
.unwrap()
.first()
.unwrap()
.matches
.as_ref()
.unwrap()
.get("$m")
.unwrap(),
&String::from(".data.path == 'hello world template'")
);
}
#[test]
fn test_rules_order() {
let mut c = Compiler::new();
for i in 0..1000 {
c.load_rules_from_str(format!("name: rule.{i}")).unwrap()
}
c.compile().unwrap();
for i in 0..1000 {
assert_eq!(c.rules[i].name, c.compiled[i].name);
}
}
}