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
use std::borrow::Cow;
use chrono::{DateTime, Utc};
use futures::{Future as StdFuture, stream, Stream as StdStream};
use hyper::client::Connect;
use serde::de::{self, Deserialize, Deserializer};
use ::Client;
use super::Stream;
#[derive(Debug)]
pub struct PvpMatches<C>
where C: Clone + Connect
{
client: Client<C>,
}
impl<C: Clone + Connect> PvpMatches<C> {
#[inline]
pub(crate) fn new(client: Client<C>) -> Self {
PvpMatches { client }
}
}
impl<C: Clone + Connect> PvpMatches<C> {
#[inline]
pub fn all(&self) -> Stream<PvpMatch> {
self.get_pvp_matches_stream(None)
}
#[inline]
pub fn in_season<S: Into<String>>(&self, season: S) -> Stream<PvpMatch> {
self.get_pvp_matches_stream(Some(season.into()))
}
fn get_pvp_matches_stream(&self, season: Option<String>) -> Stream<PvpMatch> {
let url: Cow<str> = match season {
Some(s) => format!("{}?type=season&season={}", PVP_MATCHES_URL, s).into(),
None => PVP_MATCHES_URL.into(),
};
Box::new(
self.client.get(&*url)
.map(|resp: PvpMatchesResponse| stream::iter_ok(resp))
.into_stream()
.flatten()
)
}
}
const PVP_MATCHES_URL: &str = "/pvp-matches";
type PvpMatchesResponse = Vec<PvpMatch>;
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PvpMatch {
pub id: String,
#[serde(deserialize_with = "deserialize_datetime")]
start_at: DateTime<Utc>,
#[serde(deserialize_with = "deserialize_datetime")]
end_at: DateTime<Utc>,
url: String,
description: String,
style: String,
#[serde(deserialize_with = "deserialize_datetime")]
register_at: DateTime<Utc>,
}
fn deserialize_datetime<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
where D: Deserializer<'de>
{
let s: String = Deserialize::deserialize(deserializer)?;
let fixed_datetime: DateTime<_> = DateTime::parse_from_rfc3339(&s).map_err(|e| {
de::Error::custom(format!("failed to parse string as RFC3339 datetime: {}", e))
})?;
if fixed_datetime.offset().local_minus_utc() != 0 {
return Err(de::Error::custom(format!("expected UTC datetime, got one with offset {}",
fixed_datetime.offset())));
}
Ok(fixed_datetime.with_timezone(&Utc))
}