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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! `filter_ast` provides an AST representation of a boolean filter expression.

use std::{
    fmt, iter,
    ops::{BitAnd, BitOr, Not},
    slice,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub mod visit;

/// A leaf node in a filter expression. This compares an individual field's value to a
/// specific operand using some operation.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Clause<F, P, O> {
    // Do not reorder these fields; their order is important to the `Ord` trait
    field: F,
    operator: P,
    operand: O,
}

impl<F, P, O> Clause<F, P, O> {
    pub fn new(field: F, operator: P, operand: O) -> Self {
        Clause {
            field,
            operator,
            operand,
        }
    }

    /// Get the field against which this clause performs its comparison.
    pub fn field(&self) -> &F {
        &self.field
    }

    /// Get the operator used in this clause's comparison.
    pub fn operator(&self) -> &P {
        &self.operator
    }

    /// Get the operand for this clause's comparison.
    pub fn operand(&self) -> &O {
        &self.operand
    }

    pub fn as_tuple(&self) -> (&F, &P, &O) {
        (self.field(), self.operator(), self.operand())
    }

    pub fn into_tuple(self) -> (F, P, O) {
        (self.field, self.operator, self.operand)
    }
}

impl<F: fmt::Display, P: fmt::Display, O: fmt::Display> fmt::Display for Clause<F, P, O> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} {} {}", self.field, self.operator, self.operand)
    }
}

impl<F, P, O, Rhs> BitAnd<Rhs> for Clause<F, P, O>
where
    Expr<F, P, O>: BitAnd<Rhs, Output = Expr<F, P, O>>,
{
    type Output = Expr<F, P, O>;

    fn bitand(self, rhs: Rhs) -> Self::Output {
        Expr::from(self) & rhs
    }
}

impl<F, P, O, Rhs> BitOr<Rhs> for Clause<F, P, O>
where
    Expr<F, P, O>: BitOr<Rhs, Output = Expr<F, P, O>>,
{
    type Output = Expr<F, P, O>;

    fn bitor(self, rhs: Rhs) -> Self::Output {
        Expr::from(self) | rhs
    }
}

/// Invert the filter expression by wrapping the clause in a tree with `Not` as its operator.
impl<F, P, O> Not for Clause<F, P, O> {
    type Output = Expr<F, P, O>;

    fn not(self) -> Self::Output {
        !Expr::from(self)
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum Logic {
    /// All rules must match for the tree to match
    And,
    /// Any rule must match for the tree to match
    Or,
    /// No rule may match for the tree to match
    Not,
}

/// A compound node in a filter expression, consisting of a logical operator and a set of
/// child expressions.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Tree<T> {
    operator: Logic,
    rules: Vec<T>,
}

impl<T> Tree<T> {
    pub fn new(operator: Logic, rules: Vec<T>) -> Self {
        Self { operator, rules }
    }

    pub fn operator(&self) -> Logic {
        self.operator
    }

    pub fn rules(&self) -> &[T] {
        &self.rules
    }

    /// Create an instance by collecting the rules from an iterator.
    ///
    /// # Example
    /// ```rust
    /// # use filter_ast::{Clause, Tree, Logic};
    /// let rules = vec![
    ///     Clause::new("name", "=", "jim"),
    ///     Clause::new("age", ">", "10")
    /// ];
    ///
    /// let tree = Tree::from_iter(
    ///     Logic::And,
    ///     rules.into_iter().filter(|f| *f.field() != "age")
    /// );
    ///
    /// assert_eq!(1, tree.rules().len());
    /// ```
    pub fn from_iter(operator: Logic, rules: impl Iterator<Item = T>) -> Self {
        Tree::new(operator, rules.collect())
    }

