models_parser/ast/
operator.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13use core::fmt;
14
15#[cfg(feature = "serde")]
16use serde::{Deserialize, Serialize};
17
18/// Unary operators
19#[derive(Debug, Clone, PartialEq, Eq, Hash)]
20#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
21pub enum UnaryOperator {
22    Plus,
23    Minus,
24    Not,
25    /// Bitwise Not, e.g. `~9` (PostgreSQL-specific)
26    PGBitwiseNot,
27    /// Square root, e.g. `|/9` (PostgreSQL-specific)
28    PGSquareRoot,
29    /// Cube root, e.g. `||/27` (PostgreSQL-specific)
30    PGCubeRoot,
31    /// Factorial, e.g. `9!` (PostgreSQL-specific)
32    PGPostfixFactorial,
33    /// Factorial, e.g. `!!9` (PostgreSQL-specific)
34    PGPrefixFactorial,
35    /// Absolute value, e.g. `@ -9` (PostgreSQL-specific)
36    PGAbs,
37}
38
39impl fmt::Display for UnaryOperator {
40    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41        f.write_str(match self {
42            UnaryOperator::Plus => "+",
43            UnaryOperator::Minus => "-",
44            UnaryOperator::Not => "NOT",
45            UnaryOperator::PGBitwiseNot => "~",
46            UnaryOperator::PGSquareRoot => "|/",
47            UnaryOperator::PGCubeRoot => "||/",
48            UnaryOperator::PGPostfixFactorial => "!",
49            UnaryOperator::PGPrefixFactorial => "!!",
50            UnaryOperator::PGAbs => "@",
51        })
52    }
53}
54
55/// Binary operators
56#[derive(Debug, Clone, PartialEq, Eq, Hash)]
57#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
58pub enum BinaryOperator {
59    Plus,
60    Minus,
61    Multiply,
62    Divide,
63    Modulo,
64    StringConcat,
65    Gt,
66    Lt,
67    GtEq,
68    LtEq,
69    Spaceship,
70    Eq,
71    NotEq,
72    And,
73    Or,
74    Like,
75    NotLike,
76    ILike,
77    NotILike,
78    BitwiseOr,
79    BitwiseAnd,
80    BitwiseXor,
81    PGBitwiseXor,
82    PGBitwiseShiftLeft,
83    PGBitwiseShiftRight,
84    PGRegexMatch,
85    PGRegexIMatch,
86    PGRegexNotMatch,
87    PGRegexNotIMatch,
88}
89
90impl fmt::Display for BinaryOperator {
91    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92        f.write_str(match self {
93            BinaryOperator::Plus => "+",
94            BinaryOperator::Minus => "-",
95            BinaryOperator::Multiply => "*",
96            BinaryOperator::Divide => "/",
97            BinaryOperator::Modulo => "%",
98            BinaryOperator::StringConcat => "||",
99            BinaryOperator::Gt => ">",
100            BinaryOperator::Lt => "<",
101            BinaryOperator::GtEq => ">=",
102            BinaryOperator::LtEq => "<=",
103            BinaryOperator::Spaceship => "<=>",
104            BinaryOperator::Eq => "=",
105            BinaryOperator::NotEq => "<>",
106            BinaryOperator::And => "AND",
107            BinaryOperator::Or => "OR",
108            BinaryOperator::Like => "LIKE",
109            BinaryOperator::NotLike => "NOT LIKE",
110            BinaryOperator::ILike => "ILIKE",
111            BinaryOperator::NotILike => "NOT ILIKE",
112            BinaryOperator::BitwiseOr => "|",
113            BinaryOperator::BitwiseAnd => "&",
114            BinaryOperator::BitwiseXor => "^",
115            BinaryOperator::PGBitwiseXor => "#",
116            BinaryOperator::PGBitwiseShiftLeft => "<<",
117            BinaryOperator::PGBitwiseShiftRight => ">>",
118            BinaryOperator::PGRegexMatch => "~",
119            BinaryOperator::PGRegexIMatch => "~*",
120            BinaryOperator::PGRegexNotMatch => "!~",
121            BinaryOperator::PGRegexNotIMatch => "!~*",
122        })
123    }
124}