flp_gsp/
lib.rs

1#![forbid(unsafe_code)]
2
3pub mod interpreter;
4mod parser;
5
6use parser::comparison::Comparison;
7use parser::relation::Relation;
8
9#[derive(Debug)]
10pub enum Node {
11    And(Box<Expression>, Box<Expression>),
12    Or(Box<Expression>, Box<Expression>),
13    Not(Box<Expression>),
14    Equal(String, String),
15    EqualCI(String, String),
16    Greater(String, String),
17    Less(String, String),
18    Wildcard(String, String),
19    Regex(String, String),
20    Any(String, Vec<String>),
21    Null(String),
22}
23
24#[derive(Debug)]
25pub struct Expression {
26    pub node: Node,
27}
28
29impl From<Comparison> for Expression {
30    fn from(c: Comparison) -> Self {
31        match c {
32            Comparison::IsEqual(c) => Self {
33                node: Node::Equal(c.left.0, c.right.0),
34            },
35            Comparison::IsEqualCI(c) => Self {
36                node: Node::EqualCI(c.left.0, c.right.0),
37            },
38            Comparison::IsGreater(c) => Self {
39                node: Node::Greater(c.left.0, c.right.0),
40            },
41            Comparison::IsLess(c) => Self {
42                node: Node::Less(c.left.0, c.right.0),
43            },
44            Comparison::IsWildcard(c) => Self {
45                node: Node::Wildcard(c.left.0, c.right.0),
46            },
47            Comparison::IsRegex(c) => Self {
48                node: Node::Regex(c.left.0, c.right.0),
49            },
50            Comparison::IsAny(c) => Self {
51                node: Node::Any(c.left.0, c.right.0),
52            },
53            Comparison::IsNull(c) => Self {
54                node: Node::Null(c.0.0),
55            },
56        }
57    }
58}
59
60impl From<Box<Relation>> for Expression {
61    fn from(relation: Box<Relation>) -> Self {
62        match *relation {
63            Relation::C(c) => c.into(),
64            Relation::Rar { left, right } => Self {
65                node: Node::And(Box::new(left.into()), Box::new(right.into())),
66            },
67            Relation::Rac { left, right } => Self {
68                node: Node::And(Box::new(left.into()), Box::new(right.into())),
69            },
70            Relation::Car { left, right } => Self {
71                node: Node::And(Box::new(left.into()), Box::new(right.into())),
72            },
73            Relation::Cac { left, right } => Self {
74                node: Node::And(Box::new(left.into()), Box::new(right.into())),
75            },
76            Relation::Ror { left, right } => Self {
77                node: Node::Or(Box::new(left.into()), Box::new(right.into())),
78            },
79            Relation::Roc { left, right } => Self {
80                node: Node::Or(Box::new(left.into()), Box::new(right.into())),
81            },
82            Relation::Cor { left, right } => Self {
83                node: Node::Or(Box::new(left.into()), Box::new(right.into())),
84            },
85            Relation::Coc { left, right } => Self {
86                node: Node::Or(Box::new(left.into()), Box::new(right.into())),
87            },
88            Relation::NR(r) => Self {
89                node: Node::Not(Box::new(r.into())),
90            },
91            Relation::NC(c) => Self {
92                node: Node::Not(Box::new(c.into())),
93            },
94        }
95    }
96}
97
98impl Expression {
99    pub fn try_from_str(s: &str) -> Result<Self, String> {
100        Ok(parser::relation::relation(s)
101            .map_err(|err| err.to_string())?
102            .1
103            .into())
104    }
105}