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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::borrow::Cow;
use rusoto_dynamodb::AttributeValue;
use crate::{AttributeError, Attributes};
#[cfg(feature = "uuid")]
mod uuid;
#[cfg(feature = "chrono")]
mod chrono;
pub fn extract<T: FromAttributeValue>(map: &mut Attributes, key: &str) -> Result<T, AttributeError> {
T::try_from_av(map.remove(key).ok_or_else(|| AttributeError::MissingField(key.to_owned()))?)
}
pub trait FromAttributeValue: Sized {
fn try_from_av(av: AttributeValue) -> Result<Self, AttributeError>;
}
pub trait IntoAttributeValue: Sized {
fn into_av(self) -> AttributeValue;
}
impl FromAttributeValue for String {
fn try_from_av(av: AttributeValue) -> Result<Self, AttributeError> {
av.s.ok_or(AttributeError::IncorrectType)
}
}
impl IntoAttributeValue for String {
fn into_av(self) -> AttributeValue {
AttributeValue {
s: Some(self),
..AttributeValue::default()
}
}
}
impl<T> FromAttributeValue for Cow<'_, T>
where
T: ToOwned,
T::Owned: FromAttributeValue,
{
fn try_from_av(av: AttributeValue) -> Result<Self, AttributeError> {
T::Owned::try_from_av(av).map(Cow::Owned)
}
}
impl<T> IntoAttributeValue for Cow<'_, T>
where
T: ToOwned,
T::Owned: IntoAttributeValue,
{
fn into_av(self) -> AttributeValue {
self.into_owned().into_av()
}
}
impl<T> FromAttributeValue for Vec<T>
where
T: FromAttributeValue,
{
fn try_from_av(av: AttributeValue) -> Result<Self, AttributeError> {
let l = av.l.ok_or(AttributeError::IncorrectType)?;
l.into_iter().map(T::try_from_av).collect()
}
}
impl<T> IntoAttributeValue for Vec<T>
where
T: IntoAttributeValue,
{
fn into_av(self) -> AttributeValue {
AttributeValue {
l: Some(self.into_iter().map(T::into_av).collect()),
..AttributeValue::default()
}
}
}
impl<T> FromAttributeValue for Option<T>
where
T: FromAttributeValue,
{
fn try_from_av(av: AttributeValue) -> Result<Self, AttributeError> {
match av.null {
Some(true) => Ok(None),
_ => Ok(Some(T::try_from_av(av)?)),
}
}
}
impl<T> IntoAttributeValue for Option<T>
where
T: IntoAttributeValue,
{
fn into_av(self) -> AttributeValue {
self.map_or_else(
|| AttributeValue {
null: Some(true),
..AttributeValue::default()
},
T::into_av,
)
}
}
macro_rules! convert_num {
($n:ident) => {
impl FromAttributeValue for $n {
fn try_from_av(av: AttributeValue) -> Result<Self, AttributeError> {
let n = av.n.ok_or(AttributeError::IncorrectType)?;
match n.parse() {
Ok(n) => Ok(n),
Err(e) => Err(AttributeError::ParseError(Box::new(e))),
}
}
}
impl IntoAttributeValue for $n {
fn into_av(self) -> AttributeValue {
AttributeValue {
n: Some(self.to_string()),
..AttributeValue::default()
}
}
}
};
}
convert_num!(isize);
convert_num!(i128);
convert_num!(i64);
convert_num!(i32);
convert_num!(i16);
convert_num!(i8);
convert_num!(usize);
convert_num!(u128);
convert_num!(u64);
convert_num!(u32);
convert_num!(u16);
convert_num!(u8);
convert_num!(f64);
convert_num!(f32);