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
#![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`][rusoto_doc],
//! 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::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::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::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(())
//! }
//! #
//! # fn main() {
//! #     typed_example().unwrap();
//! # }
//! ```
//!
//! # Creating an HashMap by serializing data structures
//!
//! A data structure can be converted to an HashMap by
//! [`serde_dynamodb::to_hashmap`][to_hashmap].
//!
//! ```rust
//! extern crate serde;
//! extern crate serde_dynamodb;
//!
//! extern crate rusoto_core;
//! extern crate rusoto_dynamodb;
//!
//! #[macro_use]
//! extern crate serde_derive;
//!
//! use serde_dynamodb::Error;
//!
//! #[derive(Serialize, Deserialize, Debug)]
//! struct Address {
//!     street: String,
//!     city: String,
//! }
//!
//! 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 j = serde_dynamodb::to_hashmap(&address)?;
//!
//!     // Print, write to a file, or send to an HTTP server.
//!     println!("{:?}", j);
//!
//!     Ok(())
//! }
//! #
//! # fn main() {
//! #     print_an_address().unwrap();
//! # }
//! ```
//!
//! [aws_doc]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html#Programming.LowLevelAPI.DataTypeDescriptors
//! [rusoto_doc]: https://rusoto.github.io/rusoto/rusoto_dynamodb/struct.AttributeValue.html
//! [to_hashmap]: fn.to_hashmap.html
//! [from_hashmap]: fn.from_hashmap.html
//!

extern crate rusoto_dynamodb;
extern crate serde;

mod de;
mod ser;

pub mod error;

pub use de::from_hashmap;
pub use error::Error;
pub use ser::to_hashmap;

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