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
use std::fmt;

#[derive(Clone, Debug, Eq, PartialEq)]
#[allow(unused)]
pub enum Value {
    String(Option<String>),
    Array(Option<Vec<Value>>),
    Integer(Option<i64>),
    Boolean(Option<bool>),
}

#[allow(unused)]
impl Value {
    pub fn eq(&self, b: &Value) -> bool {
        std::mem::discriminant(self) == std::mem::discriminant(b)
    }

    pub fn string(v: String) -> Self {
        Value::String(Some(v))
    }

    pub fn str(v: &str) -> Self {
        Value::String(Some(v.to_string()))
    }

    pub fn array(v: Vec<Value>) -> Self {
        Value::Array(Some(v))
    }

    pub fn int(v: i64) -> Self {
        Value::Integer(Some(v))
    }

    pub fn bool(v: bool) -> Self {
        Value::Boolean(Some(v))
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[allow(unused)]
pub struct Params {
    pub offset: Option<i64>,
    pub max_size: Option<i64>,
    pub select: Option<String>,
    pub r#where: Option<Vec<Where>>,
    pub primary_filter: Option<String>,
    pub bool_filter_list: Option<Vec<String>>,
    pub order: Option<Order>,
    pub order_by: Option<String>,
}

impl Default for Params {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(unused)]
impl Params {
    pub fn new() -> Self {
        Self {
            offset: None,
            max_size: None,
            select: None,
            r#where: None,
            primary_filter: None,
            bool_filter_list: None,
            order_by: None,
            order: None,
        }
    }

    pub fn set_offset(&mut self, offset: i64) -> &mut Self {
        self.offset = Some(offset);
        self
    }

    pub fn set_max_size(&mut self, max_size: i64) -> &mut Self {
        self.max_size = Some(max_size);
        self
    }

    pub fn set_select(&mut self, select: &str) -> &mut Self {
        self.select = Some(select.to_string());
        self
    }

    pub fn set_where(&mut self, r#where: Vec<Where>) -> &mut Self {
        self.r#where = Some(r#where);
        self
    }

    pub fn set_primary_filter(&mut self, primary_filter: &str) -> &mut Self {
        self.primary_filter = Some(primary_filter.to_string());
        self
    }

    pub fn set_bool_filter_list(&mut self, bool_filter_list: Vec<String>) -> &mut Self {
        self.bool_filter_list = Some(bool_filter_list);
        self
    }

    pub fn set_order(&mut self, order: Order) -> &mut Self {
        self.order = Some(order);
        self
    }

    pub fn set_order_by(&mut self, order_by: &str) -> &mut Self {
        self.order_by = Some(order_by.to_string());
        self
    }

    pub fn build(&self) -> Self {
        self.clone()
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[allow(unused)]
pub enum Order {
    Asc,
    Desc,
}

impl fmt::Display for Order {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[allow(unused)]
pub struct Where {
    pub r#type: FilterType,
    pub attribute: String,
    pub value: Option<Value>,
}

#[allow(unused)]
impl Where {
    pub fn new(filter_type: FilterType, attribute: &str, value: Option<Value>) -> Self {
        Where {
            r#type: filter_type,
            attribute: attribute.to_string(),
            value,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[allow(unused)]
pub enum FilterType {
    Equals,
    NotEquals,
    GreaterThan,
    LessThan,
    GreaterThanOrEquals,
    LessThanOrEquals,
    IsNull,
    IsNotNull,
    IsTrue,
    IsFalse,
    LinkedWith,
    NotLinkedWith,
    IsLinked,
    IsNotLinked,
    In,
    NotIn,
    Contains,
    NotContains,
    StartsWith,
    EndsWith,
    Like,
    NotLike,
    Or,
    AndToday,
    Past,
    Future,
    LastSevenDays,
    CurrentMonth,
    LastMonth,
    NextMonth,
    CurrentQuarter,
    LastQuarter,
    CurrentYear,
    LastYear,
    CurrentFiscalYear,
    LastFiscalYear,
    CurrentFiscalQuarter,
    LastFiscalQuarter,
    LastXDays,
    NextXDays,
    OlderThanXDays,
    AfterXDays,
    Between,
    ArrayAnyOf,
    ArrayNoneOf,
    ArrayAllOf,
    ArrayIsEmpty,
    ArrayIsNotEmpty,
}

impl fmt::Display for FilterType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}