bomboni_request 0.3.0

Utilities for working with API requests. Part of Bomboni library.
Documentation
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//! # Filter
//!
//! Utility for specifying filters on queries, as described in Google AIP standard [1].
//!
//! [1]: https://google.aip.dev/160

use std::fmt;
use std::fmt::{Display, Formatter, Write};

use itertools::Itertools;
use parser::{FilterParser, Rule};
use pest::Parser;
use pest::iterators::Pair;

use crate::filter::error::FilterError;
use crate::schema::{FunctionSchemaMap, MemberSchema, Schema, SchemaMapped, ValueType};
use crate::value::Value;
use error::FilterResult;

/// Filter error types.
pub mod error;

#[allow(clippy::upper_case_acronyms)]
pub(crate) mod parser {
    use pest_derive::Parser;

    #[derive(Parser)]
    #[grammar = "./filter/grammar.pest"]
    pub struct FilterParser;
}

/// Filter expression.
#[derive(Debug, Clone, PartialEq)]
pub enum Filter {
    /// Logical AND of filters.
    Conjunction(Vec<Self>),
    /// Logical OR of filters.
    Disjunction(Vec<Self>),
    /// Logical NOT of a filter.
    Negate(Box<Self>),
    /// Comparison filter.
    Restriction(Box<Self>, FilterComparator, Box<Self>),
    /// Function call filter.
    Function(String, Vec<Self>),
    /// Composite filter.
    Composite(Box<Self>),
    /// Field name.
    Name(String),
    /// Literal value.
    Value(Value),
}

/// Filter comparison operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FilterComparator {
    /// Less than.
    Less,
    /// Less than or equal.
    LessOrEqual,
    /// Greater than.
    Greater,
    /// Greater than or equal.
    GreaterOrEqual,
    /// Equal.
    Equal,
    /// Not equal.
    NotEqual,
    /// Has operator.
    Has,
}

impl Filter {
    /// Parses a filter from a source string.
    ///
    /// # Errors
    ///
    /// Will return [`FilterError::Parse`] if the source string cannot be parsed as a valid filter.
    ///
    /// # Panics
    ///
    /// Will panic if parsing fails to return a valid filter tree.
    pub fn parse(source: &str) -> FilterResult<Self> {
        let filter = FilterParser::parse(Rule::Filter, source)?.next().unwrap();
        Self::parse_tree(filter)
    }

