1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
use core::fmt::{self, Write};
use crate::operand::Operand;
/// Represents a [DynamoDB `IN` condition][1]. True if the value from the
/// [`Operand`] (the `op` parameter) is equal to any value in the list (the
/// `items` parameter).
///
/// The DynamoDB allows the list to contain up to 100 values. It must have at least 1.
///
/// See also: [`Path::in_`]
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use dynamodb_expression::{condition::In, operand::Operand, Path};
/// # use pretty_assertions::assert_eq;
///
/// let condition = "name".parse::<Path>()?.in_(["Jack", "Jill"]);
/// assert_eq!(r#"name IN ("Jack","Jill")"#, condition.to_string());
/// #
/// # Ok(())
/// # }
/// ```
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators
/// [`Path::in_`]: crate::path::Path::in_
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct In {
pub(crate) op: Operand,
pub(crate) items: Vec<Operand>,
}
impl In {
/// Allows for manually creating an `In` instance.
///
/// See also: [`Path::in_`]
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use dynamodb_expression::{condition::In, operand::Operand, Path};
/// # use pretty_assertions::assert_eq;
///
/// let condition = In::new("name".parse::<Path>()?, ["Jack", "Jill"]);
/// assert_eq!(r#"name IN ("Jack","Jill")"#, condition.to_string());
/// #
/// # Ok(())
/// # }
/// ```
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators
/// [`Path::in_`]: crate::path::Path::in_
pub fn new<O, I, T>(op: O, items: I) -> Self
where
O: Into<Operand>,
I: IntoIterator<Item = T>,
T: Into<Operand>,
{
Self {
op: op.into(),
items: items.into_iter().map(Into::into).collect(),
}
}
}
impl fmt::Display for In {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.op.fmt(f)?;
f.write_str(" IN (")?;
let mut first = true;
self.items.iter().try_for_each(|item| {
if first {
first = false;
} else {
f.write_char(',')?;
}
item.fmt(f)
})?;
f.write_char(')')
}
}