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
use crate::data;
use crate::error::{ApiError, Result};
use crate::EndpointResult;
#[derive(serde_derive::Deserialize, Clone, Hash, Debug)]
#[doc(hidden)]
pub(crate) struct Response {
ok: i32,
seasons: Vec<Season>,
}
#[derive(serde_derive::Deserialize, Clone, Hash, Debug)]
struct Season {
_id: String,
name: String,
date: String,
}
#[derive(Serialize, Deserialize, Clone, Hash, Debug)]
pub struct LeaderboardSeason {
pub name: String,
pub season_id: String,
pub end_date: String,
_non_exhaustive: (),
}
impl EndpointResult for Vec<LeaderboardSeason> {
type RequestResult = Response;
type ErrorResult = data::ApiError;
fn from_raw(raw: Response) -> Result<Vec<LeaderboardSeason>> {
let Response { ok, seasons } = raw;
if ok != 1 {
return Err(ApiError::NotOk(ok).into());
}
Ok(seasons
.into_iter()
.map(|s| LeaderboardSeason {
name: s.name,
season_id: s._id,
end_date: s.date,
_non_exhaustive: (),
})
.collect())
}
}
#[cfg(test)]
mod tests {
use super::LeaderboardSeason;
use crate::EndpointResult;
use serde_json;
fn test_parse(json: serde_json::Value) {
let response = serde_json::from_value(json).unwrap();
let _ = Vec::<LeaderboardSeason>::from_raw(response).unwrap();
}
#[test]
fn parse_sample_leaderboard_season_list() {
test_parse(json! ({
"ok": 1,
"seasons": [
{
"_id": "2017-03",
"date": "2017-03-01T00:00:05.605Z",
"name": "March 2017"
},
{
"_id": "2017-02",
"date": "2017-02-01T00:00:05.410Z",
"name": "February 2017"
},
{
"_id": "2017-01",
"date": "2017-01-01T00:00:03.681Z",
"name": "January 2017"
},
{
"_id": "2016-12",
"date": "2016-12-01T00:00:05.105Z",
"name": "December 2016"
},
{
"_id": "2016-11",
"date": "2016-11-01T00:00:04.489Z",
"name": "November 2016"
},
{
"_id": "2016-10",
"date": "2016-10-01T00:00:03.348Z",
"name": "October 2016"
}
]
}));
}
}