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,6 +58,16 @@ impl<L: Lifetime> FromResponse 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))
@ -152,12 +158,12 @@ impl de::Visitor for FieldVisitor {
}
}
}
}
#[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,6 +69,14 @@ impl Decodable 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))
@ -145,12 +151,12 @@ impl de::Visitor for FieldVisitor {
}
}
}
}
#[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,6 +91,14 @@ impl Decodable 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))
@ -177,12 +183,12 @@ impl de::Visitor for FieldVisitor {
}
}
}
}
#[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,6 +21,13 @@ impl FromResponse 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")
@ -35,11 +40,11 @@ impl Deserialize for Static {
.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();