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
use {super::ExprNode, crate::ast::BinaryOperator};

impl<'a> ExprNode<'a> {
    fn binary_op<T: Into<Self>>(self, op: BinaryOperator, other: T) -> Self {
        Self::BinaryOp {
            left: Box::new(self),
            op,
            right: Box::new(other.into()),
        }
    }

    #[allow(clippy::should_implement_trait)]
    pub fn add<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::Plus, other)
    }

    #[allow(clippy::should_implement_trait)]
    pub fn sub<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::Minus, other)
    }

    #[allow(clippy::should_implement_trait)]
    pub fn mul<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::Multiply, other)
    }

    #[allow(clippy::should_implement_trait)]
    pub fn div<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::Divide, other)
    }

    pub fn modulo<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::Modulo, other)
    }

    pub fn concat<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::StringConcat, other)
    }

    pub fn gt<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::Gt, other)
    }

    pub fn lt<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::Lt, other)
    }

    pub fn gte<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::GtEq, other)
    }

    pub fn lte<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::LtEq, other)
    }

    pub fn eq<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::Eq, other)
    }

    pub fn neq<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::NotEq, other)
    }

    pub fn and<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::And, other)
    }

    pub fn or<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::Or, other)
    }

    pub fn bitwise_and<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::BitwiseAnd, other)
    }

    pub fn bitwise_shift_left<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::BitwiseShiftLeft, other)
    }

    pub fn bitwise_shift_right<T: Into<Self>>(self, other: T) -> Self {
        self.binary_op(BinaryOperator::BitwiseShiftRight, other)
    }
}

#[cfg(test)]
mod tests {
    use crate::ast_builder::{col, num, test_expr, text};

    #[test]
    fn binary_op() {
        let actual = col("id").add(10);
        let expected = "id + 10";
        test_expr(actual, expected);

        let actual = num(10).sub(text("abc"));
        let expected = "10 - 'abc'";
        test_expr(actual, expected);

        let actual = col("rate").mul("amount");
        let expected = "rate * amount";
        test_expr(actual, expected);

        let actual = col("amount").div(30);
        let expected = "amount / 30";
        test_expr(actual, expected);

        let actual = col("amount").modulo(30);
        let expected = "amount % 30";
        test_expr(actual, expected);

        let actual = text("hello").concat("'world'");
        let expected = "'hello' || 'world'";
        test_expr(actual, expected);

        let actual = col("id").gt(col("Bar.id"));
        let expected = "id > Bar.id";
        test_expr(actual, expected);

        let actual = col("id").lt(col("Bar.id"));
        let expected = "id < Bar.id";
        test_expr(actual, expected);

        let actual = col("id").gte(col("Bar.id"));
        let expected = "id >= Bar.id";
        test_expr(actual, expected);

        let actual = col("id").lte(col("Bar.id"));
        let expected = "id <= Bar.id";
        test_expr(actual, expected);

        let actual = col("id").eq(10);
        let expected = "id = 10";
        test_expr(actual, expected);

        let actual = col("id").neq("'abcde'");
        let expected = "id != 'abcde'";
        test_expr(actual, expected);

        let actual = (col("id").gt(num(10))).and(col("id").lt(num(20)));
        let expected = "id > 10 AND id < 20";
        test_expr(actual, expected);

        let actual = (col("id").gt(num(10))).or(col("id").lt(num(20)));
        let expected = "id > 10 OR id < 20";
        test_expr(actual, expected);

        let actual = col("id").bitwise_and(col("value"));
        let expected = "id & value";
        test_expr(actual, expected);

        let actual = col("id").bitwise_shift_left(num(1));
        let expected = "id << 1";
        test_expr(actual, expected);

        let actual = col("id").bitwise_shift_right(num(1));
        let expected = "id >> 1";
        test_expr(actual, expected);
    }
}