    fn parse_tree(pair: Pair<Rule>) -> FilterResult<Self> {
        match pair.as_rule() {
            Rule::Filter | Rule::Expression => {
                match pair
                    .into_inner()
                    .filter(|pair| pair.as_rule() != Rule::EOI)
                    .map(Self::parse_tree)
                    .exactly_one()
                {
                    Ok(inner_tree) => inner_tree,
                    Err(inner_trees) => Ok(Self::Conjunction(inner_trees.try_collect()?)),
                }
            }
            Rule::Factor => match pair.into_inner().map(Self::parse_tree).exactly_one() {
                Ok(inner_tree) => inner_tree,
                Err(inner_trees) => Ok(Self::Disjunction(inner_trees.try_collect()?)),
            },
            Rule::Term => {
                let lexeme = pair.as_str().trim();
                if lexeme.starts_with("NOT") || lexeme.starts_with('-') {
                    Ok(Self::Negate(Box::new(Self::parse_tree(
                        pair.into_inner().next().unwrap(),
                    )?)))
                } else {
                    Self::parse_tree(pair.into_inner().next().unwrap())
                }
            }
            Rule::Restriction => {
                let mut inner_pairs = pair.into_inner();
                let comparable = inner_pairs.next().unwrap();
                match inner_pairs.next() {
                    Some(inner_pair) => {
                        let comparator = match inner_pair.as_str() {
                            "<" => FilterComparator::Less,
                            "<=" => FilterComparator::LessOrEqual,
                            ">" => FilterComparator::Greater,
                            ">=" => FilterComparator::GreaterOrEqual,
                            "=" => FilterComparator::Equal,
                            "!=" => FilterComparator::NotEqual,
                            ":" => FilterComparator::Has,
                            _ => unreachable!(),
                        };
                        let arg = inner_pairs.next().unwrap();
                        Ok(Self::Restriction(
                            Box::new(Self::parse_tree(comparable)?),
                            comparator,
                            Box::new(Self::parse_tree(arg)?),
                        ))
                    }
                    None => Self::parse_tree(comparable),
                }
            }
            Rule::Comparable => Self::parse_tree(pair.into_inner().next().unwrap()),
            Rule::Function => {
                let mut name = String::new();
                let mut arguments = Vec::new();
                let mut argument_list = false;
                for pair in pair.into_inner() {
                    if argument_list {
                        arguments.push(Self::parse_tree(pair)?);
                    } else if pair.as_rule() == Rule::Name {
                        name = pair.as_str().into();
                    } else {
                        arguments.push(Self::parse_tree(pair)?);
                        argument_list = true;
                    }
                }
                Ok(Self::Function(name, arguments))
            }
            Rule::Composite => Ok(Self::Composite(Box::new(Self::parse_tree(
                pair.into_inner().next().unwrap(),
            )?))),
            Rule::Name => Ok(Self::Name(
                pair.into_inner()
                    .map(|identifier| identifier.as_str())
                    .join("."),
            )),
            Rule::String | Rule::Boolean | Rule::Number | Rule::Any => {
                Ok(Self::Value(Value::parse(&pair)?))
            }
            _ => {
                unreachable!("{:?}", pair);
            }
        }
    }

    /// Returns the length of the filter.
    pub fn len(&self) -> usize {
        match self {
            Self::Conjunction(parts) | Self::Disjunction(parts) => {
                parts.iter().map(Self::len).sum::<usize>()
            }
            Self::Negate(tree) => 1usize + tree.as_ref().len(),
            Self::Restriction(comparable, _, arg) => {
                1usize + comparable.as_ref().len() + arg.as_ref().len()
            }
            Self::Function(tree, arguments) => {
                1usize + tree.len() + arguments.iter().map(Self::len).sum::<usize>()
            }
            Self::Composite(composite) => 1usize + composite.as_ref().len(),
            _ => 1usize,
        }
    }

