dynamodb_expression/condition/
attribute_type.rs

1use core::fmt::{self, Write};
2
3use crate::path::Path;
4
5/// The [DynamoDB `attribute_type` function][1]. True if the attribute at
6/// the specified [`Path`] is of the specified data type.
7///
8/// See also: [`Path::attribute_type`], [Type]
9///
10/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Functions
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct AttributeType {
13    // `Path` is correct here
14    // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Syntax
15    pub(crate) path: Path,
16    pub(crate) attribute_type: Type,
17}
18
19impl AttributeType {
20    pub fn new<P>(path: P, attribute_type: Type) -> Self
21    where
22        P: Into<Path>,
23    {
24        Self {
25            path: path.into(),
26            attribute_type,
27        }
28    }
29}
30
31impl fmt::Display for AttributeType {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        f.write_str("attribute_type(")?;
34        self.path.fmt(f)?;
35        f.write_str(", ")?;
36        self.attribute_type.fmt(f)?;
37        f.write_char(')')
38    }
39}
40
41/// The type of an attribute for the DynamoDB `attribute_type` function.
42///
43/// See also: [Path::attribute_type]
44#[derive(Debug, Copy, Clone, PartialEq, Eq)]
45pub enum Type {
46    String,
47    StringSet,
48    Number,
49    NumberSet,
50    Binary,
51    BinarySet,
52    Boolean,
53    Null,
54    List,
55    Map,
56}
57
58impl Type {
59    pub fn as_str(self) -> &'static str {
60        match self {
61            Self::String => "S",
62            Self::StringSet => "SS",
63            Self::Number => "N",
64            Self::NumberSet => "NS",
65            Self::Binary => "B",
66            Self::BinarySet => "BS",
67            Self::Boolean => "BOOL",
68            Self::Null => "NULL",
69            Self::List => "L",
70            Self::Map => "M",
71        }
72    }
73}
74
75impl fmt::Display for Type {
76    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77        f.write_str(self.as_str())
78    }
79}
80
81#[cfg(test)]
82mod test {
83    use pretty_assertions::assert_str_eq;
84
85    use super::Type::*;
86
87    #[test]
88    fn display_attribute_type() {
89        assert_str_eq!("S", String.to_string());
90        assert_str_eq!("SS", StringSet.to_string());
91        assert_str_eq!("N", Number.to_string());
92        assert_str_eq!("NS", NumberSet.to_string());
93        assert_str_eq!("B", Binary.to_string());
94        assert_str_eq!("BS", BinarySet.to_string());
95        assert_str_eq!("BOOL", Boolean.to_string());
96        assert_str_eq!("NULL", Null.to_string());
97        assert_str_eq!("L", List.to_string());
98        assert_str_eq!("M", Map.to_string());
99    }
100}