Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional serialization with `serde` #118

Merged
merged 1 commit into from Jul 10, 2015
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -14,12 +14,19 @@ license = "MIT/Apache-2.0"
[features]

query_encoding = ["encoding"]
serde_serialization = ["serde"]

[dependencies.encoding]

version = "0.2"
optional = true

[dependencies.serde]

version = "*"
optional = true

[dependencies]
rustc-serialize = "0.3"
matches = "0.1"

@@ -1,9 +1,10 @@
test:
cargo test --features query_encoding
cargo test --features serde_serialization
cargo test

doc:
cargo doc --features query_encoding
cargo doc --features "query_encoding serde_serialization"
@echo '<meta http-equiv=refresh content=0;url=url/index.html>' > target/doc/index.html
@cp github.png target/doc/

@@ -123,10 +123,16 @@ extern crate rustc_serialize;
#[macro_use]
extern crate matches;

#[cfg(feature="serde_serialization")]
extern crate serde;

use std::fmt::{self, Formatter};
use std::str;
use std::path::{Path, PathBuf};

#[cfg(feature="serde_serialization")]
use std::str::FromStr;

pub use host::{Host, Ipv6Address};
pub use parser::{ErrorHandler, ParseResult, ParseError};

@@ -768,6 +774,26 @@ impl rustc_serialize::Decodable for Url {
}
}

/// Serializes this URL into a `serde` stream.
///
/// This implementation is only available if the `serde_serialization` Cargo feature is enabled.
#[cfg(feature="serde_serialization")]
impl serde::Serialize for Url {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: serde::Serializer {
format!("{}", self).serialize(serializer)
}
}

/// Deserializes this URL from a `serde` stream.
///
/// This implementation is only available if the `serde_serialization` Cargo feature is enabled.
#[cfg(feature="serde_serialization")]
impl serde::Deserialize for Url {
fn deserialize<D>(deserializer: &mut D) -> Result<Url, D::Error> where D: serde::Deserializer {
let string_representation: String = try!(serde::Deserialize::deserialize(deserializer));
Ok(FromStr::from_str(&string_representation[..]).unwrap())
}
}

impl fmt::Display for Url {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.