    /// Evaluates the filter against an item.
    pub fn evaluate<T>(&self, item: &T) -> Option<Value>
    where
        T: SchemaMapped,
    {
        match self {
            Self::Conjunction(parts) => {
                let mut res = true;
                for part in parts {
                    if let Some(Value::Boolean(part_res)) = part.evaluate(item) {
                        if !part_res {
                            res = false;
                            break;
                        }
                    } else {
                        return None;
                    }
                }
                Some(Value::Boolean(res))
            }
            Self::Disjunction(parts) => {
                let mut res = false;
                for part in parts {
                    if let Some(Value::Boolean(part_res)) = part.evaluate(item) {
                        if part_res {
                            res = true;
                            break;
                        }
                    } else {
                        return None;
                    }
                }
                Some(Value::Boolean(res))
            }
            Self::Negate(composite) => {
                if let Value::Boolean(value) = composite.evaluate(item)? {
                    Some(Value::Boolean(!value))
                } else {
                    None
                }
            }
            Self::Restriction(comparable, comparator, arg) => {
                let a = comparable.evaluate(item)?;
                match a {
                    Value::Integer(a) => {
                        if let Value::Integer(b) = arg.evaluate(item)? {
                            Some(
                                match comparator {
                                    FilterComparator::Less => a < b,
                                    FilterComparator::LessOrEqual => a <= b,
                                    FilterComparator::Greater => a > b,
                                    FilterComparator::GreaterOrEqual => a >= b,
                                    FilterComparator::Equal | FilterComparator::Has => a == b,
                                    FilterComparator::NotEqual => a != b,
                                }
                                .into(),
                            )
                        } else {
                            None
                        }
                    }
                    Value::Float(a) => {
                        if let Value::Float(b) = arg.evaluate(item)? {
                            Some(
                                match comparator {
                                    FilterComparator::Less => a < b,
                                    FilterComparator::LessOrEqual => a <= b,
                                    FilterComparator::Greater => a > b,
                                    FilterComparator::GreaterOrEqual => a >= b,
                                    FilterComparator::Equal | FilterComparator::Has => {
                                        (a - b).abs() < f64::EPSILON
                                    }
                                    FilterComparator::NotEqual => (a - b).abs() > f64::EPSILON,
                                }
                                .into(),
                            )
                        } else {
                            None
                        }
                    }
                    Value::String(a) => {
                        if let Value::String(b) = arg.evaluate(item)? {
                            Some(
                                match comparator {
                                    FilterComparator::Less => a < b,
                                    FilterComparator::LessOrEqual => a <= b,
                                    FilterComparator::Greater => a > b,
                                    FilterComparator::GreaterOrEqual => a >= b,
                                    FilterComparator::Equal => a == b,
                                    FilterComparator::NotEqual => a != b,
                                    FilterComparator::Has => a.contains(b.as_str()),
                                }
                                .into(),
                            )
                        } else {
                            None
                        }
                    }
                    Value::Boolean(a) => {
                        if let Value::Boolean(b) = arg.evaluate(item)? {
                            match comparator {
                                FilterComparator::Equal | FilterComparator::Has => {
                                    Some((a == b).into())
                                }
                                FilterComparator::NotEqual => Some((a != b).into()),
                                _ => None,
                            }
                        } else {
                            None
                        }
                    }
                    Value::Timestamp(a) => {
                        if let Value::Timestamp(b) = arg.evaluate(item)? {
                            Some(
                                match comparator {
                                    FilterComparator::Less => a < b,
                                    FilterComparator::LessOrEqual => a <= b,
                                    FilterComparator::Greater => a > b,
                                    FilterComparator::GreaterOrEqual => a >= b,
                                    FilterComparator::Equal | FilterComparator::Has => a == b,
                                    FilterComparator::NotEqual => a != b,
                                }
                                .into(),
                            )
                        } else {
                            None
                        }
                    }
                    Value::Repeated(a) => match comparator {
                        FilterComparator::Equal => {
                            if let Value::Repeated(b) = arg.evaluate(item)? {
                                Some((a == b).into())
                            } else {
                                None
                            }
                        }
                        FilterComparator::NotEqual => {
                            if let Value::Repeated(b) = arg.evaluate(item)? {
                                Some((a != b).into())
                            } else {
                                None
                            }
                        }
                        FilterComparator::Has => arg.evaluate(item).map_or_else(
                            || {
                                if let Self::Composite(composite) = &**arg {
                                    match composite.as_ref() {
                                        Self::Conjunction(parts) => Some(Value::Boolean(
                                            parts.iter().map(|part| part.evaluate(item)).all(
                                                |value| {
                                                    value
                                                        .as_ref()
                                                        .is_some_and(|value| a.contains(value))
                                                },
                                            ),
                                        )),
                                        Self::Disjunction(parts) => Some(Value::Boolean(
                                            parts.iter().map(|part| part.evaluate(item)).any(
                                                |value| {
                                                    value
                                                        .as_ref()
                                                        .is_some_and(|value| a.contains(value))
                                                },
                                            ),
                                        )),
                                        _ => None,
                                    }
                                } else {
                                    None
                                }
                            },
                            |b| Some(a.contains(&b).into()),
                        ),
                        _ => None,
                    },
                    Value::Any => Some(Value::Boolean(true)),
                }
            }
            Self::Composite(composite) => composite.evaluate(item),
            Self::Value(value) => Some(value.clone()),
            Self::Name(name) => Some(item.get_field(name)),
            Self::Function(_, _) => unimplemented!("evaluate {:?}", self),
        }
    }

    /// Gets the result value type for the filter.
    ///
    /// # Errors
    ///
    /// Will return [`FilterError::UnknownFunction`] if the filter contains an unknown function name.
    /// Will return [`FilterError::UnknownMember`] if the filter contains an unknown field name.
    /// Will return [`FilterError::InvalidResultValueType`] if the filter value type cannot be determined.
    pub fn get_result_value_type(
        &self,
        schema: &Schema,
        schema_functions: Option<&FunctionSchemaMap>,
    ) -> FilterResult<ValueType> {
        match self {
            Self::Conjunction(_)
            | Self::Disjunction(_)
            | Self::Negate(_)
            | Self::Restriction(_, _, _) => Ok(ValueType::Boolean),
            Self::Function(name, _) => Ok(schema_functions
                .and_then(|schema_functions| schema_functions.get(name))
                .ok_or_else(|| FilterError::UnknownFunction(name.clone()))?
                .return_value_type),
            Self::Composite(composite) => composite.get_result_value_type(schema, schema_functions),
            Self::Name(name) => {
                let member_schema = schema
                    .get_member(name)
                    .ok_or_else(|| FilterError::UnknownMember(name.clone()))?;
                if let MemberSchema::Field(field) = member_schema {
                    Ok(field.value_type)
                } else {
                    Err(FilterError::InvalidResultValueType)
                }
            }
            Self::Value(value) => value
                .value_type()
                .ok_or(FilterError::InvalidResultValueType),
        }
    }

    /// Validates filter against schema.
    ///
    /// # Errors
    ///
    /// Will return [`FilterError::InvalidType`] if filter parts have incompatible types.
    /// Will return [`FilterError::UnknownFunction`] if the filter contains an unknown function name.
    /// Will return [`FilterError::FunctionInvalidArgumentCount`] if function argument count doesn't match schema.
    /// Will return [`FilterError::UnknownMember`] if the filter contains an unknown field name.
    /// Will return [`FilterError::UnsuitableComparator`] if an unsuitable comparator is used.
    pub fn validate(
        &self,
        schema: &Schema,
        schema_functions: Option<&FunctionSchemaMap>,
    ) -> FilterResult<()> {
        match self {
            Self::Conjunction(parts) | Self::Disjunction(parts) => {
                for part in parts {
                    part.validate(schema, schema_functions)?;
                    let part_value_type = part.get_result_value_type(schema, schema_functions)?;
                    if part_value_type != ValueType::Boolean {
                        return Err(FilterError::InvalidType {
                            actual: part_value_type,
                            expected: ValueType::Boolean,
                        });
                    }
                }
            }
            Self::Negate(tree) => {
                let result_value_type = tree.get_result_value_type(schema, schema_functions)?;
                if result_value_type != ValueType::Boolean {
                    return Err(FilterError::InvalidType {
                        actual: result_value_type,
                        expected: ValueType::Boolean,
                    });
                }
            }
            Self::Restriction(comparable, comparator, argument) => {
                comparable.validate(schema, schema_functions)?;
                let comparable_type = comparable.get_result_value_type(schema, schema_functions)?;
                argument.validate(schema, schema_functions)?;
                let argument_type = argument.get_result_value_type(schema, schema_functions)?;

                if comparator == &FilterComparator::Has {
                    return Err(FilterError::UnsuitableComparator(*comparator));
                    //     if !matches!(**argument, Filter::Conjunction(_) | Filter::Disjunction(_)) {
                    //         if comparable_type != argument_type && argument_type != ValueType::Any {
                    //             return Err(FilterError::InvalidType {
                    //                 actual: argument_type,
                    //                 expected: comparable_type,
                    //             });
                    //         }
                    //     }
                }

                if comparable_type != argument_type {
                    return Err(FilterError::InvalidType {
                        actual: argument_type,
                        expected: comparable_type,
                    });
                }
            }
            Self::Function(name, arguments) => {
                let function = schema_functions
                    .and_then(|schema_functions| schema_functions.get(name))
                    .ok_or_else(|| FilterError::UnknownFunction(name.clone()))?;
                if function.argument_value_types.len() != arguments.len() {
                    return Err(FilterError::FunctionInvalidArgumentCount {
                        name: name.clone(),
                        expected: function.argument_value_types.len(),
                    });
                }
                for (argument, argument_schema) in
                    arguments.iter().zip(&function.argument_value_types)
                {
                    argument.validate(schema, schema_functions)?;
                    let argument_value_type =
                        argument.get_result_value_type(schema, schema_functions)?;
                    if argument_value_type != *argument_schema {
                        return Err(FilterError::InvalidType {
                            actual: argument_value_type,
                            expected: *argument_schema,
                        });
                    }
                }
            }
            Self::Composite(composite) => composite.validate(schema, schema_functions)?,
            Self::Name(name) => {
                if schema.get_member(name).is_none() {
                    return Err(FilterError::UnknownMember(name.clone()));
                }
            }
            Self::Value(_) => {}
        }
        Ok(())
    }

