diff options
author | iximeow <me@iximeow.net> | 2017-11-01 04:31:43 -0700 |
---|---|---|
committer | iximeow <me@iximeow.net> | 2017-11-01 04:31:43 -0700 |
commit | 96425724b0e435c33876f4dd9685aed2bbdea1fd (patch) | |
tree | dd48118f712344d5dcea278148f3c82996f74021 /src/tw/user.rs | |
parent | c40aa2bf95131317e2a9b199afd0b34ae2d847ee (diff) |
move more logging to be through DisplayInfo statuses
move DisplayInfo into ... display
flush output, duh
Diffstat (limited to 'src/tw/user.rs')
-rw-r--r-- | src/tw/user.rs | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/src/tw/user.rs b/src/tw/user.rs index 8f41b6d..0af4eb8 100644 --- a/src/tw/user.rs +++ b/src/tw/user.rs @@ -18,29 +18,28 @@ impl Default for User { } impl User { - pub fn from_json(json: serde_json::Value) -> Option<User> { + pub fn from_json(json: serde_json::Value) -> Result<User, String> { if let serde_json::Value::Object(json_map) = json { - if json_map.contains_key("id_str") && - json_map.contains_key("name") && - json_map.contains_key("screen_name") { - if let ( - Some(id_str), - Some(name), - Some(screen_name) - ) = ( - json_map["id_str"].as_str(), - json_map["name"].as_str(), - json_map["screen_name"].as_str() - ) { - return Some(User { - id: id_str.to_owned(), - name: name.to_owned(), - handle: screen_name.to_owned() - }) - } + if let ( + Some(id_str), + Some(name), + Some(screen_name) + ) = ( + json_map.get("id_str").and_then(|x| x.as_str()), + json_map.get("name").and_then(|x| x.as_str()), + json_map.get("screen_name").and_then(|x| x.as_str()) + ) { + Ok(User { + id: id_str.to_owned(), + name: name.to_owned(), + handle: screen_name.to_owned() + }) + } else { + Err("user json missing one of id_str, name, screen_name".to_owned()) } + } else { + Err(format!("Invalid json: {:?}", json)) } - None } } |