    /// Create a new tree by applying a transform to all its rules.
    ///
    /// # Example
    /// ```rust
    /// # use filter_ast::{Tree, Logic};
    /// let tree = Tree::new(Logic::Or, vec![1, 2]);
    /// let tree2 = tree.map(|x| x * 10);
    /// assert_eq!(Tree::new(Logic::Or, vec![10, 20]), tree2);
    /// ```
    pub fn map<U, F>(self, transform: F) -> Tree<U>
    where
        F: Fn(T) -> U,
    {
        Tree::from_iter(self.operator(), self.rules.into_iter().map(transform))
    }
}

impl<T: Ord> Tree<T> {
    /// Sort the rules in the tree.
    pub fn sort(&mut self) {
        self.rules.sort()
    }
}

/// A filter expression.
///
/// Filter expressions are an abstract representation of a function `(val: T) -> bool`.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Expr<F, P, O> {
    /// A sub-tree in the filter expression.
    Tree(Tree<Expr<F, P, O>>),
    /// A leaf node in the filter expression.
    Clause(Clause<F, P, O>),
}

impl<F, P, O> Expr<F, P, O> {
    /// Create a new clause wrapped in an `Expr`.
    pub fn new_clause(field: F, operator: P, operand: O) -> Self {
        Clause::new(field, operator, operand).into()
    }

    pub fn is_tree(&self) -> bool {
        match self {
            Expr::Tree(_) => true,
            Expr::Clause(_) => false,
        }
    }

    pub fn is_clause(&self) -> bool {
        !self.is_tree()
    }

    pub fn as_tree(&self) -> Option<&Tree<Expr<F, P, O>>> {
        if let Expr::Tree(tree) = &self {
            Some(tree)
        } else {
            None
        }
    }

    pub fn as_clause(&self) -> Option<&Clause<F, P, O>> {
        if let Expr::Clause(clause) = &self {
            Some(clause)
        } else {
            None
        }
    }

    /// Apply a mapping function to each clause in the expression, creating a new expression.
    ///
    /// The mapping function is allowed to return a new tree; this enables expansion of one
    /// clause into a nested sub-tree.
    pub fn map<F2, P2, O2, TF>(self, transform: &TF) -> Expr<F2, P2, O2>
    where
        TF: Fn(Clause<F, P, O>) -> Expr<F2, P2, O2>,
    {
        match self {
            Expr::Clause(clause) => transform(clause),
            Expr::Tree(tree) => tree.map(|sub| sub.map(transform)).into(),
        }
    }

    /// Visit all clauses in depth-first order.
    ///
    /// For more advanced visiting, see [`visit::Visit`].
    pub fn clauses<'a>(&'a self) -> Clauses<'a, F, P, O> {
        Clauses {
            expr: self,
            stack: Vec::new(),
            has_visited_root: false,
        }
    }
}

impl<F, P, O> From<Clause<F, P, O>> for Expr<F, P, O> {
    fn from(v: Clause<F, P, O>) -> Self {
        Self::Clause(v)
    }
}

impl<F, P, O> From<Tree<Expr<F, P, O>>> for Expr<F, P, O> {
    fn from(v: Tree<Expr<F, P, O>>) -> Self {
        Self::Tree(v)
    }
}

/// Create a new expression representing the intersection of the provided expressions.
///
/// # Usage
/// ```rust
/// # use filter_ast::{Expr, Logic};
/// let a = Expr::new_clause("field", "=", "v1");
/// let b = Expr::new_clause("other", "=", "a");
/// let c = a & b;
/// let c_tree = c.as_tree().unwrap();
/// assert_eq!(c_tree.operator(), Logic::And);
/// assert_eq!(c_tree.rules()[0].as_clause().unwrap().field(), &"field");
/// assert_eq!(c_tree.rules()[1].as_clause().unwrap().field(), &"other");
/// ```
///
/// # Tree Simplification
/// This operation will try to avoid increasing tree depth by merging rules from input tree expressions
/// whose operator is `Logic::And`. For example, `(a & b) & (c & d & e)` will produce `a & b & c & d & e`.
impl<F, P, O> BitAnd for Expr<F, P, O> {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        let mut rules = match self {
            Self::Tree(Tree {
                operator: Logic::And,
                rules,
            }) => rules,
            _ => vec![self],
        };

