Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Serialize and Deserialze for Host
Browse files Browse the repository at this point in the history
cbrewster committed Jan 23, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 9f5efbf commit 3c5fe48
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "url"
version = "1.3.0"
version = "1.3.1"
authors = ["The rust-url developers"]

description = "URL library for Rust, based on the WHATWG URL Standard"
24 changes: 24 additions & 0 deletions src/host.rs
Original file line number Diff line number Diff line change
@@ -57,6 +57,30 @@ pub enum Host<S=String> {
Ipv6(Ipv6Addr),
}

#[cfg(feature="serde")]
impl ::serde::Serialize for Host {
fn serialize<R>(&self, serializer: &mut R) -> Result<(), R::Error> where R: ::serde::Serializer {
use std::net::IpAddr;
match *self {
Host::Domain(ref domain) => Ok(domain),
Host::Ipv4(addr) => Err(IpAddr::V4(addr)),
Host::Ipv6(addr) => Err(IpAddr::V6(addr)),
}.serialize(serializer)
}
}

#[cfg(feature="serde")]
impl::serde::Deserialize for Host {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> where D: ::serde::Deserializer {
use std::net::IpAddr;
Ok(match try!(::serde::Deserialize::deserialize(deserializer)) {
Ok(s) => Host::Domain(s),
Err(IpAddr::V4(addr)) => Host::Ipv4(addr),
Err(IpAddr::V6(addr)) => Host::Ipv6(addr),
})
}
}

#[cfg(feature = "heapsize")]
impl<S: HeapSizeOf> HeapSizeOf for Host<S> {
fn heap_size_of_children(&self) -> usize {

0 comments on commit 3c5fe48

Please sign in to comment.