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
use std::fmt::{self, Display, Formatter};
use std::time::Duration;

use chrono::{DateTime, FixedOffset};
use itertools::Itertools;
use pest::error::Error;
use pest::iterators::{Pair, Pairs};
use pest::Parser;
use pest_derive::Parser;

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

impl FilterParser {
    pub fn parse_str(input: &str) -> Result<Filter, Error<Rule>> {
        let mut pairs: Pairs<Rule> = Self::parse(Rule::filter, input)?;

        match pairs.next() {
            None => Ok(Filter::None),

            Some(pair) => Ok(Filter::Some(Expression::parse(pair).unwrap())),
        }
    }
}

pub type Filter<'a> = Option<Expression<'a>>;

#[derive(Debug)]
pub enum Value<'a> {
    Bool(bool),
    Duration(Duration),
    Float(f64),
    Int(i64),
    String(&'a str),
    Text(&'a str),
    Timestamp(DateTime<FixedOffset>),
}

impl<'a> Value<'a> {
    pub fn parse(pair: Pair<'a, Rule>) -> Result<Self, ()> {
        debug_assert_eq!(pair.as_rule(), Rule::value);

        let inner_pair = pair.into_inner().next().unwrap();

        match inner_pair.as_rule() {
            Rule::string => {
                let str = inner_pair.into_inner().as_str();

                if let Ok(value) = DateTime::parse_from_rfc3339(str) {
                    return Ok(Value::Timestamp(value));
                }

                return Ok(Value::String(str))
            }

            Rule::text => {
                let str = inner_pair.as_str();

                if let Ok(value) = str.parse() {
                    return Ok(Value::Bool(value));
                }

                if str.ends_with("s") {
                    if let Ok(secs) = str[..str.len() - 1].parse() {
                        return Ok(Value::Duration(Duration::from_secs_f64(secs)))
                    }
                }

                if let Ok(value) = str.parse() {
                    return Ok(Value::Int(value));
                }

                if let Ok(value) = str.parse() {
                    return Ok(Value::Float(value));
                }

                if let Ok(value) = DateTime::parse_from_rfc3339(str) {
                    return Ok(Value::Timestamp(value));
                }

                Ok(Value::Text(str))
            }

            _ => unreachable!()
        }
    }

    pub fn string_repr(&self) -> String {
        match self {
            Self::Bool(value) => value.to_string(),
            Self::Duration(value) => format!("{}s", value.as_secs_f64()),
            Self::Float(value) => value.to_string(),
            Self::Int(value) => value.to_string(),
            Self::String(value) => value.to_string(),
            Self::Text(value) => value.to_string(),
            Self::Timestamp(value) => value.to_rfc3339(),
        }
    }
}

impl Display for Value<'_> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::Bool(value) => write!(f, "{}", value),
            Self::Duration(value) => write!(f, "{}s", value.as_secs_f64()),
            Self::Float(value) => write!(f, "{}", value),
            Self::Int(value) => write!(f, "{}", value),
            Self::String(value) => write!(f, "\"{}\"", value),
            Self::Text(value) => write!(f, "{}", value),
            Self::Timestamp(value) => write!(f, "\"{}\"", value.to_rfc3339()),
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Comparator {
    Eq,
    Gt,
    GtEq,
    Has,
    Lt,
    LtEq,
    Ne,
}

impl Comparator {
    pub fn parse(pair: Pair<Rule>) -> Result<Self, ()> {
        debug_assert_eq!(pair.as_rule(), Rule::comparator);

        let inner_pair = pair.into_inner().next().unwrap();

        Ok(match inner_pair.as_rule() {
            Rule::eq => Self::Eq,
            Rule::gt => Self::Gt,
            Rule::gt_eq => Self::GtEq,
            Rule::has => Self::Has,
            Rule::lt => Self::Lt,
            Rule::lt_eq => Self::LtEq,
            Rule::ne => Self::Ne,
            _ => return Err(()),
        })
    }
}

impl Display for Comparator {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Eq => " = ",
                Self::Gt => " > ",
                Self::GtEq => " >= ",
                Self::Has => ":",
                Self::Lt => " < ",
                Self::LtEq => " <= ",
                Self::Ne => " != ",
            }
        )
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BinOp {
    And,
    Or,
}

impl Display for BinOp {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::And => " AND ",
                Self::Or => " OR ",
            }
        )
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum UnOp {
    Neg,
    Not,
}

impl Display for UnOp {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Neg => "-",
                Self::Not => "NOT ",
            }
        )
    }
}

