feat(http): add optional serialization of common types via serde

This is behind a Cargo feature to avoid forcing downstream users to
depend on `serde`. It is needed for Servo IPC to work.
This commit is contained in:
Patrick Walton
2015-07-10 13:16:11 -07:00
parent 623824d8b2
commit 87de1b77bc
6 changed files with 140 additions and 0 deletions

View File

@@ -7,6 +7,9 @@ use error::Error;
use self::Method::{Options, Get, Post, Put, Delete, Head, Trace, Connect, Patch,
Extension};
#[cfg(feature = "serde-serialization")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// The Request Method (VERB)
///
/// Currently includes 8 variants representing the 8 methods defined in
@@ -125,6 +128,21 @@ impl fmt::Display for Method {
}
}
#[cfg(feature = "serde-serialization")]
impl Serialize for Method {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
format!("{}", self).serialize(serializer)
}
}
#[cfg(feature = "serde-serialization")]
impl Deserialize for Method {
fn deserialize<D>(deserializer: &mut D) -> Result<Method, D::Error> where D: Deserializer {
let string_representation: String = try!(Deserialize::deserialize(deserializer));
Ok(FromStr::from_str(&string_representation[..]).unwrap())
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;