diff --git a/examples/echobot.rs b/examples/echobot.rs index fb0aef3..82f91da 100644 --- a/examples/echobot.rs +++ b/examples/echobot.rs @@ -36,13 +36,16 @@ fn main() { ); // process json for profile, contacts and servers...else { - let profile = Profile::new( + let profile = match Profile::new( &event.data["Identity"], &event.data["name"], &event.data["picture"], &event.data["ContactsJson"], &event.data["ServerList"], - ); + ) { + Ok(p) => p, + Err(e) => panic!("error parsing profile: {}", e) + }; print!("profile: {:?}", profile); } "NewMessageFromPeer" => { diff --git a/src/structs.rs b/src/structs.rs index 12d6ee3..7dd7fb7 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -119,41 +119,53 @@ impl Profile { picture: &str, contacts_json: &str, server_list: &str, - ) -> Profile { - let contacts = Profile::process_contacts(contacts_json); - let servers = Profile::process_servers(server_list); - Profile { + ) -> Result { + let contacts = match Profile::process_contacts(contacts_json){ + Ok(c) => c, + Err(e) => return Err(e) + }; + let servers = match Profile::process_servers(server_list) { + Ok(s) => s, + Err(e) => return Err(e) + }; + Ok(Profile { onion: identity.to_string(), nick: name.to_string(), image_path: picture.to_string(), attr: Default::default(), contacts: contacts, servers: servers, - } + }) } - fn process_contacts(constacts_json: &str) -> HashMap { + fn process_contacts(constacts_json: &str) -> Result, String> { let mut contacts: HashMap = HashMap::new(); if constacts_json == "null" { - return contacts; + return Ok(contacts); } println!("contacts_json: '{}'", constacts_json); - let contacts_map: Vec = serde_json::from_str(constacts_json).unwrap(); + let contacts_map: Vec = match serde_json::from_str(constacts_json) { + Ok(cm) => cm, + Err(e) => return Err(format!("invalid json: {:?}", e)) + }; for contact in contacts_map { contacts.insert(contact.onion.clone(), contact); } - contacts + Ok(contacts) } - fn process_servers(servers_json: &str) -> HashMap { + fn process_servers(servers_json: &str) -> Result, String> { let mut servers: HashMap = HashMap::new(); if servers_json == "null" { - return servers; + return Ok(servers); } - let servers_map: Vec = serde_json::from_str(servers_json).unwrap(); + let servers_map: Vec = match serde_json::from_str(servers_json) { + Ok(sm) => sm, + Err(e) => return Err(format!("invalid jason: {:?}", e)) + }; for server in servers_map { servers.insert(server.onion.clone(), server); } - servers + Ok(servers) } }