#[derive(Debug)]
pub enum Expression<'a> {
    Binary {
        lhs: Box<Expression<'a>>,
        op: BinOp,
        rhs: Box<Expression<'a>>,
    },

    FCall {
        name: &'a str,
        args: Vec<Expression<'a>>,
    },

    Member {
        value: Value<'a>,
        path: Vec<Value<'a>>,
    },

    Restriction {
        lhs: Box<Expression<'a>>,
        op: Comparator,
        rhs: Box<Expression<'a>>,
    },

    Sequence(Vec<Expression<'a>>),

    Unary {
        op: UnOp,
        rhs: Box<Expression<'a>>,
    },

    Value(Value<'a>),
}

impl<'a> Expression<'a> {
    pub fn parse(pair: Pair<'a, Rule>) -> Result<Self, ()> {
        debug_assert_eq!(pair.as_rule(), Rule::expression);

        let mut inner = pair.into_inner();

        let mut expr = Expression::parse_impl(inner.next().unwrap())?;

        while let Some(rhs_pair) = inner.next() {
            let rhs = Expression::parse_impl(rhs_pair)?;

            expr = Expression::Binary {
                lhs: Box::new(expr),
                op: BinOp::And,
                rhs: Box::new(rhs),
            }
        }

        Ok(expr)
    }

    fn parse_impl(pair: Pair<'a, Rule>) -> Result<Self, ()> {
        match pair.as_rule() {
            Rule::expression => Self::parse(pair),

            Rule::factor => {
                let mut inner = pair.into_inner();

                let mut expr = Expression::parse_impl(inner.next().unwrap())?;

                while let Some(rhs_pair) = inner.next() {
                    let rhs = Expression::parse_impl(rhs_pair)?;

                    expr = Expression::Binary {
                        lhs: Box::new(expr),
                        op: BinOp::Or,
                        rhs: Box::new(rhs),
                    };
                }

                Ok(expr)
            }

            Rule::sequence => {
                let mut inner = pair.into_inner();

                match inner.clone().count() {
                    0 => Err(()),

                    1 => Expression::parse_impl(inner.next().unwrap()),

                    _ => {
                        let exprs = inner
                            .map(|pair| Expression::parse_impl(pair))
                            .try_collect()?;

                        Ok(Expression::Sequence(exprs))
                    }
                }
            }

            Rule::term => {
                let mut inner = pair.into_inner();

                match inner.next().unwrap() {
                    inner_pair if inner_pair.as_rule() == Rule::negation => {
                        let op = match inner_pair.as_str() {
                            "-" => UnOp::Neg,
                            "NOT" => UnOp::Not,
                            _ => unreachable!(),
                        };

                        let term_pair = inner.next().unwrap();

                        let rhs = Expression::parse_impl(term_pair)?;

                        Ok(Expression::Unary {
                            op,
                            rhs: Box::new(rhs),
                        })
                    }

                    term_pair => Ok(Expression::parse_impl(term_pair)?),
                }
            }

            Rule::restriction => {
                let mut inner = pair.into_inner();

                let lhs = Expression::parse_impl(inner.next().unwrap())?;

                match inner.next() {
                    None => Ok(lhs),

                    Some(comparator_pair) => {
                        let op = Comparator::parse(comparator_pair)?;

                        let rhs = Expression::parse_impl(inner.next().unwrap())?;

                        Ok(Expression::Restriction {
                            lhs: Box::new(lhs),
                            op,
                            rhs: Box::new(rhs),
                        })
                    }
                }
            }

            Rule::member => {
                let mut inner = pair.into_inner();

                let value = Value::parse(inner.next().unwrap())?;

                match inner.peek() {
                    None => Ok(Expression::Value(value)),

                    Some(_) => {
                        let mut path = Vec::new();

                        for field_pair in inner {
                            // Should always be equal when coming from Pest
                            debug_assert_eq!(field_pair.as_rule(), Rule::field);

                            let inner_pair = field_pair.into_inner().next().unwrap();

                            path.push(parse_field(inner_pair)?);
                        }

                        Ok(Expression::Member { value, path })
                    }
                }
            }

            Rule::function => {
                let mut inner = pair.into_inner();

                let name = {
                    let name_pair = inner.next().unwrap();

                    debug_assert_eq!(name_pair.as_rule(), Rule::function_name);

                    name_pair.as_str()
                };

                let args = inner
                    .next()
                    .map(|arg_list_pair| {
                        debug_assert_eq!(arg_list_pair.as_rule(), Rule::arg_list);

                        arg_list_pair
                            .into_inner()
                            .map(|pair| Expression::parse_impl(pair))
                            .try_collect()
                    })
                    .unwrap_or_else(|| Ok(Vec::new()))?;

                Ok(Expression::FCall { name, args })
            }

            Rule::value => {
                let inner_pair = pair.into_inner().next().unwrap();

                Ok(Expression::Value(Value::parse(inner_pair)?))
            }

            _ => unimplemented!("{:?}", pair),
        }
    }
}

impl Display for Expression<'_> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::Binary { lhs, op, rhs } => write!(f, "({}{}{})", lhs, op, rhs),

            Self::FCall { name, args } => {
                let args_str = args
                    .iter()
                    .map(ToString::to_string)
                    .collect::<Vec<String>>()
                    .join(", ");

                write!(f, "({}({}))", name, args_str)
            }

            Self::Member { value, path } => {
                let path_str = path
                    .iter()
                    .map(ToString::to_string)
                    .collect::<Vec<String>>()
                    .join(".");

                write!(f, "({}.{})", value, path_str)
            }

            Self::Restriction { lhs, op, rhs } => write!(f, "({}{}{})", lhs, op, rhs),

            Self::Sequence(expressions) => {
                let joined_str = expressions
                    .iter()
                    .map(|expr| format!("{}", expr))
                    .collect::<Vec<String>>()
                    .join(" ");

                write!(f, "({})", joined_str)
            }

            Self::Unary { op, rhs } => write!(f, "({}{})", op, rhs),

            Self::Value(value) => value.fmt(f),
        }
    }
}

fn parse_field(pair: Pair<Rule>) -> Result<Value, ()> {
    match pair.as_rule() {
        Rule::value => Value::parse(pair),
        Rule::keyword => Ok(Value::Text(pair.as_str())),
        _ => Err(()),
    }
}