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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! Interpreting user self information.

use EndpointResult;
use data::{self, Badge};
use error::{ApiError, Result};
use std::marker::PhantomData;

/// User info raw result.
#[derive(Deserialize, Clone, Debug)]
#[doc(hidden)]
pub struct Response {
    ok: i32,
    _id: String,
    username: String,
    password: bool,
    cpu: i32,
    gcl: u64,
    money: f64,
    // These can be added if needed
    // lastChargeTime: Option<String>,
    // lastTweetTime: Option<String>,
    // github: Option<serde_json::Value>,
    // twitter: Option<serde_json::Value>,
    // notifyPrefs: Option<serde_json::Value>,
    // steam: Option<serde_json::Value>,
    badge: Badge,
}

/// Result of a call to get the information for the logged in user.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct MyInfo {
    /// Unique user ID referring to this user.
    pub user_id: String,
    /// Unique username referring to this user.
    pub username: String,
    /// Whether or not a password can be used to login for this user.
    pub has_password: bool,
    /// This user's current CPU allowance.
    pub cpu: i32,
    /// This user's current total count of GCL points (perform calculation to find actual gcl level).
    pub gcl_points: u64,
    /// This user's current credit balance.
    pub credits: f64,
    /// Phantom data in order to allow adding any additional fields in the future.
    #[serde(skip)]
    _phantom: PhantomData<()>,
}

impl EndpointResult for MyInfo {
    type RequestResult = Response;
    type ErrorResult = data::ApiError;

    fn from_raw(raw: Response) -> Result<MyInfo> {
        let Response {
            ok,
            _id: user_id,
            username,
            password,
            cpu,
            gcl,
            money,
            ..
        } = raw;

        if ok != 1 {
            return Err(ApiError::NotOk(ok).into());
        }
        Ok(MyInfo {
            user_id: user_id,
            username: username,
            has_password: password,
            cpu: cpu,
            gcl_points: gcl,
            credits: money,
            _phantom: PhantomData,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::MyInfo;
    use EndpointResult;
    use serde_json;

    fn test_parse(json: serde_json::Value) {
        let response = serde_json::from_value(json).unwrap();

        let _ = MyInfo::from_raw(response).unwrap();
    }

    #[test]
    fn parse_sample_info() {
        test_parse(json! ({
            "_id": "57874d42d0ae911e3bd15bbc",
            "badge": {
                "color1": "#260d0d",
                "color2": "#6b2e41",
                "color3": "#ffe56d",
                "flip": false,
                "param": -100,
                "type": 21
            },
            "cpu": 170,
            "credits": 0,
            "email": "daboross@daboross.net",
            "gcl": 571069296,
            "github": {
                "id": "1152146",
                "username": "daboross"
            },
            "lastRespawnDate": 1475270405700i64,
            "money": 3957697.9500000584f64,
            "notifyPrefs": {
                "errorsInterval": 0
            },
            "ok": 1,
            "password": true,
            "promoPeriodUntil": 1471635211172i64,
            "steam": {
                "displayName": "daboross",
                "id": "76561198033802814",
                "ownership": [
                    464350
                ]
            },
            "subscription": true,
            "subscriptionTokens": 0,
            "username": "daboross"
        }));
    }
}