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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#![deny(
    warnings,
    missing_debug_implementations,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unsafe_code,
    unstable_features,
    unused_import_braces,
    unused_qualifications,
    missing_docs
)]

//! # DynamoDB
//!
//! In its low level API, DynamoDB works with JSON objects with extra levels to
//! set the type of the values.
//!
//! ```json,ignore
//! {
//!     "Item": {
//!         "Age": {"N": "8"},
//!         "Colors": {
//!             "L": [
//!                 {"S": "White"},
//!                 {"S": "Brown"},
//!                 {"S": "Black"}
//!             ]
//!         },
//!         "Name": {"S": "Fido"},
//!         "Vaccinations": {
//!             "M": {
//!                 "Rabies": {
//!                     "L": [
//!                         {"S": "2009-03-17"},
//!                         {"S": "2011-09-21"},
//!                         {"S": "2014-07-08"}
//!                     ]
//!                 },
//!                 "Distemper": {"S": "2015-10-13"}
//!             }
//!         },
//!         "Breed": {"S": "Beagle"},
//!         "AnimalType": {"S": "Dog"}
//!     }
//! }
//! ```
//!
//! The allowed type keys are described [here][aws_doc].
//!
//! # Rusoto DynamoDB
//!
//! Rusoto DynamoDB map those values to [`AttributeValue`][dynamodb_attribute],
//! and functions to get/set/... from DynamoDB use `HashMap<String, AttributeValue>`
//! as a way to represent the data.
//!
//! # Parsing HashMap as strongly typed data structures
//!
//! Serde provides a powerful way of mapping HashMap data into Rust data structures
//! largely automatically by using [serde_dynamodb_streams::from_hashmap][from_hashmap]
//!
//! ```rust,ignore
//! extern crate serde;
//! extern crate serde_dynamodb;
//!
//! extern crate rusoto_core;
//! extern crate rusoto_dynamodb;
//!
//! #[macro_use]
//! extern crate serde_derive;
//!
//! use std::collections::HashMap;
//!
//! use rusoto_core::Region;
//! use rusoto_dynamodb::{DynamoDb, DynamoDbClient, QueryInput, AttributeValue};
//!
//! use serde_dynamodb_streams::Error;
//!
//! #[derive(Serialize, Deserialize, Debug)]
//! struct Person {
//!     surname: String,
//!     age: u8,
//!     phones: Vec<String>,
//! }
//!
//! fn typed_example() -> Result<(), Error> {
//!
//!     let client = DynamoDbClient::simple(Region::UsEast1);
//!
//!     let mut query = HashMap::new();
//!     query.insert(String::from(":surname"), AttributeValue {
//!         s: Some(String::from("Smith")),
//!         ..Default::default()
//!     });
//!     // get data from DynamoDB
//!     let persons: Vec<Person> = client
//!         .query(&QueryInput {
//!             table_name: String::from("person"),
//!             key_condition_expression: Some(String::from("surname = :surname")),
//!             expression_attribute_values: Some(query),
//!             ..Default::default()
//!         })
//!         .sync()
//!         .unwrap()
//!         .items
//!         .unwrap_or_else(|| vec![])
//!         .into_iter()
//!         .map(|item| serde_dynamodb_streams::from_hashmap(item).unwrap())
//!         .collect();
//!
//!
//!     // Do things just like with any other Rust data structure.
//!     for p in persons {
//!         println!("Please call {} at the number {}", p.surname, p.phones[0]);
//!     }
//!
//!     Ok(())
//! }
//! ```
//!
//! # Creating an HashMap by serializing data structures
//!
//! A data structure can be converted to an HashMap by
//! [`serde_dynamodb_streams::to_hashmap`][to_hashmap].
//!
//! ```rust
//! use serde::{Serialize, Deserialize};
//! use serde_dynamodb_streams::Error;
//!
//! #[derive(Serialize, Deserialize, Debug)]
//! struct Address {
//!     street: String,
//!     city: String,
//! }
//!
//! # #[cfg(feature = "rusoto_dynamodb")]
//! fn print_an_address() -> Result<(), Error> {
//!     // Some data structure.
//!     let address = Address {
//!         street: "10 Downing Street".to_owned(),
//!         city: "London".to_owned(),
//!     };
//!
//!     // Serialize it to an HashMap.
//!     let dynamodb_object = serde_dynamodb_streams::to_hashmap(&address)?;
//!
//!     // Print, write to a file, or send to an HTTP server.
//!     println!("{:?}", dynamodb_object);
//!
//!     Ok(())
//! }
//! ```
//!
//! # Features
//!
//! ## rusoto_dynamodb
//!
//! Feature `rusoto_dynamodb` is enabled by default and add methods
//! [`serde_dynamodb_streams::from_hashmap`][from_hashmap] and
//! [`serde_dynamodb_streams::to_hashmap`][to_hashmap]. Those methods work with
//! [`AttributeValue`][dynamodb_attribute] from [DynamoDB][dynamodb].
//!
//! ## rusoto_dynamodbstreams
//!
//! Feature `rusoto_dynamodbstreams` is disabled by default and add module
//! [`streams`][streams] with methods
//! [`serde_dynamodb_streams::streams::from_hashmap`][from_hashmap_streams] and
//! [`serde_dynamodb_streams::streams::to_hashmap`][to_hashmap_streams]. Those methods work with
//! [`AttributeValue`][dynamodbstreams_attribute] from [DynamoDB Streams][dynamodbstreams].
//!
//! [aws_doc]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html#Programming.LowLevelAPI.DataTypeDescriptors
//! [dynamodb]: https://rusoto.github.io/rusoto/rusoto_dynamodb/index.html
//! [dynamodb_attribute]: https://rusoto.github.io/rusoto/rusoto_dynamodb/struct.AttributeValue.html
//! [dynamodbstreams]: https://rusoto.github.io/rusoto/rusoto_dynamodbstreams/index.html
//! [dynamodbstreams_attribute]: https://rusoto.github.io/rusoto/rusoto_dynamodbstreams/struct.AttributeValue.html
//! [to_hashmap]: fn.to_hashmap.html
//! [from_hashmap]: fn.from_hashmap.html
//! [streams]: streams/index.html
//! [to_hashmap_streams]: streams/fn.to_hashmap.html
//! [from_hashmap_streams]: streams/fn.from_hashmap.html
//!

