Skip to content

Instantly share code, notes, and snippets.

@whmountains
Last active August 28, 2017 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whmountains/eac78a84b61ad8033729731b329dfc1f to your computer and use it in GitHub Desktop.
Save whmountains/eac78a84b61ad8033729731b329dfc1f to your computer and use it in GitHub Desktop.
Rust macro for defining DynamoDB items

This is a Rust macro for defining DynamoDB items for use with rusoto_dynamodb. It also requires that the hashmap! macro from the maplit crate be in scope.

#[macro_use] extern crate maplit;
use std::collections::HashMap;
macro_rules! ddb_item {
($($p:tt: $t:tt => $x:expr),*) => {
hashmap!{
$(
String::from(stringify!($p)) => AttributeValue {
$t: Some($x),
..Default::default()
},
)*
}
}
}
#[macro_use] extern crate maplit;
extern crate rusoto_core;
extern crate rusoto_dynamodb;
extern crate uuid;
use std::default::Default;
use uuid::Uuid;
use rusoto_core::{default_tls_client, DefaultCredentialsProvider, Region};
use rusoto_dynamodb::{DynamoDb, DynamoDbClient, PutItemInput, AttributeValue};
macro_rules! ddb_item {
($($p:tt: $t:tt => $x:expr),*) => {
hashmap!{
$(
String::from(stringify!($p)) => AttributeValue {
$t: Some($x),
..Default::default()
},
)*
}
}
}
fn main() {
let credentials = DefaultCredentialsProvider::new().unwrap();
let client = DynamoDbClient::new(default_tls_client().unwrap(), credentials, Region::SaEast1);
let entry_id = Uuid::new_v4();
let query_params = PutItemInput {
table_name: String::from("TABLE NAME"),
item: ddb_item!{
Id: s => format!("{}", entry_id),
Status: s => String::from("some status")
},
..Default::default()
};
match client.put_item(&query_params) {
Ok(_) => {
println!("Successfully created entry")
}
Err(error) => {
println!("Error: {:?}", error);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment