dynamodb_expression/value/set/
string_set.rs

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