structs use Result to propagate deserialization errors as per issue #4

This commit is contained in:
Dan Ballard 2022-01-16 14:24:22 -05:00
parent a28813fb2e
commit 2d4e7bb751
2 changed files with 30 additions and 15 deletions

View File

@ -36,13 +36,16 @@ fn main() {
); );
// process json for profile, contacts and servers...else { // process json for profile, contacts and servers...else {
let profile = Profile::new( let profile = match Profile::new(
&event.data["Identity"], &event.data["Identity"],
&event.data["name"], &event.data["name"],
&event.data["picture"], &event.data["picture"],
&event.data["ContactsJson"], &event.data["ContactsJson"],
&event.data["ServerList"], &event.data["ServerList"],
); ) {
Ok(p) => p,
Err(e) => panic!("error parsing profile: {}", e)
};
print!("profile: {:?}", profile); print!("profile: {:?}", profile);
} }
"NewMessageFromPeer" => { "NewMessageFromPeer" => {

View File

@ -119,41 +119,53 @@ impl Profile {
picture: &str, picture: &str,
contacts_json: &str, contacts_json: &str,
server_list: &str, server_list: &str,
) -> Profile { ) -> Result<Profile, String> {
let contacts = Profile::process_contacts(contacts_json); let contacts = match Profile::process_contacts(contacts_json){
let servers = Profile::process_servers(server_list); Ok(c) => c,
Profile { 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(), onion: identity.to_string(),
nick: name.to_string(), nick: name.to_string(),
image_path: picture.to_string(), image_path: picture.to_string(),
attr: Default::default(), attr: Default::default(),
contacts: contacts, contacts: contacts,
servers: servers, servers: servers,
} })
} }
fn process_contacts(constacts_json: &str) -> HashMap<String, Contact> { fn process_contacts(constacts_json: &str) -> Result<HashMap<String, Contact>, String> {
let mut contacts: HashMap<String, Contact> = HashMap::new(); let mut contacts: HashMap<String, Contact> = HashMap::new();
if constacts_json == "null" { if constacts_json == "null" {
return contacts; return Ok(contacts);
} }
println!("contacts_json: '{}'", constacts_json); println!("contacts_json: '{}'", constacts_json);
let contacts_map: Vec<Contact> = serde_json::from_str(constacts_json).unwrap(); let contacts_map: Vec<Contact> = match serde_json::from_str(constacts_json) {
Ok(cm) => cm,
Err(e) => return Err(format!("invalid json: {:?}", e))
};
for contact in contacts_map { for contact in contacts_map {
contacts.insert(contact.onion.clone(), contact); contacts.insert(contact.onion.clone(), contact);
} }
contacts Ok(contacts)
} }
fn process_servers(servers_json: &str) -> HashMap<String, Server> { fn process_servers(servers_json: &str) -> Result<HashMap<String, Server>, String> {
let mut servers: HashMap<String, Server> = HashMap::new(); let mut servers: HashMap<String, Server> = HashMap::new();
if servers_json == "null" { if servers_json == "null" {
return servers; return Ok(servers);
} }
let servers_map: Vec<Server> = serde_json::from_str(servers_json).unwrap(); let servers_map: Vec<Server> = match serde_json::from_str(servers_json) {
Ok(sm) => sm,
Err(e) => return Err(format!("invalid jason: {:?}", e))
};
for server in servers_map { for server in servers_map {
servers.insert(server.onion.clone(), server); servers.insert(server.onion.clone(), server);
} }
servers Ok(servers)
} }
} }