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
pub struct Filters<'a> {
    pub(crate) and: Option<u8>,
    pub(crate) blank: Option<u8>,
    pub(crate) filters: Vec<Filter<'a>>,
}

impl<'a> Filters<'a> {
    pub fn new() -> Self {
        Self {
            and: None,
            blank: None,
            filters: vec![],
        }
    }
    pub fn eq(vals: Vec<&'a str>) -> Self {
        Self {
            and: None,
            blank: None,
            filters: vals.iter().map(|&v| Filter::eq(v)).collect(),
        }
    }
    pub fn blank() -> Self {
        Self {
            and: None,
            blank: Some(1),
            filters: vec![],
        }
    }
    pub fn not_blank() -> Self {
        Self {
            and: None,
            blank: None,
            filters: vec![Filter::ne(" ")],
        }
    }
    pub fn or(&mut self, filter: Filter<'a>) -> &mut Self {
        self.filters.push(filter);
        self
    }
    pub fn and(&mut self, filter: Filter<'a>) -> &mut Self {
        self.and = Some(1);
        self.filters.push(filter);
        self
    }

    pub(crate) fn is_custom_filters(&self) -> bool {
        self.filters.iter().any(|f| f.custom_filter)
    }
}

pub struct Filter<'a> {
    pub(crate) val: &'a str,
    pub(crate) operator: Option<&'a str>,
    custom_filter: bool,
}

impl<'a> Filter<'a> {
    pub fn eq(val: &'a str) -> Self {
        Self {
            val,
            operator: None,
            custom_filter: false,
        }
    }
    pub fn gt(val: &'a str) -> Self {
        Self {
            val,
            operator: Some("greaterThan"),
            custom_filter: true,
        }
    }
    pub fn lt(val: &'a str) -> Self {
        Self {
            val,
            operator: Some("lessThan"),
            custom_filter: true,
        }
    }

    pub fn ne(val: &'a str) -> Self {
        Self {
            val,
            operator: Some("notEqual"),
            custom_filter: true,
        }
    }
}