        match rhs {
            Self::Tree(Tree {
                operator: Logic::And,
                rules: rhs_rules,
            }) => rules.extend(rhs_rules),
            _ => rules.extend(iter::once(rhs)),
        };

        Tree {
            operator: Logic::And,
            rules,
        }
        .into()
    }
}

impl<F, P, O> BitAnd<Clause<F, P, O>> for Expr<F, P, O> {
    type Output = Self;

    fn bitand(self, rhs: Clause<F, P, O>) -> Self::Output {
        self & Expr::from(rhs)
    }
}

impl<F, P, O> BitAnd<Tree<Expr<F, P, O>>> for Expr<F, P, O> {
    type Output = Self;

    fn bitand(self, rhs: Tree<Expr<F, P, O>>) -> Self::Output {
        self & Expr::from(rhs)
    }
}

/// Create a new expression representing the union of the provided expressions.
///
/// # Usage
/// ```rust
/// # use filter_ast::{Expr, Logic};
/// let a = Expr::new_clause("field", "=", "v1");
/// let b = Expr::new_clause("other", "=", "a");
/// let c = a | b;
/// let c_tree = c.as_tree().unwrap();
/// assert_eq!(c_tree.operator(), Logic::Or);
/// assert_eq!(c_tree.rules()[0].as_clause().unwrap().field(), &"field");
/// assert_eq!(c_tree.rules()[1].as_clause().unwrap().field(), &"other");
/// ```
///
/// # Tree Simplification
/// This operation will try to avoid increasing tree depth by merging rules from input tree expressions
/// whose operator is `Logic::And`. For example, `(a | b) | (c | d | e)` will produce `a | b | c | d | e`.
impl<F, P, O> BitOr for Expr<F, P, O> {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        let mut rules = match self {
            Self::Tree(Tree {
                operator: Logic::Or,
                rules,
            }) => rules,
            _ => vec![self],
        };

        match rhs {
            Self::Tree(Tree {
                operator: Logic::Or,
                rules: rhs_rules,
            }) => rules.extend(rhs_rules),
            _ => rules.extend(iter::once(rhs)),
        };

        Tree {
            operator: Logic::Or,
            rules,
        }
        .into()
    }
}

impl<F, P, O> BitOr<Clause<F, P, O>> for Expr<F, P, O> {
    type Output = Self;

    fn bitor(self, rhs: Clause<F, P, O>) -> Self::Output {
        self | Expr::from(rhs)
    }
}

impl<F, P, O> BitOr<Tree<Expr<F, P, O>>> for Expr<F, P, O> {
    type Output = Self;

    fn bitor(self, rhs: Tree<Expr<F, P, O>>) -> Self::Output {
        self | Expr::from(rhs)
    }
}

/// Create a new expression representing the inverse of the provided expression.
impl<F, P, O> Not for Expr<F, P, O> {
    type Output = Self;

    fn not(self) -> Self::Output {
        Tree::new(Logic::Not, vec![self]).into()
    }
}

/// Iterator over all clauses in an expression.
///
/// Created with [`Expr::clauses`].
#[derive(Clone)]
pub struct Clauses<'ast, F, P, O> {
    expr: &'ast Expr<F, P, O>,
    stack: Vec<slice::Iter<'ast, Expr<F, P, O>>>,
    has_visited_root: bool,
}

impl<'ast, F, P, O> Clauses<'ast, F, P, O> {
    fn start_tree(&mut self, tree: &'ast Tree<Expr<F, P, O>>) {
        self.stack.push(tree.rules().iter());
    }
}