    /// Checks if the filter is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Adds a conjunction to the filter.
    pub fn add_conjunction(&mut self, other: Self) {
        match self {
            Self::Conjunction(filters) => {
                filters.push(other);
            }
            _ => {
                *self = Self::Conjunction(vec![self.clone(), other]);
            }
        }
    }

    /// Adds a disjunction to the filter.
    pub fn add_disjunction(&mut self, other: Self) {
        match self {
            Self::Disjunction(filters) => {
                filters.push(other);
            }
            _ => {
                *self = Self::Disjunction(vec![self.clone(), other]);
            }
        }
    }
}

impl Default for Filter {
    fn default() -> Self {
        Self::Conjunction(Vec::new())
    }
}

impl Display for Filter {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Conjunction(parts) => parts.iter().map(ToString::to_string).join(" AND ").fmt(f),
            Self::Disjunction(parts) => parts.iter().map(ToString::to_string).join(" OR ").fmt(f),
            Self::Negate(tree) => {
                f.write_str("NOT ")?;
                tree.fmt(f)
            }
            Self::Restriction(comparable, comparator, arg) => match comparator {
                FilterComparator::Has => {
                    comparable.fmt(f)?;
                    f.write_char(':')?;
                    arg.fmt(f)
                }
                _ => {
                    write!(f, "{comparable} {comparator} {arg}")
                }
            },
            Self::Function(name, arguments) => {
                write!(
                    f,
                    "{}({})",
                    name,
                    arguments.iter().map(ToString::to_string).join(", ")
                )
            }
            Self::Composite(composite) => {
                f.write_char('(')?;
                composite.fmt(f)?;
                f.write_char(')')
            }
            Self::Name(name) => name.fmt(f),
            Self::Value(value) => value.fmt(f),
        }
    }
}

impl Display for FilterComparator {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Less => f.write_str("<"),
            Self::LessOrEqual => f.write_str("<="),
            Self::Greater => f.write_str(">"),
            Self::GreaterOrEqual => f.write_str(">="),
            Self::Equal => f.write_str("="),
            Self::NotEqual => f.write_str("!="),
            Self::Has => f.write_str(":"),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::testing::schema::{RequestItem, TaskItem, UserItem};

    use super::*;

