dynamodb_expression/condition/
comparison.rs1use core::fmt::{self, Write};
2
3use crate::operand::Operand;
4
5#[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#[derive(Debug, Copy, Clone, PartialEq, Eq)]
39pub enum Comparator {
40 Eq,
42 Ne,
44 Lt,
46 Le,
48 Gt,
50 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
73pub 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
91pub 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
109pub 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
127pub 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
145pub 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
163pub 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}