dynamodb_expression/operand/
mod.rs

1//! Types related to operands for [DynamoDB condition and filter expressions][1].
2//!
3//! [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html
4
5mod operand_type;
6mod size;
7
8pub(crate) use self::operand_type::OperandType;
9pub use self::size::Size;
10
11use core::fmt;
12
13use crate::condition::{
14    equal, greater_than, greater_than_or_equal, less_than, less_than_or_equal, not_equal, Between,
15    Condition, In,
16};
17
18/// Represents a [part of a DynamoDB comparison][1].
19///
20/// You can use `Operand::from` to construct an instance from any of these:
21/// * [`Path`]
22/// * [`Element`]
23/// * [`Name`]
24/// * [`IndexedField`]
25/// * [`Scalar`]
26/// * [`Ref`]
27/// * [`Condition`] (as well as `Box<Condition>`)
28/// * [`Size`]
29///
30/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html
31/// [`Path`]: crate::path::Path
32/// [`Element`]: crate::path::Element
33/// [`Name`]: crate::path::Name
34/// [`IndexedField`]: crate::path::IndexedField
35/// [`Scalar`]: crate::value::Scalar
36/// [`Ref`]: crate::value::Ref
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct Operand {
39    pub(crate) op: OperandType,
40}
41
42impl Operand {
43    /// Check if the value of this operand is equal to the given value.
44    ///
45    /// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
46    pub fn equal<T>(self, right: T) -> Condition
47    where
48        T: Into<Operand>,
49    {
50        equal(self, right).into()
51    }
52
53    /// Check if the value of this operand is not equal to the given value.
54    ///
55    /// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
56    pub fn not_equal<T>(self, right: T) -> Condition
57    where
58        T: Into<Operand>,
59    {
60        not_equal(self, right).into()
61    }
62
63    /// Check if the value of this operand is greater than the given value.
64    ///
65    /// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
66    pub fn greater_than<T>(self, right: T) -> Condition
67    where
68        T: Into<Operand>,
69    {
70        greater_than(self, right).into()
71    }
72
73    /// Check if the value of this operand is greater than or equal to the given value.
74    ///
75    /// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
76    pub fn greater_than_or_equal<T>(self, right: T) -> Condition
77    where
78        T: Into<Operand>,
79    {
80        greater_than_or_equal(self, right).into()
81    }
82
83    /// Check if the value of this operand is less than the given value.
84    ///
85    /// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
86    pub fn less_than<T>(self, right: T) -> Condition
87    where
88        T: Into<Operand>,
89    {
90        less_than(self, right).into()
91    }
92
93    /// Check if the value of this operand is less than or equal to the given value.
94    ///
95    /// [DynamoDB documentation.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
96    pub fn less_than_or_equal<T>(self, right: T) -> Condition
97    where
98        T: Into<Operand>,
99    {
100        less_than_or_equal(self, right).into()
101    }
102
103    /// `self BETWEEN b AND c` - true if `self` is greater than or equal to `b`, and less than or equal to `c`.
104    ///
105    /// [DynamoDB documentation](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
106    pub fn between<L, U>(self, lower: L, upper: U) -> Condition
107    where
108        L: Into<Operand>,
109        U: Into<Operand>,
110    {
111        Condition::Between(Between {
112            op: self,
113            lower: lower.into(),
114            upper: upper.into(),
115        })
116    }
117
118    /// `self IN (b[, ..])` — true if `self` is equal to any value in the list.
119    ///
120    /// The list can contain up to 100 values. It must have at least 1.
121    ///
122    /// [DynamoDB documentation](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators)
123    pub fn in_<I, T>(self, items: I) -> Condition
124    where
125        I: IntoIterator<Item = T>,
126        T: Into<Operand>,
127    {
128        Condition::In(In::new(self, items))
129    }
130}
131
132impl fmt::Display for Operand {
133    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134        self.op.fmt(f)
135    }
136}
137
138impl<T> From<T> for Operand
139where
140    T: Into<OperandType>,
141{
142    fn from(op: T) -> Self {
143        Self { op: op.into() }
144    }
145}