Implement Serialize and Deserialize for Bearer
This commit is contained in:
parent
bdd3cd8a68
commit
ba5b74c698
|
@ -1,5 +1,9 @@
|
|||
use std::marker::PhantomData;
|
||||
|
||||
use hyper::header;
|
||||
use rustc_serialize::json::Json;
|
||||
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||
use serde::{ser, de};
|
||||
|
||||
use super::{Token, Lifetime};
|
||||
use client::response::{FromResponse, ParseError, JsonHelper};
|
||||
|
@ -58,6 +62,97 @@ impl<L: Lifetime> FromResponse for Bearer<L> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<L: Lifetime + Serialize> Serialize for Bearer<L> {
|
||||
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
|
||||
serializer.visit_struct("Bearer", SerVisitor(self, 0))
|
||||
}
|
||||
}
|
||||
|
||||
struct SerVisitor<'a, L: Lifetime + Serialize + 'a>(&'a Bearer<L>, u8);
|
||||
impl<'a, L: Lifetime + Serialize + 'a> ser::MapVisitor for SerVisitor<'a, L> {
|
||||
fn visit<S: Serializer>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error> {
|
||||
self.1 += 1;
|
||||
match self.1 {
|
||||
1 => serializer.visit_struct_elt("access_token", &self.0.access_token).map(Some),
|
||||
2 => serializer.visit_struct_elt("scope", &self.0.scope).map(Some),
|
||||
3 => serializer.visit_struct_elt("lifetime", &self.0.lifetime).map(Some),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn len(&self) -> Option<usize> { Some(3) }
|
||||
}
|
||||
|
||||
impl<L: Lifetime + Deserialize> Deserialize for Bearer<L> {
|
||||
fn deserialize<D: Deserializer>(deserializer: &mut D) -> Result<Self, D::Error> {
|
||||
static FIELDS: &'static [&'static str] = &["access_token", "scope", "lifetime"];
|
||||
deserializer.visit_struct("Bearer", FIELDS, DeVisitor(PhantomData))
|
||||
}
|
||||
}
|
||||
|
||||
struct DeVisitor<L: Lifetime + Deserialize>(PhantomData<L>);
|
||||
impl<L: Lifetime + Deserialize> de::Visitor for DeVisitor<L> {
|
||||
type Value = Bearer<L>;
|
||||
|
||||
fn visit_map<V: de::MapVisitor>(&mut self, mut visitor: V) -> Result<Bearer<L>, V::Error> {
|
||||
let mut access_token = None;
|
||||
let mut scope = None;
|
||||
let mut lifetime = None;
|
||||
|
||||
loop {
|
||||
match try!(visitor.visit_key()) {
|
||||
Some(Field::AccessToken) => access_token = Some(try!(visitor.visit_value())),
|
||||
Some(Field::Scope) => scope = Some(try!(visitor.visit_value())),
|
||||
Some(Field::Lifetime) => lifetime = Some(try!(visitor.visit_value())),
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
|
||||
let access_token = match access_token {
|
||||
Some(s) => s,
|
||||
None => return visitor.missing_field("access_token"),
|
||||
};
|
||||
let lifetime = match lifetime {
|
||||
Some(l) => l,
|
||||
None => return visitor.missing_field("lifetime"),
|
||||
};
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok(Bearer {
|
||||
access_token: access_token,
|
||||
scope: scope,
|
||||
lifetime: lifetime,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
enum Field {
|
||||
AccessToken,
|
||||
Scope,
|
||||
Lifetime,
|
||||
}
|
||||
|
||||
impl Deserialize for Field {
|
||||
fn deserialize<D: Deserializer>(deserializer: &mut D) -> Result<Self, D::Error> {
|
||||
deserializer.visit(FieldVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
struct FieldVisitor;
|
||||
impl de::Visitor for FieldVisitor {
|
||||
type Value = Field;
|
||||
|
||||
fn visit_str<E: de::Error>(&mut self, value: &str) -> Result<Field, E> {
|
||||
match value {
|
||||
"access_token" => Ok(Field::AccessToken),
|
||||
"scope" => Ok(Field::Scope),
|
||||
"lifetime" => Ok(Field::Lifetime),
|
||||
_ => Err(de::Error::syntax("expected access_token, scope or lifetime")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::{UTC, Duration};
|
||||
|
|
Loading…
Reference in New Issue