impl<'ast, F, P, O> Iterator for Clauses<'ast, F, P, O> {
    type Item = &'ast Clause<F, P, O>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.stack.last_mut() {
            Some(i) => match i.next() {
                Some(Expr::Clause(clause)) => Some(clause),
                Some(Expr::Tree(tree)) => {
                    self.start_tree(tree);
                    self.next()
                }
                None => {
                    self.stack.pop();
                    self.next()
                }
            },
            None if !self.has_visited_root => {
                self.has_visited_root = true;
                match self.expr {
                    Expr::Clause(clause) => Some(clause),
                    Expr::Tree(tree) => {
                        self.start_tree(tree);
                        self.next()
                    }
                }
            }
            None => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[allow(clippy::clone_double_ref)]
    fn expand_ipaddr(
        clause: Clause<&'static str, &'static str, &'static str>,
    ) -> Expr<&'static str, &'static str, &'static str> {
        if *clause.field() == ".ipaddr" {
            Tree::new(
                Logic::Or,
                vec![
                    Expr::new_clause(
                        "clientAddr",
                        clause.operator().clone(),
                        clause.operand().clone(),
                    ),
                    Expr::new_clause(
                        "serverAddr",
                        clause.operator().clone(),
                        clause.operand().clone(),
                    ),
                    Expr::new_clause(
                        "senderAddr",
                        clause.operator().clone(),
                        clause.operand().clone(),
                    ),
                    Expr::new_clause(
                        "receiverAddr",
                        clause.operator().clone(),
                        clause.operand().clone(),
                    ),
                ],
            )
            .into()
        } else {
            clause.into()
        }
    }

    /// Test that `Expr::map` can be used to expand clauses into subtrees
    #[test]
    fn expand_filter() {
        let filter = Expr::from(Tree::new(
            Logic::And,
            vec![
                Expr::new_clause("serverAddr", "=", "1"),
                Expr::new_clause(".ipaddr", "=", "2"),
                Expr::new_clause("serverAddr", "=", "3"),
            ],
        ));

        let mapped = filter.map(&expand_ipaddr);

        let m_tree = mapped
            .as_tree()
            .expect("Filter mapping should not convert tree to clause");
        assert_eq!(3, m_tree.rules().len());
        assert!(m_tree.rules[1].is_tree());
    }

    /// Test that `Expr::map` compiles and runs with a closure
    #[test]
    fn map_filter_with_closure() {
        let filter = Expr::from(Tree::new(
            Logic::Or,
            vec![
                Expr::new_clause("server", "=", "a"),
                Expr::new_clause(".device", "=", "a"),
            ],
        ));

        let mapped = filter.map(&|clause| {
            if *clause.field() == ".device" {
                Expr::new_clause("example", "!=", "b")
            } else {
                clause.into()
            }
        });

        let m_tree = mapped
            .as_tree()
            .expect("Filter mapping should not convert tree to clause");

        assert_eq!(*m_tree.rules()[1].as_clause().unwrap().field(), "example");
    }

    #[test]
    fn clause_iter() {
        let filter = Expr::from(Tree::new(
            Logic::Or,
            vec![
                Expr::new_clause(0, "=", "a"),
                Tree::new(
                    Logic::And,
                    vec![Expr::new_clause(1, "=", "a"), Expr::new_clause(2, "=", "a")],
                )
                .into(),
                Expr::new_clause(3, "=", "a"),
            ],
        ));

        let indices = filter
            .clauses()
            .map(|clause| *clause.field())
            .collect::<Vec<_>>();
        let expected = (0..=3).collect::<Vec<_>>();
        assert_eq!(indices, expected);
    }

    #[test]
    fn bitand_clauses() {
        let expr = Clause::new("server", "=", "a")
            & Clause::new("client", "=", "b")
            & Clause::new("url", "~", "login");
        let tree: Tree<Expr<_, _, _>> = expr.as_tree().cloned().unwrap();
        assert_eq!(tree.operator, Logic::And);
        assert_eq!(tree.rules.len(), 3);
    }
}