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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::collections::HashMap;
use time::Timespec;
use crate::{
data::{self, Badge},
decoders::{optional_timespec_seconds, timespec_seconds},
error::{ApiError, Result},
EndpointResult,
};
#[derive(serde_derive::Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub(crate) struct Response {
ok: i32,
#[serde(rename = "_id")]
user_id: String,
username: String,
#[serde(default)]
email: Option<String>,
password: bool,
cpu: i32,
gcl: u64,
money: f64,
#[serde(default)]
badge: Option<Badge>,
cpu_shard: Option<HashMap<String, u32>>,
#[serde(default)]
#[serde(with = "optional_timespec_seconds")]
cpu_shard_updated_time: Option<Timespec>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct MyInfo {
pub user_id: String,
pub username: String,
pub has_password: bool,
pub cpu: i32,
pub gcl_points: u64,
pub credits: f64,
pub shard_allocations: Option<UserCpuShardAllocation>,
#[serde(skip)]
_non_exhaustive: (),
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct UserCpuShardAllocation {
pub allocations: HashMap<String, u32>,
#[serde(with = "timespec_seconds")]
pub last_update: Timespec,
}
impl EndpointResult for MyInfo {
type RequestResult = Response;
type ErrorResult = data::ApiError;
fn from_raw(raw: Response) -> Result<MyInfo> {
let Response {
ok,
user_id,
username,
password,
cpu,
gcl,
money,
cpu_shard,
cpu_shard_updated_time,
..
} = 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,
shard_allocations: cpu_shard.and_then(|allocations| {
cpu_shard_updated_time.map(|last_update| UserCpuShardAllocation {
allocations,
last_update,
})
}),
_non_exhaustive: (),
})
}
}
#[cfg(test)]
mod tests {
use super::MyInfo;
use crate::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"
}));
}
}