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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
mod and;
mod attribute_exists;
mod attribute_not_exists;
pub mod attribute_type;
mod begins_with;
mod between;
mod comparison;
mod contains;
mod in_;
mod not;
mod or;
mod parenthetical;

pub use and::And;
pub use attribute_exists::AttributeExists;
pub use attribute_not_exists::AttributeNotExists;
pub use attribute_type::AttributeType;
pub use begins_with::BeginsWith;
pub use between::Between;
pub use comparison::{
    equal, greater_than, greater_than_or_equal, less_than, less_than_or_equal, not_equal,
    Comparator, Comparison,
};
pub use contains::Contains;
pub use in_::In;
pub use not::Not;
pub use or::Or;
pub use parenthetical::Parenthetical;

use core::{fmt, ops};

/// Represents a logical condition in a [DynamoDB expression][1].
///
/// You will usually create these using the methods on [`Path`].
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Syntax
/// [`Path`]: crate::path::Path
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Condition {
    AttributeExists(AttributeExists),
    AttributeNotExists(AttributeNotExists),
    AttributeType(AttributeType),
    BeginsWith(BeginsWith),
    Between(Between),
    Contains(Contains),
    In(In),
    Not(Not),
    And(And),
    Or(Or),
    Comparison(Comparison),
    Parenthetical(Parenthetical),
}

impl Condition {
    /// A [DynamoDB logical `AND`][1] condition.
    ///
    /// ```
    /// use dynamodb_expression::Path;
    /// # use pretty_assertions::assert_eq;
    ///
    /// let a = Path::new_name("a");
    /// let b = Path::new_name("b");
    /// let c = Path::new_name("c");
    /// let d = Path::new_name("d");
    ///
    /// let condition = a.greater_than(b).and(c.less_than(d));
    /// assert_eq!("a > b AND c < d", condition.to_string());
    /// ```
    ///
    /// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.LogicalEvaluations
    pub fn and<R>(self, right: R) -> Self
    where
        R: Into<Condition>,
    {
        Self::And(And {
            left: self.into(),
            right: right.into().into(),
        })
    }

    /// A [DynamoDB logical `OR`][1] condition.
    ///
    /// ```
    /// use dynamodb_expression::Path;
    /// # use pretty_assertions::assert_eq;
    ///
    /// let a = Path::new_name("a");
    /// let b = Path::new_name("b");
    /// let c = Path::new_name("c");
    /// let d = Path::new_name("d");
    ///
    /// let condition = a.greater_than(b).or(c.less_than(d));
    /// assert_eq!("a > b OR c < d", condition.to_string());
    /// ```
    ///
    /// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.LogicalEvaluations
    pub fn or<R>(self, right: R) -> Self
    where
        R: Into<Condition>,
    {
        Self::Or(Or {
            left: self.into(),
            right: right.into().into(),
        })
    }

    /// A [DynamoDB logical `NOT`][1] condition.
    ///
    /// ```
    /// use dynamodb_expression::Path;
    /// # use pretty_assertions::assert_eq;
    ///
    /// let a = Path::new_name("a");
    /// let b = Path::new_name("b");
    ///
    /// let condition = a.greater_than(b).not();
    /// assert_eq!("NOT a > b", condition.to_string());
    /// ```
    ///
    /// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.LogicalEvaluations
    #[allow(clippy::should_implement_trait)]
    pub fn not(self) -> Self {
        Self::Not(Not::from(self))
    }

    /// Wraps a condition in [parentheses][1].
    ///
    /// ```
    /// use dynamodb_expression::Path;
    /// # use pretty_assertions::assert_eq;
    ///
    /// let a = Path::new_name("a");
    /// let b = Path::new_name("b");
    /// let c = Path::new_name("c");
    /// let d = Path::new_name("d");
    ///
    /// let condition = a.greater_than(b).parenthesize().and(c.less_than(d).parenthesize());
    /// assert_eq!("(a > b) AND (c < d)", condition.to_string());
    /// ```
    ///
    /// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Parentheses
    pub fn parenthesize(self) -> Self {
        Self::Parenthetical(self.into())
    }
}

impl ops::Not for Condition {
    type Output = Condition;

    /// A [DynamoDB logical `NOT`][1] condition.
    ///
    /// ```
    /// use dynamodb_expression::Path;
    /// # use pretty_assertions::assert_eq;
    ///
    /// let a = Path::new_name("a");
    /// let b = Path::new_name("b");
    ///
    /// let condition = !a.greater_than(b);
    /// assert_eq!("NOT a > b", condition.to_string());
    /// ```
    ///
    /// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.LogicalEvaluations
    fn not(self) -> Self::Output {
        Condition::not(self)
    }
}

