Move serde support to a (default) feature

This commit is contained in:
Curtis McEnroe 2016-03-12 20:07:49 -05:00
parent fd5f776537
commit 4a88c8779a
6 changed files with 276 additions and 230 deletions

View File

@ -12,13 +12,19 @@ documentation = "https://cmcenroe.me/inth-oauth2/inth_oauth2"
repository = "https://github.com/programble/inth-oauth2"
readme = "README.md"
[features]
default = ["serde"]
[dependencies]
chrono = "0.2.17"
hyper = "0.7.0"
rustc-serialize = "0.3.16"
serde = "0.7.0"
url = "0.5.0"
[dependencies.serde]
version = "0.7.0"
optional = true
[dev-dependencies]
serde_json = "0.7.0"
yup-hyper-mock = "1.3.2"

View File

@ -113,7 +113,7 @@
//! ### Persisting tokens
//!
//! All token types implement `Encodable` / `Decodable` from `rustc_serialize` and `Serialize` /
//! `Deserialize` from `serde`.
//! `Deserialize` from `serde` (with the default `serde` feature).
//!
//! ```no_run
//! # extern crate inth_oauth2;
@ -134,12 +134,15 @@
//! extern crate serde_json;
//! # use inth_oauth2::Client;
//! # use inth_oauth2::provider::google::Installed;
//! # #[cfg(feature = "serde")]
//! # fn main() {
//! # let http_client = Default::default();
//! # let client = Client::<Installed>::new(String::new(), String::new(), None);
//! # let token = client.request_token(&http_client, "").unwrap();
//! let json = serde_json::to_string(&token).unwrap();
//! # }
//! # #[cfg(not(feature = "serde"))]
//! # fn main() { }
//! ```
#![warn(
@ -158,9 +161,11 @@
extern crate chrono;
extern crate hyper;
extern crate rustc_serialize;
extern crate serde;
extern crate url;
#[cfg(feature = "serde")]
extern crate serde;
pub use token::{Token, Lifetime};
pub use client::{Client, ClientError};
@ -169,5 +174,5 @@ pub mod provider;
pub mod error;
pub mod client;
#[cfg(test)]
#[cfg(all(test, feature = "serde"))]
extern crate serde_json;

View File

