Last active
June 7, 2020 14:04
-
-
Save BrianKopp/d3f9496a97a28d5f55d5b01f93d6cfc9 to your computer and use it in GitHub Desktop.
RustRusotoRocket_UserMapToFrom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate rusoto_dynamodb; | |
use rusoto_dynamodb::AttributeValue; | |
use std::default::Default; | |
use std::result::Result; | |
impl User { | |
fn from_map(map: HashMap<String, AttributeValue>) -> Result<User, &'static str> { | |
return Ok(User { | |
id: match map.get("id") { | |
None => { | |
return Err("no id attribute"); | |
}, | |
Some(att) => att.s.clone() | |
}, | |
first_name: match map.get("first_name") { | |
None => { | |
return Err("no first_name attribute"); | |
}, | |
Some(att) => att.s.as_ref().unwrap().to_string() | |
}, | |
last_name: match map.get("last_name") { | |
None => { | |
return Err("no last_name attribute"); | |
}, | |
Some(att) => att.s.as_ref().unwrap().to_string() | |
}, | |
}); | |
} | |
fn to_map(&self) -> HashMap<String, AttributeValue> { | |
let mut map = HashMap::new(); | |
match &self.id { | |
Some(id) => { | |
map.insert( | |
"id".to_string(), | |
AttributeValue { | |
s: Some(id.to_string()), | |
..Default::default() | |
} | |
); | |
}, | |
None => { | |
map.insert( | |
"id".to_string(), | |
AttributeValue { | |
s: Some("TODO".to_string()), | |
..Default::default() | |
} | |
); | |
} | |
} | |
map.insert( | |
"first_name".to_string(), | |
AttributeValue { | |
s: Some(self.first_name.to_string()), | |
..Default::default() | |
} | |
); | |
map.insert( | |
"last_name".to_string(), | |
AttributeValue { | |
s: Some(self.last_name.to_string()), | |
..Default::default() | |
} | |
); | |
map | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment