atc_router/
ast.rs

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
use crate::schema::Schema;
use cidr::IpCidr;
use regex::Regex;
use std::net::IpAddr;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub enum Expression {
    Logical(Box<LogicalExpression>),
    Predicate(Predicate),
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub enum LogicalExpression {
    And(Expression, Expression),
    Or(Expression, Expression),
    Not(Expression),
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LhsTransformations {
    Lower,
    Any,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOperator {
    Equals,         // ==
    NotEquals,      // !=
    Regex,          // ~
    Prefix,         // ^=
    Postfix,        // =^
    Greater,        // >
    GreaterOrEqual, // >=
    Less,           // <
    LessOrEqual,    // <=
    In,             // in
    NotIn,          // not in
    Contains,       // contains
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub enum Value {
    String(String),
    IpCidr(IpCidr),
    IpAddr(IpAddr),
    Int(i64),
    #[cfg_attr(feature = "serde", serde(with = "serde_regex"))]
    Regex(Regex),
}

impl PartialEq for Value {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Regex(_), _) | (_, Self::Regex(_)) => {
                panic!("Regexes can not be compared using eq")
            }
            (Self::String(s1), Self::String(s2)) => s1 == s2,
            (Self::IpCidr(i1), Self::IpCidr(i2)) => i1 == i2,
            (Self::IpAddr(i1), Self::IpAddr(i2)) => i1 == i2,
            (Self::Int(i1), Self::Int(i2)) => i1 == i2,
            _ => false,
        }
    }
}

impl Value {
    pub fn my_type(&self) -> Type {
        match self {
            Value::String(_) => Type::String,
            Value::IpCidr(_) => Type::IpCidr,
            Value::IpAddr(_) => Type::IpAddr,
            Value::Int(_) => Type::Int,
            Value::Regex(_) => Type::Regex,
        }
    }
}

impl From<String> for Value {
    fn from(v: String) -> Self {
        Value::String(v)
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Eq, PartialEq)]
#[repr(C)]
pub enum Type {
    String,
    IpCidr,
    IpAddr,
    Int,
    Regex,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct Lhs {
    pub var_name: String,
    pub transformations: Vec<LhsTransformations>,
}

impl Lhs {
    pub fn my_type<'a>(&self, schema: &'a Schema) -> Option<&'a Type> {
        schema.type_of(&self.var_name)
    }

    pub fn get_transformations(&self) -> (bool, bool) {
        let mut lower = false;
        let mut any = false;

        self.transformations.iter().for_each(|i| match i {
            LhsTransformations::Any => any = true,
            LhsTransformations::Lower => lower = true,
        });

        (lower, any)
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct Predicate {
    pub lhs: Lhs,
    pub rhs: Value,
    pub op: BinaryOperator,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::parse;
    use std::fmt;

    impl fmt::Display for Expression {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(
                f,
                "{}",
                match self {
                    Expression::Logical(logical) => logical.to_string(),
                    Expression::Predicate(predicate) => predicate.to_string(),
                }
            )
        }
    }

    impl fmt::Display for LogicalExpression {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(
                f,
                "{}",
                match self {
                    LogicalExpression::And(left, right) => {
                        format!("({} && {})", left, right)
                    }
                    LogicalExpression::Or(left, right) => {
                        format!("({} || {})", left, right)
                    }
                    LogicalExpression::Not(e) => {
                        format!("!({})", e)
                    }
                }
            )
        }
    }

    impl fmt::Display for LhsTransformations {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(
                f,
                "{}",
                match self {
                    LhsTransformations::Lower => "lower".to_string(),
                    LhsTransformations::Any => "any".to_string(),
                }
            )
        }
    }

    impl fmt::Display for Value {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            match self {
                Value::String(s) => write!(f, "\"{}\"", s),
                Value::IpCidr(cidr) => write!(f, "{}", cidr),
                Value::IpAddr(addr) => write!(f, "{}", addr),
                Value::Int(i) => write!(f, "{}", i),
                Value::Regex(re) => write!(f, "\"{}\"", re),
            }
        }
    }

    impl fmt::Display for Lhs {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            let mut s = self.var_name.to_string();
            for transformation in &self.transformations {
                s = format!("{}({})", transformation, s);
            }
            write!(f, "{}", s)
        }
    }

    impl fmt::Display for BinaryOperator {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            use BinaryOperator::*;

            write!(
                f,
                "{}",
                match self {
                    Equals => "==",
                    NotEquals => "!=",
                    Regex => "~",
                    Prefix => "^=",
                    Postfix => "=^",
                    Greater => ">",
                    GreaterOrEqual => ">=",
                    Less => "<",
                    LessOrEqual => "<=",
                    In => "in",
                    NotIn => "not in",
                    Contains => "contains",
                }
            )
        }
    }

    impl fmt::Display for Predicate {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "({} {} {})", self.lhs, self.op, self.rhs)
        }
    }

    #[test]
    fn expr_op_and_prec() {
        let tests = vec![
            ("a > 0", "(a > 0)"),
            ("a in \"abc\"", "(a in \"abc\")"),
            ("a == 1 && b != 2", "((a == 1) && (b != 2))"),
            (
                "a ^= \"1\" && b =^ \"2\" || c >= 3",
                "((a ^= \"1\") && ((b =^ \"2\") || (c >= 3)))",
            ),
            (
                "a == 1 && b != 2 || c >= 3",
                "((a == 1) && ((b != 2) || (c >= 3)))",
            ),
            (
                "a > 1 || b < 2 && c <= 3 || d not in \"foo\"",
                "(((a > 1) || (b < 2)) && ((c <= 3) || (d not in \"foo\")))",
            ),
            (
                "a > 1 || ((b < 2) && (c <= 3)) || d not in \"foo\"",
                "(((a > 1) || ((b < 2) && (c <= 3))) || (d not in \"foo\"))",
            ),
            ("!(a == 1)", "!((a == 1))"),
            (
                "!(a == 1) && b == 2 && !(c == 3) && d >= 4",
                "(((!((a == 1)) && (b == 2)) && !((c == 3))) && (d >= 4))",
            ),
            (
                "!(a == 1 || b == 2 && c == 3) && d == 4",
                "(!((((a == 1) || (b == 2)) && (c == 3))) && (d == 4))",
            ),
        ];
        for (input, expected) in tests {
            let result = parse(input).unwrap();
            assert_eq!(result.to_string(), expected);
        }
    }

    #[test]
    fn expr_var_name_and_ip() {
        let tests = vec![
            // ipv4_literal
            ("kong.foo in 1.1.1.1", "(kong.foo in 1.1.1.1)"),
            // ipv4_cidr_literal
            (
                "kong.foo.foo2 in 10.0.0.0/24",
                "(kong.foo.foo2 in 10.0.0.0/24)",
            ),
            // ipv6_literal
            (
                "kong.foo.foo3 in 2001:db8::/32",
                "(kong.foo.foo3 in 2001:db8::/32)",
            ),
            // ipv6_cidr_literal
            (
                "kong.foo.foo4 in 2001:db8::/32",
                "(kong.foo.foo4 in 2001:db8::/32)",
            ),
        ];
        for (input, expected) in tests {
            let result = parse(input).unwrap();
            assert_eq!(result.to_string(), expected);
        }
    }

    #[test]
    fn expr_regex() {
        let tests = vec![
            // regex_literal
            (
                "kong.foo.foo5 ~ \"^foo.*$\"",
                "(kong.foo.foo5 ~ \"^foo.*$\")",
            ),
            // regex_literal
            (
                "kong.foo.foo6 ~ \"^foo.*$\"",
                "(kong.foo.foo6 ~ \"^foo.*$\")",
            ),
        ];
        for (input, expected) in tests {
            let result = parse(input).unwrap();
            assert_eq!(result.to_string(), expected);
        }
    }

    #[test]
    fn expr_digits() {
        let tests = vec![
            // dec literal
            ("kong.foo.foo7 == 123", "(kong.foo.foo7 == 123)"),
            // hex literal
            ("kong.foo.foo8 == 0x123", "(kong.foo.foo8 == 291)"),
            // oct literal
            ("kong.foo.foo9 == 0123", "(kong.foo.foo9 == 83)"),
            // dec negative literal
            ("kong.foo.foo10 == -123", "(kong.foo.foo10 == -123)"),
            // hex negative literal
            ("kong.foo.foo11 == -0x123", "(kong.foo.foo11 == -291)"),
            // oct negative literal
            ("kong.foo.foo12 == -0123", "(kong.foo.foo12 == -83)"),
        ];
        for (input, expected) in tests {
            let result = parse(input).unwrap();
            assert_eq!(result.to_string(), expected);
        }
    }

    #[test]
    fn expr_transformations() {
        let tests = vec![
            // lower
            (
                "lower(kong.foo.foo13) == \"foo\"",
                "(lower(kong.foo.foo13) == \"foo\")",
            ),
            // any
            (
                "any(kong.foo.foo14) == \"foo\"",
                "(any(kong.foo.foo14) == \"foo\")",
            ),
        ];
        for (input, expected) in tests {
            let result = parse(input).unwrap();
            assert_eq!(result.to_string(), expected);
        }
    }

    #[test]
    fn expr_transformations_nested() {
        let tests = vec![
            // lower + lower
            (
                "lower(lower(kong.foo.foo15)) == \"foo\"",
                "(lower(lower(kong.foo.foo15)) == \"foo\")",
            ),
            // lower + any
            (
                "lower(any(kong.foo.foo16)) == \"foo\"",
                "(lower(any(kong.foo.foo16)) == \"foo\")",
            ),
            // any + lower
            (
                "any(lower(kong.foo.foo17)) == \"foo\"",
                "(any(lower(kong.foo.foo17)) == \"foo\")",
            ),
            // any + any
            (
                "any(any(kong.foo.foo18)) == \"foo\"",
                "(any(any(kong.foo.foo18)) == \"foo\")",
            ),
        ];
        for (input, expected) in tests {
            let result = parse(input).unwrap();
            assert_eq!(result.to_string(), expected);
        }
    }

    #[test]
    fn str_unicode_test() {
        let tests = vec![
            // cjk chars
            ("t_msg in \"你好\"", "(t_msg in \"你好\")"),
            // 0xXXX unicode
            ("t_msg in \"\u{4f60}\u{597d}\"", "(t_msg in \"你好\")"),
        ];
        for (input, expected) in tests {
            let result = parse(input).unwrap();
            assert_eq!(result.to_string(), expected);
        }
    }

    #[test]
    fn rawstr_test() {
        let tests = vec![
            // invalid escape sequence
            (r##"a == r#"/path/to/\d+"#"##, r#"(a == "/path/to/\d+")"#),
            // valid escape sequence
            (r##"a == r#"/path/to/\n+"#"##, r#"(a == "/path/to/\n+")"#),
        ];
        for (input, expected) in tests {
            let result = parse(input).unwrap();
            assert_eq!(result.to_string(), expected);
        }
    }
}