aboutsummaryrefslogtreecommitdiff
path: root/src/tw/user.rs
blob: 1da82f030b4651bcfe7a62cfa9973d435d501244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
    }
}