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
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::fmt;

use super::functions;
use super::QueryError;
use aw_models::Event;
use aw_transform::classify::{RegexRule, Rule};

use serde::{Serialize, Serializer};
use serde_json::value::Value;
use serde_json::Number;

// TODO: greater/less comparisons

#[derive(Clone, Serialize)]
#[serde(untagged)]
pub enum DataType {
    None(),
    Bool(bool),
    Number(f64),
    String(String),
    Event(Event),
    List(Vec<DataType>),
    Dict(HashMap<String, DataType>),
    #[serde(serialize_with = "serialize_function")]
    Function(String, functions::QueryFn),
}

#[allow(clippy::trivially_copy_pass_by_ref)]
fn serialize_function<S>(
    _element: &str,
    _fun: &functions::QueryFn,
    _serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    panic!("Query function was unevaluated and was attempted to be serialized, panic!");
    //element.id.serialize(serializer)
}

// Needed because of a limitation in rust where you cannot derive(Debug) on a
// enum which has a fn with reference parameters which our QueryFn has
// https://stackoverflow.com/questions/53380040/function-pointer-with-a-reference-argument-cannot-derive-debug
impl fmt::Debug for DataType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            DataType::None() => write!(f, "None()"),
            DataType::Bool(b) => write!(f, "Bool({})", b),
            DataType::Number(n) => write!(f, "Number({})", n),
            DataType::String(s) => write!(f, "String({})", s),
            DataType::Event(e) => write!(f, "Event({:?})", e),
            DataType::List(l) => write!(f, "List({:?})", l),
            DataType::Dict(d) => write!(f, "Dict({:?})", d),
            DataType::Function(name, _fun) => write!(f, "Function({})", name),
        }
    }
}

/* Like eq, but raises an error when comparing between different types.
 * Should be used as often as possible */
impl DataType {
    pub fn query_eq(&self, other: &DataType) -> Result<bool, QueryError> {
        match (self, other) {
            // TODO: Comparisons of bool == num, bool == str
            (DataType::None(), DataType::None()) => Ok(false),
            (DataType::Bool(b1), DataType::Bool(b2)) => Ok(b1 == b2),
            (DataType::Number(n1), DataType::Number(n2)) => Ok(n1 == n2),
            (DataType::String(s1), DataType::String(s2)) => Ok(s1 == s2),
            (DataType::Event(e1), DataType::Event(e2)) => Ok(e1 == e2),
            (DataType::List(l1), DataType::List(l2)) => Ok(l1 == l2),
            (DataType::Dict(d1), DataType::Dict(d2)) => Ok(d1 == d2),
            // We do not care about comparing functions
            _ => Err(QueryError::InvalidType(format!(
                "Cannot compare values of different types {:?} and {:?}",
                self, other
            ))),
        }
    }
}

/* Required for query_eq when comparing two dicts */
impl PartialEq for DataType {
    fn eq(&self, other: &DataType) -> bool {
        match (self, other) {
            (DataType::None(), DataType::None()) => true,
            // TODO: Comparisons of bool == num, bool == str
            (DataType::Bool(b1), DataType::Bool(b2)) => b1 == b2,
            (DataType::Number(n1), DataType::Number(n2)) => n1 == n2,
            (DataType::String(s1), DataType::String(s2)) => s1 == s2,
            (DataType::Event(e1), DataType::Event(e2)) => e1 == e2,
            (DataType::List(l1), DataType::List(l2)) => l1 == l2,
            (DataType::Dict(d1), DataType::Dict(d2)) => d1 == d2,
            // We do not care about comparing functions
            _ => false,
        }
    }
}

impl TryFrom<&DataType> for Vec<DataType> {
    type Error = QueryError;
    fn try_from(value: &DataType) -> Result<Self, Self::Error> {
        match value {
            DataType::List(ref s) => Ok(s.clone()),
            ref invalid_type => Err(QueryError::InvalidFunctionParameters(format!(
                "Expected function parameter of type List, got {:?}",
                invalid_type
            ))),
        }
    }
}

impl TryFrom<&DataType> for String {
    type Error = QueryError;
    fn try_from(value: &DataType) -> Result<Self, Self::Error> {
        match value {
            DataType::String(s) => Ok(s.clone()),
            ref invalid_type => Err(QueryError::InvalidFunctionParameters(format!(
                "Expected function parameter of type String, list contains {:?}",
                invalid_type
            ))),
        }
    }
}

impl TryFrom<&DataType> for Vec<String> {
    type Error = QueryError;
    fn try_from(value: &DataType) -> Result<Self, Self::Error> {
        let mut tagged_strings: Vec<DataType> = value.try_into()?;
        let mut strings = Vec::new();
        for string in tagged_strings.drain(..) {
            let s: String = (&string).try_into()?;
            strings.push(s);
        }
        Ok(strings)
    }
}

impl TryFrom<&DataType> for Vec<Event> {
    type Error = QueryError;
    fn try_from(value: &DataType) -> Result<Self, Self::Error> {
        let mut tagged_events: Vec<DataType> = value.try_into()?;
        let mut events = Vec::new();
        for event in tagged_events.drain(..) {
            match event {
                DataType::Event(e) => events.push(e.clone()),
                ref invalid_type => {
                    return Err(QueryError::InvalidFunctionParameters(format!(
                        "Expected function parameter of type List of Events, list contains {:?}",
                        invalid_type
                    )))
                }
            }
        }
        Ok(events)
    }
}

impl TryFrom<&DataType> for Vec<(String, Rule)> {
    type Error = QueryError;
    fn try_from(value: &DataType) -> Result<Self, Self::Error> {
        let mut tagged_lists: Vec<DataType> = value.try_into()?;
        let mut lists: Vec<(String, Rule)> = Vec::new();
        for list in tagged_lists.drain(..) {
            match list {
                DataType::List(ref l) => {
                    let tag: String = match l.get(0) {
                        Some(tag) => tag.try_into()?,
                        None => return Err(QueryError::InvalidFunctionParameters(
                            format!("Expected function parameter of type list of (tag, rule) tuples, list contains {:?}", l)))
                    };
                    let rule: Rule = match l.get(1) {
                        Some(rule) => rule.try_into()?,
                        None => return Err(QueryError::InvalidFunctionParameters(
                            format!("Expected function parameter of type list of (tag, rule) tuples, list contains {:?}", l)))
                    };
                    lists.push((tag, rule));
                }
                ref invalid_type => {
                    return Err(QueryError::InvalidFunctionParameters(format!(
                        "Expected function parameter of type list of (tag, rule) tuples, got {:?}",
                        invalid_type
                    )))
                }
            }
        }
        Ok(lists)
    }
}

impl TryFrom<&DataType> for Vec<(Vec<String>, Rule)> {
    type Error = QueryError;
    fn try_from(value: &DataType) -> Result<Self, Self::Error> {
        let mut tagged_lists: Vec<DataType> = value.try_into()?;
        let mut lists: Vec<(Vec<String>, Rule)> = Vec::new();
        for list in tagged_lists.drain(..) {
            match list {
                DataType::List(ref l) => {
                    let category: Vec<String> = match l.get(0) {
                        Some(category) => category.try_into()?,
                        None => return Err(QueryError::InvalidFunctionParameters(
                            format!("Expected function parameter of type list of (category, rule) tuples, list contains {:?}", l)))
                    };
                    let rule: Rule = match l.get(1) {
                        Some(rule) => rule.try_into()?,
                        None => return Err(QueryError::InvalidFunctionParameters(
                            format!("Expected function parameter of type list of (category, rule) tuples, list contains {:?}", l)))
                    };
                    lists.push((category, rule));
                }
                ref invalid_type => {
                    return Err(QueryError::InvalidFunctionParameters(format!(
                    "Expected function parameter of type list of (category, rule) tuples, got {:?}",
                    invalid_type
                )))
                }
            }
        }
        Ok(lists)
    }
}

impl TryFrom<&DataType> for f64 {
    type Error = QueryError;
    fn try_from(value: &DataType) -> Result<Self, Self::Error> {
        match value {
            DataType::Number(f) => Ok(*f),
            ref invalid_type => Err(QueryError::InvalidFunctionParameters(format!(
                "Expected function parameter of type Number, got {:?}",
                invalid_type
            ))),
        }
    }
}

impl TryFrom<&DataType> for usize {
    type Error = QueryError;
    fn try_from(value: &DataType) -> Result<Self, Self::Error> {
        let f: f64 = value.try_into()?;
        Ok(f as usize)
    }
}

impl TryFrom<&DataType> for Value {
    type Error = QueryError;
    fn try_from(value: &DataType) -> Result<Self, Self::Error> {
        match value {
            DataType::None() => Ok(Value::Null),
            DataType::Bool(b) => Ok(Value::Bool(*b)),
            DataType::Number(n) => Ok(Value::Number(Number::from_f64(*n).unwrap())),
            DataType::String(s) => Ok(Value::String(s.to_string())),
            DataType::List(_l) => {
                let mut tagged_values: Vec<DataType> = value.try_into()?;
                let mut values: Vec<Value> = Vec::new();
                for value in tagged_values.drain(..) {
                    values.push((&value).try_into()?);
                }
                Ok(Value::Array(values))
            }
            ref invalid_type => Err(QueryError::InvalidFunctionParameters(format!(
                "Query2 support for parsing values is limited, does not support parsing {:?}",
                invalid_type
            ))),
        }
    }
}

impl TryFrom<&DataType> for Vec<Value> {
    type Error = QueryError;
    fn try_from(value: &DataType) -> Result<Self, Self::Error> {
        let mut tagged_values: Vec<DataType> = value.try_into()?;
        let mut values: Vec<Value> = Vec::new();
        for value in tagged_values.drain(..) {
            values.push((&value).try_into()?);
        }
        Ok(values)
    }
}

impl TryFrom<&DataType> for Rule {
    type Error = QueryError;

    fn try_from(data: &DataType) -> Result<Self, Self::Error> {
        let obj = match data {
            DataType::Dict(dict) => dict,
            _ => {
                return Err(QueryError::InvalidFunctionParameters(format!(
                    "Expected rule dict, got {:?}",
                    data
                )))
            }
        };
        let rtype_val = match obj.get("type") {
            Some(rtype) => rtype,
            None => {
                return Err(QueryError::InvalidFunctionParameters(
                    "rule does not have a type".to_string(),
                ))
            }
        };
        let rtype = match rtype_val {
            DataType::String(s) => s,
            _ => {
                return Err(QueryError::InvalidFunctionParameters(
                    "rule type is not a string".to_string(),
                ))
            }
        };
        if rtype == "none" {
            Ok(Self::None)
        } else if rtype == "regex" {
            let regex_val = match obj.get("regex") {
                Some(regex_val) => regex_val,
                None => {
                    return Err(QueryError::InvalidFunctionParameters(
                        "regex rule is missing the 'regex' field".to_string(),
                    ))
                }
            };
            let regex_str = match regex_val {
                DataType::String(s) => s,
                _ => {
                    return Err(QueryError::InvalidFunctionParameters(
                        "the regex field of the regex rule is not a string".to_string(),
                    ))
                }
            };
            let ignore_case_val = match obj.get("ignore_case") {
                Some(case_val) => case_val,
                None => &DataType::Bool(false),
            };
            let ignore_case = match ignore_case_val {
                DataType::Bool(b) => b,
                _ => {
                    return Err(QueryError::InvalidFunctionParameters(
                        "the ignore_case field of the regex rule is not a bool".to_string(),
                    ))
                }
            };
            let regex_rule = match RegexRule::new(regex_str, *ignore_case) {
                Ok(regex_rule) => regex_rule,
                Err(err) => {
                    return Err(QueryError::RegexCompileError(format!(
                        "Failed to compile regex string '{}': '{:?}",
                        regex_str, err
                    )))
                }
            };
            Ok(Self::Regex(regex_rule))
        } else {
            Err(QueryError::InvalidFunctionParameters(format!(
                "Unknown rule type '{}'",
                rtype
            )))
        }
    }
}