dynamodb_expression/operand/size.rs
1use core::fmt::{self, Write};
2
3use crate::{
4 condition::{
5 equal, greater_than, greater_than_or_equal, less_than, less_than_or_equal, not_equal,
6 Between, Comparison, In,
7 },
8 operand::Operand,
9 path::Path,
10};
11
12/// The [DynamoDB `size` function][1]. Returns a number representing an attributes size.
13///
14/// See also: [Path::size]
15///
16/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Functions
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct Size {
19 // `Path` is correct here
20 // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Syntax
21 pub(crate) path: Path,
22}
23
24impl Size {
25 /// Check if the value of this operand is equal to the given value.
26 ///
27 /// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
28 pub fn equal<T>(self, right: T) -> Comparison
29 where
30 T: Into<Operand>,
31 {
32 equal(self, right)
33 }
34
35 /// Check if the value of this operand is not equal to the given value.
36 ///
37 /// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
38 pub fn not_equal<T>(self, right: T) -> Comparison
39 where
40 T: Into<Operand>,
41 {
42 not_equal(self, right)
43 }
44
45 /// Check if the value of this operand is greater than the given value.
46 ///
47 /// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
48 pub fn greater_than<T>(self, right: T) -> Comparison
49 where
50 T: Into<Operand>,
51 {
52 greater_than(self, right)
53 }
54
55 /// Check if the value of this operand is greater than or equal to the given value.
56 ///
57 /// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
58 pub fn greater_than_or_equal<T>(self, right: T) -> Comparison
59 where
60 T: Into<Operand>,
61 {
62 greater_than_or_equal(self, right)
63 }
64
65 /// Check if the value of this operand is less than the given value.
66 ///
67 /// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
68 pub fn less_than<T>(self, right: T) -> Comparison
69 where
70 T: Into<Operand>,
71 {
72 less_than(self, right)
73 }
74
75 /// Check if the value of this operand is less than or equal to the given value.
76 ///
77 /// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
78 pub fn less_than_or_equal<T>(self, right: T) -> Comparison
79 where
80 T: Into<Operand>,
81 {
82 less_than_or_equal(self, right)
83 }
84
85 /// The [DynamoDB `BETWEEN` operator][1]. True if `self` is greater than or
86 /// equal to `lower`, and less than or equal to `upper`.
87 ///
88 /// See also: [`Path::size`], [`Key::between`]
89 ///
90 /// ```
91 /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
92 /// use dynamodb_expression::{Num, Path};
93 /// # use pretty_assertions::assert_eq;
94 ///
95 /// let condition = "foo".parse::<Path>()?.size().between(Num::new(512), Num::new(1024));
96 /// assert_eq!(r#"size(foo) BETWEEN 512 AND 1024"#, condition.to_string());
97 /// #
98 /// # Ok(())
99 /// # }
100 /// ```
101 ///
102 /// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators
103 /// [`Key::between`]: crate::key::Key::between
104 pub fn between<L, U>(self, lower: L, upper: U) -> Between
105 where
106 L: Into<Operand>,
107 U: Into<Operand>,
108 {
109 Between {
110 op: self.into(),
111 lower: lower.into(),
112 upper: upper.into(),
113 }
114 }
115
116 /// A [DynamoDB `IN` operation][1]. True if the value at this [`Path`] is equal
117 /// to any value in the list.
118 ///
119 /// The list can contain up to 100 values. It must have at least 1.
120 ///
121 /// See also: [`Path::size`]
122 ///
123 /// ```
124 /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
125 /// use dynamodb_expression::{Num, Path};
126 /// # use pretty_assertions::assert_eq;
127 ///
128 /// let condition = "foo".parse::<Path>()?.size().in_([10, 20, 30, 40, 50].map(Num::new));
129 /// assert_eq!(r#"size(foo) IN (10,20,30,40,50)"#, condition.to_string());
130 /// #
131 /// # Ok(())
132 /// # }
133 /// ```
134 ///
135 /// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators
136 pub fn in_<I, T>(self, items: I) -> In
137 where
138 I: IntoIterator<Item = T>,
139 T: Into<Operand>,
140 {
141 In::new(self, items)
142 }
143}
144
145impl fmt::Display for Size {
146 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147 f.write_str("size(")?;
148 self.path.fmt(f)?;
149 f.write_char(')')
150 }
151}
152
153impl<T> From<T> for Size
154where
155 T: Into<Path>,
156{
157 fn from(path: T) -> Self {
158 Self { path: path.into() }
159 }
160}