    #[test]
    fn validate_schema() {
        let schema = UserItem::get_schema();
        macro_rules! check {
            (@ok $filter:expr) => {
                match check!($filter) {
                    Ok(_) => {}
                    Err(err) => {
                        panic!("Expected Ok, got Err: {:#?}", err);
                    }
                }
            };
            (@error $filter:expr) => {
                match check!($filter) {
                    Ok(_) => {
                        panic!("Expected Err, got Ok");
                    }
                    Err(_) => {}
                }
            };
            ($filter:expr) => {
                Filter::parse($filter).unwrap().validate(&schema, None)
            };
        }

        check!(@ok "42");
        check!(@ok "false");
        check!(@ok r#"id="42""#);

        check!(@error "a");
        check!(@error "a.b");
        check!(@error "f()");
        check!(@error "id=42");
    }

    #[test]
    fn it_works() {
        Filter::parse("  ").unwrap();
        Filter::parse("").unwrap();
        Filter::parse("x").unwrap();
        Filter::parse("42").unwrap();
        Filter::parse("x =  42").unwrap();
        Filter::parse("x=42").unwrap();
        Filter::parse("42").unwrap();
        Filter::parse("3.14").unwrap();
        Filter::parse("NOT a").unwrap();
        Filter::parse("NOT    a").unwrap();
        Filter::parse("a b AND c AND d").unwrap();
        Filter::parse("a < 10 OR a >= 100").unwrap();
        Filter::parse("NOT (a OR b)").unwrap();
        Filter::parse("-30").unwrap();
        Filter::parse("x.b:42").unwrap();
        Filter::parse("experiment.rollout <= cohort(request.user)").unwrap();
        Filter::parse("expr.type_map.type").unwrap();
        Filter::parse("expr.type_map.type").unwrap();
        Filter::parse("regex(m.key, a)").unwrap();
        Filter::parse(r#"math.mem("30mb")"#).unwrap();
        Filter::parse(r#"regex(m.key, "^.*prod.*$")"#).unwrap();
        Filter::parse(r#"(msg.endsWith("world") AND retries < 10)"#).unwrap();
        Filter::parse("x:*").unwrap();

        assert!(Filter::parse("x==42").is_err());
        assert!(Filter::parse("--").is_err());
    }

    #[test]
    fn parse_tree() {
        use Filter::*;
        use FilterComparator::*;

        let tree = Filter::parse("(a.f(42) AND c < 10) OR x AND y:z AND NOT w != true").unwrap();
        assert_eq!(tree.len(), 17);
        assert_eq!(
            tree,
            Conjunction(vec![
                Disjunction(vec![
                    Composite(Box::new(Conjunction(vec![
                        Function("a.f".into(), vec![Value(42.into())]),
                        Restriction(Box::new(Name("c".into())), Less, Box::new(Value(10.into()))),
                    ],),)),
                    Name("x".into()),
                ],),
                Restriction(Box::new(Name("y".into())), Has, Box::new(Name("z".into()))),
                Negate(Box::new(Restriction(
                    Box::new(Name("w".into())),
                    NotEqual,
                    Box::new(Value(true.into())),
                )),),
            ])
        );
    }

    #[test]
    fn to_string() {
        let src = "(a.f(42) AND c < 10) OR x AND y:z AND NOT w != true";
        let tree = Filter::parse(src).unwrap();
        assert_eq!(tree.to_string(), src);
    }

    #[test]
    fn modify() {
        let mut f = Filter::parse("x=42").unwrap();
        f.add_conjunction(Filter::parse("false").unwrap());
        assert_eq!(f.to_string(), "x = 42 AND false");
        let mut f = Filter::parse("x=42").unwrap();
        f.add_disjunction(Filter::parse("true").unwrap());
        assert_eq!(f.to_string(), "x = 42 OR true");
    }

    #[test]
    fn evaluate() {
        let f = Filter::parse(
            r#"
            user.age >= 18
            AND user.id:"4"
            AND NOT (task.deleted = false)
            AND task.content = user.displayName
            AND task.tags:("a" "b")
            AND task.tags:("d" OR "a")
        "#,
        )
        .unwrap();

        let res = f
            .evaluate(&RequestItem {
                user: UserItem {
                    id: "42".into(),
                    display_name: "test".into(),
                    age: 30,
                },
                task: TaskItem {
                    id: "1".into(),
                    user_id: "42".into(),
                    content: "test".into(),
                    deleted: true,
                    tags: vec!["a".into(), "b".into(), "c".into()],
                },
            })
            .unwrap();
        assert_eq!(res, Value::Boolean(true));
    }
}