dynamodb_expression/condition/
comparison.rs

1use core::fmt::{self, Write};
2
3use crate::operand::Operand;
4
5/// Represents a [DynamoDB comparison operation][1] for use in a [`Condition`].
6///
7/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators
8/// [`Condition`]: crate::condition::Condition
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct Comparison {
11    pub(crate) left: Operand,
12    pub(crate) cmp: Comparator,
13    pub(crate) right: Operand,
14}
15
16impl fmt::Display for Comparison {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        self.left.fmt(f)?;
19        f.write_char(' ')?;
20        self.cmp.fmt(f)?;
21        f.write_char(' ')?;
22        self.right.fmt(f)
23    }
24}
25
26/**
27[DynamoDB comparison operators](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
28
29```no-compile
30comparator ::=
31    =
32    | <>
33    | <
34    | <=
35    | >
36    | >=
37*/
38#[derive(Debug, Copy, Clone, PartialEq, Eq)]
39pub enum Comparator {
40    /// Equal (`=`)
41    Eq,
42    /// Not equal (`<>`)
43    Ne,
44    /// Less than (`<`)
45    Lt,
46    /// Less than or equal (`<=`)
47    Le,
48    /// Greater than (`>`)
49    Gt,
50    /// Greater than or equal (`>=`)
51    Ge,
52}
53
54impl Comparator {
55    pub fn as_str(self) -> &'static str {
56        match self {
57            Self::Eq => "=",
58            Self::Ne => "<>",
59            Self::Lt => "<",
60            Self::Le => "<=",
61            Self::Gt => ">",
62            Self::Ge => ">=",
63        }
64    }
65}
66
67impl fmt::Display for Comparator {
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        f.write_str(self.as_str())
70    }
71}
72
73/// Check if the two [values][1] or [paths][2] are equal.
74///
75/// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
76///
77/// [1]: [crate::value]
78/// [2]: [crate::path::Path]
79pub fn equal<L, R>(left: L, right: R) -> Comparison
80where
81    L: Into<Operand>,
82    R: Into<Operand>,
83{
84    Comparison {
85        left: left.into(),
86        cmp: Comparator::Eq,
87        right: right.into(),
88    }
89}
90
91/// Check if the two [values][1] or [paths][2] are not equal.
92///
93/// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
94///
95/// [1]: [crate::value]
96/// [2]: [crate::path::Path]
97pub fn not_equal<L, R>(left: L, right: R) -> Comparison
98where
99    L: Into<Operand>,
100    R: Into<Operand>,
101{
102    Comparison {
103        left: left.into(),
104        cmp: Comparator::Ne,
105        right: right.into(),
106    }
107}
108
109/// Check if a [value][1] or [`Path`] is greater than another.
110///
111/// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
112///
113/// [1]: [crate::value]
114/// [`Path`]: crate::path::Path
115pub fn greater_than<L, R>(left: L, right: R) -> Comparison
116where
117    L: Into<Operand>,
118    R: Into<Operand>,
119{
120    Comparison {
121        left: left.into(),
122        cmp: Comparator::Gt,
123        right: right.into(),
124    }
125}
126
127/// Check if a [value][1] or [`Path`] is greater than or equal to another.
128///
129/// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
130///
131/// [1]: [crate::value]
132/// [`Path`]: crate::path::Path
133pub fn greater_than_or_equal<L, R>(left: L, right: R) -> Comparison
134where
135    L: Into<Operand>,
136    R: Into<Operand>,
137{
138    Comparison {
139        left: left.into(),
140        cmp: Comparator::Ge,
141        right: right.into(),
142    }
143}
144
145/// Check if a [value][1] or [`Path`] is less than another.
146///
147/// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
148///
149/// [1]: [crate::value]
150/// [`Path`]: crate::path::Path
151pub fn less_than<L, R>(left: L, right: R) -> Comparison
152where
153    L: Into<Operand>,
154    R: Into<Operand>,
155{
156    Comparison {
157        left: left.into(),
158        cmp: Comparator::Lt,
159        right: right.into(),
160    }
161}
162
163/// Check if a [value][1] or [`Path`] is less than or equal to another.
164///
165/// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
166///
167/// [1]: [crate::value]
168/// [`Path`]: crate::path::Path
169pub fn less_than_or_equal<L, R>(left: L, right: R) -> Comparison
170where
171    L: Into<Operand>,
172    R: Into<Operand>,
173{
174    Comparison {
175        left: left.into(),
176        cmp: Comparator::Le,
177        right: right.into(),
178    }
179}
180
181#[cfg(test)]
182mod test {
183    use pretty_assertions::assert_str_eq;
184
185    use crate::path::Name;
186
187    use super::{Comparator::*, *};
188
189    #[test]
190    fn display() {
191        assert_str_eq!("=", Eq.to_string());
192        assert_str_eq!("<>", Ne.to_string());
193        assert_str_eq!("<", Lt.to_string());
194        assert_str_eq!("<=", Le.to_string());
195        assert_str_eq!(">", Gt.to_string());
196        assert_str_eq!(">=", Ge.to_string());
197    }
198
199    #[test]
200    fn eq() {
201        assert_eq!(
202            "foo = bar",
203            equal(Name::from("foo"), Name::from("bar")).to_string()
204        );
205    }
206
207    #[test]
208    fn ne() {
209        assert_eq!(
210            "foo <> bar",
211            not_equal(Name::from("foo"), Name::from("bar")).to_string()
212        );
213    }
214
215    #[test]
216    fn lt() {
217        assert_eq!(
218            "foo < bar",
219            less_than(Name::from("foo"), Name::from("bar")).to_string()
220        );
221    }
222
223    #[test]
224    fn le() {
225        assert_eq!(
226            "foo <= bar",
227            less_than_or_equal(Name::from("foo"), Name::from("bar")).to_string()
228        );
229    }
230
231    #[test]
232    fn gt() {
233        assert_eq!(
234            "foo > bar",
235            greater_than(Name::from("foo"), Name::from("bar")).to_string()
236        );
237    }
238
239    #[test]
240    fn ge() {
241        assert_eq!(
242            "foo >= bar",
243            greater_than_or_equal(Name::from("foo"), Name::from("bar")).to_string()
244        );
245    }
246}