spacetimedb_lib/
operator.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
//! Operators are implemented as "alias" of functions that are loaded
//! at the start of the [ProgramVm] creation, ie:
//!
//! `+` == std::math::add
//!
use derive_more::From;
use spacetimedb_lib::de::Deserialize;
use spacetimedb_lib::ser::Serialize;
use std::fmt;

#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum OpCmp {
    Eq,
    NotEq,
    Lt,
    LtEq,
    Gt,
    GtEq,
}

impl From<OpCmp> for &str {
    fn from(x: OpCmp) -> Self {
        match x {
            OpCmp::Eq => "std::cmp::eq",
            OpCmp::NotEq => "std::cmp::neq",
            OpCmp::Lt => "std::cmp::lt",
            OpCmp::LtEq => "std::cmp::le",
            OpCmp::Gt => "std::cmp::gt",
            OpCmp::GtEq => "std::cmp::ge",
        }
    }
}

impl OpCmp {
    /// Reverse the order of the `cmp`, to helps in reducing the cases on evaluation, ie:
    pub fn reverse(self) -> Self {
        match self {
            OpCmp::Eq => self,
            OpCmp::NotEq => self,
            OpCmp::Lt => OpCmp::Gt,
            OpCmp::LtEq => OpCmp::GtEq,
            OpCmp::Gt => OpCmp::Lt,
            OpCmp::GtEq => OpCmp::LtEq,
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum OpUnary {
    Not,
}

impl From<OpUnary> for &str {
    fn from(x: OpUnary) -> Self {
        match x {
            OpUnary::Not => "std::ops::not",
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum OpMath {
    Add,
    Minus,
    Mul,
    Div,
}

impl From<OpMath> for &str {
    fn from(x: OpMath) -> Self {
        match x {
            OpMath::Add => "std::math::add",
            OpMath::Minus => "std::math::minus",
            OpMath::Mul => "std::math::mul",
            OpMath::Div => "std::math::div",
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum OpLogic {
    And,
    Or,
}

impl From<OpLogic> for &str {
    fn from(x: OpLogic) -> Self {
        match x {
            OpLogic::And => "std::ops::and",
            OpLogic::Or => "std::ops::or",
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, From)]
pub enum OpQuery {
    Cmp(OpCmp),
    Logic(OpLogic),
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, From)]
pub enum Op {
    Cmp(OpCmp),
    Logic(OpLogic),
    Unary(OpUnary),
    Math(OpMath),
}

impl Op {
    pub fn is_logical(&self) -> bool {
        matches!(self, Op::Cmp(_) | Op::Logic(_))
    }
}

impl fmt::Display for OpCmp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let x = match self {
            OpCmp::Eq => "==",
            OpCmp::NotEq => "!=",
            OpCmp::Lt => "<",
            OpCmp::LtEq => "<=",
            OpCmp::Gt => ">",
            OpCmp::GtEq => ">=",
        };
        write!(f, "{x}")
    }
}

impl fmt::Display for OpLogic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let x = match self {
            OpLogic::And => "and",
            OpLogic::Or => "or",
        };
        write!(f, "{x}")
    }
}

impl fmt::Display for OpUnary {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let x = match self {
            OpUnary::Not => "not",
        };
        write!(f, "{x}")
    }
}

impl fmt::Display for OpMath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let x = match self {
            OpMath::Add => "+",
            OpMath::Minus => "-",
            OpMath::Mul => "*",
            OpMath::Div => "/",
        };
        write!(f, "{x}")
    }
}

impl fmt::Display for Op {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Op::Cmp(x) => {
                write!(f, "{x}")
            }
            Op::Logic(x) => {
                write!(f, "{x}")
            }
            Op::Unary(x) => {
                write!(f, "{x}")
            }
            Op::Math(x) => {
                write!(f, "{x}")
            }
        }
    }
}

impl fmt::Display for OpQuery {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OpQuery::Cmp(x) => {
                write!(f, "{x}")
            }
            OpQuery::Logic(x) => {
                write!(f, "{x}")
            }
        }
    }
}