pub mod error;

pub use error::Error;

mod common;

#[cfg(feature = "rusoto_dynamodb")]
mod dynamodb;
#[cfg(feature = "rusoto_dynamodb")]
pub use dynamodb::de::from_hashmap;
#[cfg(feature = "rusoto_dynamodb")]
pub use dynamodb::ser::to_hashmap;

#[cfg(feature = "rusoto_dynamodbstreams")]
mod dynamodbstreams;
#[cfg(feature = "rusoto_dynamodbstreams")]
pub mod streams {
    //! Methods in this module are generated to work with
    //! [`AttributeValue`][dynamodbstreams_attribute] from [DynamoDB Streams][dynamodbstreams]
    //! instead of [`AttributeValue`][dynamodb_attribute] from [DynamoDB][dynamodb].
    //!
    //! [dynamodb]: https://rusoto.github.io/rusoto/rusoto_dynamodb/index.html
    //! [dynamodb_attribute]: https://rusoto.github.io/rusoto/rusoto_dynamodb/struct.AttributeValue.html
    //! [dynamodbstreams]: https://rusoto.github.io/rusoto/rusoto_dynamodbstreams/index.html
    //! [dynamodbstreams_attribute]: https://rusoto.github.io/rusoto/rusoto_dynamodbstreams/struct.AttributeValue.html
    pub use crate::dynamodbstreams::de::from_hashmap;
    pub use crate::dynamodbstreams::ser::to_hashmap;
}

/// A data structure that can be used as a DynamoDB `QueryInput`
#[cfg(feature = "rusoto_dynamodb")]
pub trait ToQueryInput {
    /// Transform this structure as a DynamoDB `QueryInput` on the given `table`
    fn to_query_input(&self, table: String) -> rusoto_dynamodb::QueryInput;
}