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
use core::fmt;
use std::collections::BTreeSet;
use aws_sdk_dynamodb::{primitives::Blob, types::AttributeValue};
use super::base64;
/// A set of unique binary values for DynamoDB
///
/// <https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes.SetTypes>
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BinarySet(BTreeSet<Vec<u8>>);
impl BinarySet {
/// A set of unique binary values for DynamoDB
///
/// <https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes.SetTypes>
pub fn new<T>(set: T) -> Self
where
T: Into<BinarySet>,
{
set.into()
}
// Intentionally not using `impl From<BinarySet> for AttributeValue` because
// I don't want to make this a public API people rely on. The purpose of this
// crate is not to make creating `AttributeValues` easier. They should try
// `serde_dynamo`.
pub(super) fn into_attribute_value(self) -> AttributeValue {
AttributeValue::Bs(self.0.into_iter().map(Blob::new).collect())
}
}
impl<T> FromIterator<T> for BinarySet
where
T: IntoIterator<Item = u8>,
{
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
{
Self(
iter.into_iter()
.map(|value| value.into_iter().collect())
.collect(),
)
}
}
impl<I, T> From<I> for BinarySet
where
I: IntoIterator<Item = T>,
T: IntoIterator<Item = u8>,
{
fn from(values: I) -> Self {
Self::from_iter(values)
}
}
impl fmt::Display for BinarySet {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.0.iter().map(base64)).finish()
}
}