impl fmt::Display for Condition {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Condition::AttributeExists(condition) => condition.fmt(f),
            Condition::AttributeNotExists(condition) => condition.fmt(f),
            Condition::AttributeType(condition) => condition.fmt(f),
            Condition::BeginsWith(condition) => condition.fmt(f),
            Condition::Between(condition) => condition.fmt(f),
            Condition::Contains(condition) => condition.fmt(f),
            Condition::In(condition) => condition.fmt(f),
            Condition::Not(condition) => condition.fmt(f),
            Condition::And(condition) => condition.fmt(f),
            Condition::Or(condition) => condition.fmt(f),
            Condition::Comparison(condition) => condition.fmt(f),
            Condition::Parenthetical(condition) => condition.fmt(f),
        }
    }
}

impl From<AttributeExists> for Condition {
    fn from(condition: AttributeExists) -> Self {
        Self::AttributeExists(condition)
    }
}

impl From<AttributeNotExists> for Condition {
    fn from(condition: AttributeNotExists) -> Self {
        Self::AttributeNotExists(condition)
    }
}

impl From<AttributeType> for Condition {
    fn from(condition: AttributeType) -> Self {
        Self::AttributeType(condition)
    }
}

impl From<BeginsWith> for Condition {
    fn from(condition: BeginsWith) -> Self {
        Self::BeginsWith(condition)
    }
}

impl From<Between> for Condition {
    fn from(condition: Between) -> Self {
        Self::Between(condition)
    }
}

impl From<Contains> for Condition {
    fn from(condition: Contains) -> Self {
        Self::Contains(condition)
    }
}

impl From<In> for Condition {
    fn from(condition: In) -> Self {
        Self::In(condition)
    }
}

impl From<Not> for Condition {
    fn from(condition: Not) -> Self {
        Self::Not(condition)
    }
}

impl From<And> for Condition {
    fn from(condition: And) -> Self {
        Self::And(condition)
    }
}

impl From<Or> for Condition {
    fn from(condition: Or) -> Self {
        Self::Or(condition)
    }
}

impl From<Comparison> for Condition {
    fn from(condition: Comparison) -> Self {
        Self::Comparison(condition)
    }
}

impl From<Parenthetical> for Condition {
    fn from(condition: Parenthetical) -> Self {
        Self::Parenthetical(condition)
    }
}

// As of v0.29, `aws_sdk_dynamodb` wants an `Into<String>` to be passed to the
// `.filter_expression()` methods on its `*Input` types. So, we'll implement
// that to make this nicer to work with.
impl From<Condition> for String {
    fn from(condition: Condition) -> Self {
        // TODO: Is there a more efficient way when all of these require formatting?
        condition.to_string()
    }
}

#[cfg(test)]
pub(crate) mod test {
    use pretty_assertions::assert_eq;

    use crate::path::Path;

    use super::{
        comparison::{greater_than, less_than},
        Condition,
    };

    /// `a > b`
    pub fn cmp_a_gt_b() -> Condition {
        Condition::Comparison(greater_than(
            "a".parse::<Path>().unwrap(),
            "b".parse::<Path>().unwrap(),
        ))
    }

    /// `c < d`
    pub fn cmp_c_lt_d() -> Condition {
        Condition::Comparison(less_than(
            "c".parse::<Path>().unwrap(),
            "d".parse::<Path>().unwrap(),
        ))
    }

    #[test]
    fn display() {
        assert_eq!("a > b", cmp_a_gt_b().to_string());
        assert_eq!("c < d", cmp_c_lt_d().to_string());
    }

    #[test]
    fn and() {
        use crate::Path;
        use pretty_assertions::assert_eq;

        let a = Path::new_name("a");
        let b = Path::new_name("b");
        let c = Path::new_name("c");
        let d = Path::new_name("d");

        let condition = a.greater_than(b).and(c.less_than(d));
        assert_eq!("a > b AND c < d", condition.to_string());
    }

    #[test]
    fn or() {
        use crate::Path;
        use pretty_assertions::assert_eq;

        let a = Path::new_name("a");
        let b = Path::new_name("b");
        let c = Path::new_name("c");
        let d = Path::new_name("d");

        let condition = a.greater_than(b).or(c.less_than(d));
        assert_eq!("a > b OR c < d", condition.to_string());
    }

    #[test]
    fn not() {
        use crate::Path;
        use pretty_assertions::assert_eq;

        let a = Path::new_name("a");
        let b = Path::new_name("b");

        let condition = a.greater_than(b).not();
        assert_eq!("NOT a > b", condition.to_string());
    }

    #[test]
    fn not_operator() {
        use crate::Path;
        use pretty_assertions::assert_eq;

        let a = Path::new_name("a");
        let b = Path::new_name("b");

        let condition = !a.greater_than(b);
        assert_eq!("NOT a > b", condition.to_string());
    }
}