dynamodb_expression/condition/
or.rs

1use core::fmt;
2
3use crate::condition::Condition;
4
5/// Represents a [DynamoDB logical `OR`][1] condition.
6///
7/// See also: [`Condition::or`]
8///
9/// ```
10/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
11/// use dynamodb_expression::Path;
12/// # use pretty_assertions::assert_eq;
13///
14/// let a = "a".parse::<Path>()?;
15/// let b = "b".parse::<Path>()?;
16/// let c = "c".parse::<Path>()?;
17/// let d = "d".parse::<Path>()?;
18///
19/// let condition = a.greater_than(b).or(c.less_than(d));
20/// assert_eq!("a > b OR c < d", condition.to_string());
21/// #
22/// # Ok(())
23/// # }
24/// ```
25///
26/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.LogicalEvaluations
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct Or {
29    pub(crate) left: Box<Condition>,
30    pub(crate) right: Box<Condition>,
31}
32
33impl fmt::Display for Or {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        self.left.fmt(f)?;
36        f.write_str(" OR ")?;
37        self.right.fmt(f)
38    }
39}
40
41#[cfg(test)]
42mod test {
43    use crate::{condition::Or, Path};
44    use pretty_assertions::assert_eq;
45
46    #[test]
47    fn or() {
48        let a = "a".parse::<Path>().unwrap();
49        let b = "b".parse::<Path>().unwrap();
50        let c = "c".parse::<Path>().unwrap();
51        let d = "d".parse::<Path>().unwrap();
52
53        let condition = Or {
54            left: a.greater_than(b).into(),
55            right: c.less_than(d).into(),
56        };
57        assert_eq!("a > b OR c < d", condition.to_string());
58    }
59}