use std::{
borrow::Cow,
collections::{BTreeSet, HashSet},
};
use aws_sdk_dynamodb::primitives::Blob;
use super::*;
impl IntoAttributeValue for AttributeValue {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
self
}
}
impl IntoAttributeValue for String {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::S(self)
}
}
impl IntoStringAttributeValue for String {}
impl IntoAttributeValue for &str {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::S(self.to_owned())
}
}
impl IntoStringAttributeValue for &str {}
impl IntoAttributeValue for &String {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::S(self.to_owned())
}
}
impl IntoStringAttributeValue for &String {}
impl IntoAttributeValue for Cow<'_, str> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::S(self.into_owned())
}
}
impl IntoStringAttributeValue for Cow<'_, str> {}
impl IntoAttributeValue for bool {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::Bool(self)
}
}
macro_rules! impl_numeric {
($($t:ty),*) => {
$(
impl IntoAttributeValue for $t {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::N(self.to_string())
}
}
impl IntoNumberAttributeValue for $t {}
)*
};
}
impl_numeric!(
i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64
);
impl<T: Into<String>> IntoAttributeValue for AsNumber<T> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::N(self.0.into())
}
}
impl<T: Into<String>> IntoNumberAttributeValue for AsNumber<T> {}
impl IntoAttributeValue for Vec<u8> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::B(Blob::new(self))
}
}
impl IntoBinaryAttributeValue for Vec<u8> {}
impl IntoAttributeValue for &[u8] {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::B(Blob::new(self))
}
}
impl IntoBinaryAttributeValue for &[u8] {}
macro_rules! impl_vec_list {
($($t:ty),*) => {
$(
impl IntoAttributeValue for Vec<$t> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::L(
self.into_iter()
.map(IntoAttributeValue::into_attribute_value)
.collect(),
)
}
}
)*
};
}
impl_vec_list!(
String,
&str,
bool,
i8,
i16,
i32,
i64,
i128,
isize,
u16,
u32,
u64,
u128,
usize,
f32,
f64,
AttributeValue,
Vec<u8>
);
macro_rules! impl_slice_list {
($($t:ty),*) => {
$(
impl IntoAttributeValue for &[$t] {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::L(
self.into_iter()
.cloned()
.map(IntoAttributeValue::into_attribute_value)
.collect(),
)
}
}
)*
};
}
impl_slice_list!(
String,
&str,
bool,
i8,
i16,
i32,
i64,
i128,
isize,
u16,
u32,
u64,
u128,
usize,
f32,
f64,
Vec<u8>,
&[u8]
);
impl IntoAttributeValue for &[AttributeValue] {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::L(self.to_vec())
}
}
impl IntoAttributeValue for HashSet<String> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::Ss(self.into_iter().collect())
}
}
impl IntoAttributeValue for HashSet<&str> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::Ss(self.into_iter().map(ToOwned::to_owned).collect())
}
}
impl IntoAttributeValue for BTreeSet<String> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::Ss(self.into_iter().collect())
}
}
impl IntoAttributeValue for BTreeSet<&str> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::Ss(self.into_iter().map(ToOwned::to_owned).collect())
}
}
impl IntoAttributeValue for AsSet<String> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::Ss(self.0)
}
}
impl IntoAttributeValue for AsSet<&str> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::Ss(self.into_iter().map(ToOwned::to_owned).collect())
}
}
macro_rules! impl_number_set {
($col:ident, $($t:ty),*) => {
$(
impl IntoAttributeValue for $col<$t> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::Ns(self.into_iter().map(|v| v.to_string()).collect())
}
}
)*
};
}
impl_number_set!(
HashSet, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64
);
impl_number_set!(
BTreeSet, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64
);
impl_number_set!(
AsSet, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64
);
impl IntoAttributeValue for HashSet<Vec<u8>> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::Bs(self.into_iter().map(Blob::new).collect())
}
}
impl IntoAttributeValue for BTreeSet<Vec<u8>> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::Bs(self.into_iter().map(Blob::new).collect())
}
}
impl IntoAttributeValue for AsSet<Vec<u8>> {
#[inline]
fn into_attribute_value(self) -> AttributeValue {
AttributeValue::Bs(self.into_iter().map(Blob::new).collect())
}
}