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]. A collection of unique strings.
14 ///
15 /// See also: [`Set::new_string_set`], [`Value::new_string_set`]
16 ///
17 /// # Examples
18 ///
19 /// ```rust
20 /// # use pretty_assertions::assert_eq;
21 /// #
22 /// use dynamodb_expression::value::StringSet;
23 ///
24 /// assert_eq!(
25 /// StringSet::new([String::from("a"), String::from("b"), String::from("c")]),
26 /// StringSet::new(["a", "b", "b", "c"]),
27 /// );
28 /// ```
29 ///
30 /// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes.SetTypes
31 /// [`Set::new_string_set`]: crate::Set::new_string_set
32 /// [`Value::new_string_set`]: crate::Value::new_string_set
33 pub fn new<T>(set: T) -> Self
34 where
35 T: Into<StringSet>,
36 {
37 set.into()
38 }
39
40 // Intentionally not using `impl From<StringSet> for AttributeValue` because
41 // I don't want to make this a public API people rely on. The purpose of this
42 // crate is not to make creating `AttributeValues` easier. They should try
43 // `serde_dynamo`.
44 pub(super) fn into_attribute_value(self) -> AttributeValue {
45 AttributeValue::Ss(self.0.into_iter().collect())
46 }
47}
48
49impl<T> FromIterator<T> for StringSet
50where
51 T: Into<String>,
52{
53 fn from_iter<I>(iter: I) -> Self
54 where
55 I: IntoIterator<Item = T>,
56 {
57 Self(iter.into_iter().map(Into::into).collect())
58 }
59}
60
61impl<I, T> From<I> for StringSet
62where
63 I: IntoIterator<Item = T>,
64 T: Into<String>,
65{
66 fn from(values: I) -> Self {
67 Self::from_iter(values)
68 }
69}
70
71impl fmt::Display for StringSet {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 f.debug_list().entries(self.0.iter()).finish()
74 }
75}