@ -1,9 +1,5 @@
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};
@ -62,14 +58,24 @@ impl<L: Lifetime> FromResponse for Bearer<L> {
}
}
impl<L: Lifetime + Serialize> Serialize for Bearer<L> {
#[cfg(feature = "serde")]
mod serde {
use std::marker::PhantomData;
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use serde::{ser, de};
use token::Lifetime;
use super::Bearer;
impl<L: Lifetime + Serialize> Serialize for Bearer<L> {
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
serializer.serialize_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> {
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 {
@ -81,17 +87,17 @@ impl<'a, L: Lifetime + Serialize + 'a> ser::MapVisitor for SerVisitor<'a, L> {
}
fn len(&self) -> Option<usize> { Some(3) }
}
}
impl<L: Lifetime + Deserialize> Deserialize for Bearer<L> {
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.deserialize_struct("Bearer", FIELDS, DeVisitor(PhantomData))
}
}
}
struct DeVisitor<L: Lifetime + Deserialize>(PhantomData<L>);
impl<L: Lifetime + Deserialize> de::Visitor for DeVisitor<L> {
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> {
@ -125,22 +131,22 @@ impl<L: Lifetime + Deserialize> de::Visitor for DeVisitor<L> {
lifetime: lifetime,
})
}
}
}
enum Field {
enum Field {
AccessToken,
Scope,
Lifetime,
}
}
impl Deserialize for Field {
impl Deserialize for Field {
fn deserialize<D: Deserializer>(deserializer: &mut D) -> Result<Self, D::Error> {
deserializer.deserialize(FieldVisitor)
}
}
}
struct FieldVisitor;
impl de::Visitor for FieldVisitor {
struct FieldVisitor;
impl de::Visitor for FieldVisitor {
type Value = Field;
fn visit_str<E: de::Error>(&mut self, value: &str) -> Result<Field, E> {
@ -151,13 +157,13 @@ impl de::Visitor for FieldVisitor {
_ => Err(de::Error::custom("expected access_token, scope or lifetime")),
}
}
}
}
#[cfg(test)]
mod tests {
use chrono::{UTC, Duration};
use rustc_serialize::json::Json;
use serde_json;
use client::response::{FromResponse, ParseError};
use token::{Static, Refresh};
@ -260,8 +266,11 @@ mod tests {
assert!(refresh.expires() <= &(UTC::now() + Duration::seconds(3600)));
}
#[cfg(feature = "serde")]
#[test]
fn serialize_deserialize() {
use serde_json;
let original = Bearer {
access_token: String::from("foo"),
scope: Some(String::from("bar")),

View File

@ -1,8 +1,6 @@
use chrono::{DateTime, UTC, Duration, TimeZone};
use rustc_serialize::json::Json;
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use serde::{ser, de};
use super::Lifetime;
use client::response::{FromResponse, ParseError, JsonHelper};
@ -71,14 +69,22 @@ impl Decodable for Expiring {
}
}
impl Serialize for Expiring {
#[cfg(feature = "serde")]
mod serde {
use chrono::{UTC, TimeZone};
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use serde::{ser, de};
use super::Expiring;
impl Serialize for Expiring {
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
serializer.serialize_struct("Expiring", SerVisitor(self, 0))
}
}
}
struct SerVisitor<'a>(&'a Expiring, u8);
impl<'a> ser::MapVisitor for SerVisitor<'a> {
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 {
@ -88,17 +94,17 @@ impl<'a> ser::MapVisitor for SerVisitor<'a> {
}
fn len(&self) -> Option<usize> { Some(1) }
}
}
impl Deserialize for Expiring {
impl Deserialize for Expiring {
fn deserialize<D: Deserializer>(deserializer: &mut D) -> Result<Self, D::Error> {
static FIELDS: &'static [&'static str] = &["expires"];
deserializer.deserialize_struct("Expiring", FIELDS, DeVisitor)
}
}
}
struct DeVisitor;
impl de::Visitor for 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> {
@ -122,20 +128,20 @@ impl de::Visitor for DeVisitor {
expires: expires,
})
}
}
}
enum Field {
enum Field {
Expires,
}
}
impl Deserialize for Field {
impl Deserialize for Field {
fn deserialize<D: Deserializer>(deserializer: &mut D) -> Result<Self, D::Error> {
deserializer.deserialize(FieldVisitor)
}
}
}
struct FieldVisitor;
impl de::Visitor for FieldVisitor {
struct FieldVisitor;
impl de::Visitor for FieldVisitor {
type Value = Field;
fn visit_str<E: de::Error>(&mut self, value: &str) -> Result<Field, E> {
@ -144,13 +150,13 @@ impl de::Visitor for FieldVisitor {
_ => Err(de::Error::custom("expected expires")),
}
}
}
}
#[cfg(test)]
mod tests {
use chrono::{UTC, Duration, Timelike};
use rustc_serialize::json::{self, Json};
use serde_json;
use client::response::FromResponse;
use super::Expiring;
@ -173,8 +179,11 @@ mod tests {
assert_eq!(expiring, decoded);
}
#[cfg(feature = "serde")]
#[test]
fn serialize_deserialize() {
use serde_json;
let original = Expiring {
expires: UTC::now().with_nanosecond(0).unwrap(),
};

View File

@ -1,8 +1,6 @@
use chrono::{DateTime, UTC, Duration, TimeZone};
use rustc_serialize::json::Json;
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use serde::{ser, de};
use super::Lifetime;
use client::response::{FromResponse, ParseError, JsonHelper};
@ -93,14 +91,22 @@ impl Decodable for Refresh {
}
}
impl Serialize for Refresh {
#[cfg(feature = "serde")]
mod serde {
use chrono::{UTC, TimeZone};
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use serde::{ser, de};
use super::Refresh;
impl Serialize for Refresh {
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
serializer.serialize_struct("Refresh", SerVisitor(self, 0))
}
}
}
struct SerVisitor<'a>(&'a Refresh, u8);
impl<'a> ser::MapVisitor for SerVisitor<'a> {
struct SerVisitor<'a>(&'a Refresh, 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 {
@ -111,17 +117,17 @@ impl<'a> ser::MapVisitor for SerVisitor<'a> {
}
fn len(&self) -> Option<usize> { Some(2) }
}
}
impl Deserialize for Refresh {
impl Deserialize for Refresh {
fn deserialize<D: Deserializer>(deserializer: &mut D) -> Result<Self, D::Error> {
static FIELDS: &'static [&'static str] = &["refresh_token", "expires"];
deserializer.deserialize_struct("Refresh", FIELDS, DeVisitor)
}
}
}
struct DeVisitor;
impl de::Visitor for DeVisitor {
struct DeVisitor;
impl de::Visitor for DeVisitor {
type Value = Refresh;
fn visit_map<V: de::MapVisitor>(&mut self, mut visitor: V) -> Result<Refresh, V::Error> {
@ -152,21 +158,21 @@ impl de::Visitor for DeVisitor {
expires: expires,
})
}
}
}
enum Field {
enum Field {
RefreshToken,
Expires,
}
}
impl Deserialize for Field {
impl Deserialize for Field {
fn deserialize<D: Deserializer>(deserializer: &mut D) -> Result<Self, D::Error> {
deserializer.deserialize(FieldVisitor)
}
}
}
struct FieldVisitor;
impl de::Visitor for FieldVisitor {
struct FieldVisitor;
impl de::Visitor for FieldVisitor {
type Value = Field;
fn visit_str<E: de::Error>(&mut self, value: &str) -> Result<Field, E> {
@ -176,13 +182,13 @@ impl de::Visitor for FieldVisitor {
_ => Err(de::Error::custom("expected refresh_token or expires")),
}
}
}
}
#[cfg(test)]
mod tests {
use chrono::{UTC, Duration, Timelike};
use rustc_serialize::json::{self, Json};
use serde_json;
use client::response::FromResponse;
use super::Refresh;
@ -220,8 +226,11 @@ mod tests {
assert_eq!(refresh, decoded);
}
#[cfg(feature = "serde")]
#[test]
fn serialize_deserialize() {
use serde_json;
let original = Refresh {
refresh_token: String::from("foo"),
expires: UTC::now().with_nanosecond(0).unwrap(),

View File

@ -1,6 +1,4 @@
use rustc_serialize::json::Json;
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use serde::de::impls::UnitVisitor;
use super::Lifetime;
use client::response::{FromResponse, ParseError, JsonHelper};
@ -23,23 +21,30 @@ impl FromResponse for Static {
}
}
impl Serialize for 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 {
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 serde_json;
use client::response::{FromResponse, ParseError};
use super::Static;
@ -59,8 +64,11 @@ mod tests {
);
}
#[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();