dynamodb_expression/value/set/
num_set.rs

1use core::fmt;
2use std::collections::BTreeSet;
3
4use aws_sdk_dynamodb::types::AttributeValue;
5
6use crate::Num;
7
8/// Represents a [DynamoDB number set][1].
9///
10/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes.SetTypes
11#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12pub struct NumSet(BTreeSet<Num>);
13
14impl NumSet {
15    /// Creates a value to use as a [DynamoDB number set][1].
16    ///
17    /// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes.SetTypes
18    pub fn new<T>(set: T) -> Self
19    where
20        T: Into<NumSet>,
21    {
22        set.into()
23    }
24
25    // Intentionally not using `impl From<NumSet> for AttributeValue` because
26    // I don't want to make this a public API people rely on. The purpose of this
27    // crate is not to make creating `AttributeValues` easier. They should try
28    // `serde_dynamo`.
29    pub(super) fn into_attribute_value(self) -> AttributeValue {
30        AttributeValue::Ns(self.0.into_iter().map(Into::into).collect())
31    }
32}
33
34impl<T> FromIterator<T> for NumSet
35where
36    T: Into<Num>,
37{
38    fn from_iter<I>(iter: I) -> Self
39    where
40        I: IntoIterator<Item = T>,
41    {
42        Self(iter.into_iter().map(Into::into).collect())
43    }
44}
45
46impl<I, T> From<I> for NumSet
47where
48    I: IntoIterator<Item = T>,
49    T: Into<Num>,
50{
51    fn from(values: I) -> Self {
52        Self::from_iter(values)
53    }
54}
55
56impl fmt::Display for NumSet {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        f.debug_list().entries(self.0.iter().map(DebugNum)).finish()
59    }
60}
61
62struct DebugNum<'a>(&'a Num);
63
64impl<'a> fmt::Debug for DebugNum<'a> {
65    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66        f.write_str(&self.0.n)
67    }
68}