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
use rustc_serialize::json::Json;
use super::Lifetime;
use client::response::{FromResponse, ParseError, JsonHelper};
#[derive(Debug, Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable)]
pub struct Static;
impl Lifetime for Static {
fn expired(&self) -> bool { false }
}
impl FromResponse for Static {
fn from_response(json: &Json) -> Result<Self, ParseError> {
let obj = try!(JsonHelper(json).as_object());
if obj.0.contains_key("expires_in") {
return Err(ParseError::UnexpectedField("expires_in"));
}
Ok(Static)
}
}
#[cfg(feature = "serde")]
mod serde {
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use serde::de::impls::UnitVisitor;
use super::Static;
impl Serialize for Static {
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
serializer.serialize_unit_struct("Static")
}
}
impl Deserialize for Static {
fn deserialize<D: Deserializer>(deserializer: &mut D) -> Result<Self, D::Error> {
deserializer.deserialize_unit_struct("Static", UnitVisitor)
.and(Ok(Static))
}
}
}
#[cfg(test)]
mod tests {
use rustc_serialize::json::Json;
use client::response::{FromResponse, ParseError};
use super::Static;
#[test]
fn from_response() {
let json = Json::from_str("{}").unwrap();
assert_eq!(Static, Static::from_response(&json).unwrap());
}
#[test]
fn from_response_with_expires_in() {
let json = Json::from_str(r#"{"expires_in":3600}"#).unwrap();
assert_eq!(
ParseError::UnexpectedField("expires_in"),
Static::from_response(&json).unwrap_err()
);
}
#[cfg(feature = "serde")]
#[test]
fn serialize_deserialize() {
use serde_json;
let original = Static;
let serialized = serde_json::to_value(&original);
let deserialized = serde_json::from_value(serialized).unwrap();
assert_eq!(original, deserialized);
}
}