aboutsummaryrefslogtreecommitdiff
path: root/tw/user.rs
diff options
context:
space:
mode:
authorAndy Wortman <ixineeringeverywhere@gmail.com>2017-10-01 23:51:13 -0700
committerAndy Wortman <ixineeringeverywhere@gmail.com>2017-10-01 23:51:13 -0700
commitafd61ae0822690f30d37859c806a8d8d843b8c1a (patch)
treeb556fba4831a5e16f6838a6d4e1448e2be92cec7 /tw/user.rs
parent943824e02fa771fa8350e4da90f2c9591ec4647e (diff)
extract twitter message handling logic
Diffstat (limited to 'tw/user.rs')
-rw-r--r--tw/user.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/tw/user.rs b/tw/user.rs
new file mode 100644
index 0000000..1da82f0
--- /dev/null
+++ b/tw/user.rs
@@ -0,0 +1,46 @@
+extern crate serde_json;
+
+#[derive(Debug, Serialize, Deserialize)]
+pub struct User {
+ pub id: String,
+ pub name: String,
+ pub handle: String
+}
+
+impl Default for User {
+ fn default() -> User {
+ User {
+ id: "".to_owned(),
+ name: "_default_".to_owned(),
+ handle: "_default_".to_owned()
+ }
+ }
+}
+
+impl User {
+ pub fn from_json(json: serde_json::Value) -> Option<User> {
+ 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()
+ })
+ }
+ }
+ }
+ None
+ }
+}
+