Merge pull request #4 from programble/feature/serde
Implement Serde Serialize and Deserialize for tokens
This commit is contained in:
commit
41137fe3de
|
@ -16,7 +16,9 @@ readme = "README.md"
|
||||||
chrono = "0.2.17"
|
chrono = "0.2.17"
|
||||||
hyper = "0.7.0"
|
hyper = "0.7.0"
|
||||||
rustc-serialize = "0.3.16"
|
rustc-serialize = "0.3.16"
|
||||||
|
serde = "0.6.1"
|
||||||
url = "0.5.0"
|
url = "0.5.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
serde_json = "0.6.0"
|
||||||
yup-hyper-mock = "1.3.2"
|
yup-hyper-mock = "1.3.2"
|
||||||
|
|
19
src/lib.rs
19
src/lib.rs
|
@ -108,7 +108,8 @@
|
||||||
//!
|
//!
|
||||||
//! ### Persisting tokens
|
//! ### Persisting tokens
|
||||||
//!
|
//!
|
||||||
//! All token types implement `Encodable` and `Decodable` from `rustc_serialize`.
|
//! All token types implement `Encodable` / `Decodable` from `rustc_serialize` and `Serialize` /
|
||||||
|
//! `Deserialize` from `serde`.
|
||||||
//!
|
//!
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! # extern crate inth_oauth2;
|
//! # extern crate inth_oauth2;
|
||||||
|
@ -122,6 +123,18 @@
|
||||||
//! let json = json::encode(&token).unwrap();
|
//! let json = json::encode(&token).unwrap();
|
||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
|
//!
|
||||||
|
//! ```no_run
|
||||||
|
//! # extern crate inth_oauth2;
|
||||||
|
//! extern crate serde_json;
|
||||||
|
//! # use inth_oauth2::Client;
|
||||||
|
//! # use inth_oauth2::provider::Google;
|
||||||
|
//! # fn main() {
|
||||||
|
//! # let client = Client::<Google>::new(Default::default(), "", "", None);
|
||||||
|
//! # let token = client.request_token("").unwrap();
|
||||||
|
//! let json = serde_json::to_string(&token).unwrap();
|
||||||
|
//! # }
|
||||||
|
//! ```
|
||||||
|
|
||||||
#![warn(
|
#![warn(
|
||||||
missing_docs,
|
missing_docs,
|
||||||
|
@ -139,6 +152,7 @@
|
||||||
extern crate chrono;
|
extern crate chrono;
|
||||||
extern crate hyper;
|
extern crate hyper;
|
||||||
extern crate rustc_serialize;
|
extern crate rustc_serialize;
|
||||||
|
extern crate serde;
|
||||||
extern crate url;
|
extern crate url;
|
||||||
|
|
||||||
pub use token::{Token, Lifetime};
|
pub use token::{Token, Lifetime};
|
||||||
|
@ -148,3 +162,6 @@ pub mod token;
|
||||||
pub mod provider;
|
pub mod provider;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod client;
|
pub mod client;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
extern crate serde_json;
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use hyper::header;
|
use hyper::header;
|
||||||
use rustc_serialize::json::Json;
|
use rustc_serialize::json::Json;
|
||||||
|
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||||
|
use serde::{ser, de};
|
||||||
|
|
||||||
use super::{Token, Lifetime};
|
use super::{Token, Lifetime};
|
||||||
use client::response::{FromResponse, ParseError, JsonHelper};
|
use client::response::{FromResponse, ParseError, JsonHelper};
|
||||||
|
@ -58,10 +62,102 @@ 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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use chrono::{UTC, Duration};
|
use chrono::{UTC, Duration};
|
||||||
use rustc_serialize::json::Json;
|
use rustc_serialize::json::Json;
|
||||||
|
use serde_json;
|
||||||
|
|
||||||
use client::response::{FromResponse, ParseError};
|
use client::response::{FromResponse, ParseError};
|
||||||
use token::{Static, Expiring};
|
use token::{Static, Expiring};
|
||||||
|
@ -163,4 +259,16 @@ mod tests {
|
||||||
assert!(expiring.expires() > &UTC::now());
|
assert!(expiring.expires() > &UTC::now());
|
||||||
assert!(expiring.expires() <= &(UTC::now() + Duration::seconds(3600)));
|
assert!(expiring.expires() <= &(UTC::now() + Duration::seconds(3600)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialize_deserialize() {
|
||||||
|
let original = Bearer {
|
||||||
|
access_token: String::from("foo"),
|
||||||
|
scope: Some(String::from("bar")),
|
||||||
|
lifetime: Static,
|
||||||
|
};
|
||||||
|
let serialized = serde_json::to_value(&original);
|
||||||
|
let deserialized = serde_json::from_value(serialized).unwrap();
|
||||||
|
assert_eq!(original, deserialized);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
use chrono::{DateTime, UTC, Duration, TimeZone};
|
use chrono::{DateTime, UTC, Duration, TimeZone};
|
||||||
use rustc_serialize::json::Json;
|
use rustc_serialize::json::Json;
|
||||||
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
|
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
|
||||||
|
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||||
|
use serde::{ser, de};
|
||||||
|
|
||||||
use super::Lifetime;
|
use super::Lifetime;
|
||||||
use client::response::{FromResponse, ParseError, JsonHelper};
|
use client::response::{FromResponse, ParseError, JsonHelper};
|
||||||
|
@ -91,10 +93,96 @@ impl Decodable for Expiring {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Serialize for Expiring {
|
||||||
|
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
|
||||||
|
serializer.visit_struct("Expiring", SerVisitor(self, 0))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SerVisitor<'a>(&'a Expiring, u8);
|
||||||
|
impl<'a> ser::MapVisitor for SerVisitor<'a> {
|
||||||
|
fn visit<S: Serializer>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error> {
|
||||||
|
self.1 += 1;
|
||||||
|
match self.1 {
|
||||||
|
1 => serializer.visit_struct_elt("refresh_token", &self.0.refresh_token).map(Some),
|
||||||
|
2 => serializer.visit_struct_elt("expires", &self.0.expires.timestamp()).map(Some),
|
||||||
|
_ => Ok(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn len(&self) -> Option<usize> { Some(2) }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deserialize for Expiring {
|
||||||
|
fn deserialize<D: Deserializer>(deserializer: &mut D) -> Result<Self, D::Error> {
|
||||||
|
static FIELDS: &'static [&'static str] = &["refresh_token", "expires"];
|
||||||
|
deserializer.visit_struct("Expiring", FIELDS, DeVisitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DeVisitor;
|
||||||
|
impl de::Visitor for DeVisitor {
|
||||||
|
type Value = Expiring;
|
||||||
|
|
||||||
|
fn visit_map<V: de::MapVisitor>(&mut self, mut visitor: V) -> Result<Expiring, V::Error> {
|
||||||
|
let mut refresh_token = None;
|
||||||
|
let mut expires = None;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match try!(visitor.visit_key()) {
|
||||||
|
Some(Field::RefreshToken) => refresh_token = Some(try!(visitor.visit_value())),
|
||||||
|
Some(Field::Expires) => expires = Some(try!(visitor.visit_value())),
|
||||||
|
None => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let refresh_token = match refresh_token {
|
||||||
|
Some(s) => s,
|
||||||
|
None => return visitor.missing_field("refresh_token"),
|
||||||
|
};
|
||||||
|
let expires = match expires {
|
||||||
|
Some(i) => UTC.timestamp(i, 0),
|
||||||
|
None => return visitor.missing_field("expires"),
|
||||||
|
};
|
||||||
|
|
||||||
|
try!(visitor.end());
|
||||||
|
|
||||||
|
Ok(Expiring {
|
||||||
|
refresh_token: refresh_token,
|
||||||
|
expires: expires,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Field {
|
||||||
|
RefreshToken,
|
||||||
|
Expires,
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
"refresh_token" => Ok(Field::RefreshToken),
|
||||||
|
"expires" => Ok(Field::Expires),
|
||||||
|
_ => Err(de::Error::syntax("expected refresh_token or expires")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use chrono::{UTC, Duration, Timelike};
|
use chrono::{UTC, Duration, Timelike};
|
||||||
use rustc_serialize::json::{self, Json};
|
use rustc_serialize::json::{self, Json};
|
||||||
|
use serde_json;
|
||||||
|
|
||||||
use client::response::FromResponse;
|
use client::response::FromResponse;
|
||||||
use super::Expiring;
|
use super::Expiring;
|
||||||
|
@ -131,4 +219,15 @@ mod tests {
|
||||||
let decoded = json::decode(&json).unwrap();
|
let decoded = json::decode(&json).unwrap();
|
||||||
assert_eq!(expiring, decoded);
|
assert_eq!(expiring, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialize_deserialize() {
|
||||||
|
let original = Expiring {
|
||||||
|
refresh_token: String::from("foo"),
|
||||||
|
expires: UTC::now().with_nanosecond(0).unwrap(),
|
||||||
|
};
|
||||||
|
let serialized = serde_json::to_value(&original);
|
||||||
|
let deserialized = serde_json::from_value(serialized).unwrap();
|
||||||
|
assert_eq!(original, deserialized);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
use rustc_serialize::json::Json;
|
use rustc_serialize::json::Json;
|
||||||
|
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||||
|
use serde::de::impls::UnitVisitor;
|
||||||
|
|
||||||
use super::Lifetime;
|
use super::Lifetime;
|
||||||
use client::response::{FromResponse, ParseError, JsonHelper};
|
use client::response::{FromResponse, ParseError, JsonHelper};
|
||||||
|
@ -21,9 +23,23 @@ impl FromResponse for Static {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Serialize for Static {
|
||||||
|
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
|
||||||
|
serializer.visit_unit_struct("Static")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deserialize for Static {
|
||||||
|
fn deserialize<D: Deserializer>(deserializer: &mut D) -> Result<Self, D::Error> {
|
||||||
|
deserializer.visit_unit_struct("Static", UnitVisitor)
|
||||||
|
.and(Ok(Static))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use rustc_serialize::json::Json;
|
use rustc_serialize::json::Json;
|
||||||
|
use serde_json;
|
||||||
|
|
||||||
use client::response::{FromResponse, ParseError};
|
use client::response::{FromResponse, ParseError};
|
||||||
use super::Static;
|
use super::Static;
|
||||||
|
@ -42,4 +58,12 @@ mod tests {
|
||||||
Static::from_response(&json).unwrap_err()
|
Static::from_response(&json).unwrap_err()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialize_deserialize() {
|
||||||
|
let original = Static;
|
||||||
|
let serialized = serde_json::to_value(&original);
|
||||||
|
let deserialized = serde_json::from_value(serialized).unwrap();
|
||||||
|
assert_eq!(original, deserialized);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue