spacetimedb_lib/
operator.rs

1//! Operator support for the query macro.
2
3use derive_more::From;
4use spacetimedb_lib::de::Deserialize;
5use spacetimedb_lib::ser::Serialize;
6use std::fmt;
7
8#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
9pub enum OpCmp {
10    Eq,
11    NotEq,
12    Lt,
13    LtEq,
14    Gt,
15    GtEq,
16}
17
18impl From<OpCmp> for &str {
19    fn from(x: OpCmp) -> Self {
20        match x {
21            OpCmp::Eq => "std::cmp::eq",
22            OpCmp::NotEq => "std::cmp::neq",
23            OpCmp::Lt => "std::cmp::lt",
24            OpCmp::LtEq => "std::cmp::le",
25            OpCmp::Gt => "std::cmp::gt",
26            OpCmp::GtEq => "std::cmp::ge",
27        }
28    }
29}
30
31impl OpCmp {
32    /// Reverse the order of the `cmp`, to helps in reducing the cases on evaluation, ie:
33    pub fn reverse(self) -> Self {
34        match self {
35            OpCmp::Eq => self,
36            OpCmp::NotEq => self,
37            OpCmp::Lt => OpCmp::Gt,
38            OpCmp::LtEq => OpCmp::GtEq,
39            OpCmp::Gt => OpCmp::Lt,
40            OpCmp::GtEq => OpCmp::LtEq,
41        }
42    }
43}
44
45#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
46pub enum OpUnary {
47    Not,
48}
49
50impl From<OpUnary> for &str {
51    fn from(x: OpUnary) -> Self {
52        match x {
53            OpUnary::Not => "std::ops::not",
54        }
55    }
56}
57
58#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
59pub enum OpMath {
60    Add,
61    Minus,
62    Mul,
63    Div,
64}
65
66impl From<OpMath> for &str {
67    fn from(x: OpMath) -> Self {
68        match x {
69            OpMath::Add => "std::math::add",
70            OpMath::Minus => "std::math::minus",
71            OpMath::Mul => "std::math::mul",
72            OpMath::Div => "std::math::div",
73        }
74    }
75}
76
77#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
78pub enum OpLogic {
79    And,
80    Or,
81}
82
83impl From<OpLogic> for &str {
84    fn from(x: OpLogic) -> Self {
85        match x {
86            OpLogic::And => "std::ops::and",
87            OpLogic::Or => "std::ops::or",
88        }
89    }
90}
91
92#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, From)]
93pub enum OpQuery {
94    Cmp(OpCmp),
95    Logic(OpLogic),
96}
97
98#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, From)]
99pub enum Op {
100    Cmp(OpCmp),
101    Logic(OpLogic),
102    Unary(OpUnary),
103    Math(OpMath),
104}
105
106impl Op {
107    pub fn is_logical(&self) -> bool {
108        matches!(self, Op::Cmp(_) | Op::Logic(_))
109    }
110}
111
112impl fmt::Display for OpCmp {
113    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114        let x = match self {
115            OpCmp::Eq => "==",
116            OpCmp::NotEq => "!=",
117            OpCmp::Lt => "<",
118            OpCmp::LtEq => "<=",
119            OpCmp::Gt => ">",
120            OpCmp::GtEq => ">=",
121        };
122        write!(f, "{x}")
123    }
124}
125
126impl fmt::Display for OpLogic {
127    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128        let x = match self {
129            OpLogic::And => "and",
130            OpLogic::Or => "or",
131        };
132        write!(f, "{x}")
133    }
134}
135
136impl fmt::Display for OpUnary {
137    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138        let x = match self {
139            OpUnary::Not => "not",
140        };
141        write!(f, "{x}")
142    }
143}
144
145impl fmt::Display for OpMath {
146    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147        let x = match self {
148            OpMath::Add => "+",
149            OpMath::Minus => "-",
150            OpMath::Mul => "*",
151            OpMath::Div => "/",
152        };
153        write!(f, "{x}")
154    }
155}
156
157impl fmt::Display for Op {
158    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
159        match self {
160            Op::Cmp(x) => {
161                write!(f, "{x}")
162            }
163            Op::Logic(x) => {
164                write!(f, "{x}")
165            }
166            Op::Unary(x) => {
167                write!(f, "{x}")
168            }
169            Op::Math(x) => {
170                write!(f, "{x}")
171            }
172        }
173    }
174}
175
176impl fmt::Display for OpQuery {
177    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178        match self {
179            OpQuery::Cmp(x) => {
180                write!(f, "{x}")
181            }
182            OpQuery::Logic(x) => {
183                write!(f, "{x}")
184            }
185        }
186    }
187}