From 133b64ef39910c6a83b90e120cea1bfea3ab39d3 Mon Sep 17 00:00:00 2001 From: Tomasz Date: Sun, 7 May 2017 17:35:06 +0100 Subject: [PATCH 1/4] Add Display for HostAndPort --- src/host.rs | 9 +++++++++ tests/unit.rs | 26 +++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/host.rs b/src/host.rs index 6eae31dd..15492fae 100644 --- a/src/host.rs +++ b/src/host.rs @@ -192,6 +192,15 @@ impl<'a> HostAndPort<&'a str> { } } +impl> fmt::Display for HostAndPort { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + try!(self.host.fmt(f)); + try!(f.write_str(":")); + self.port.fmt(f) + } +} + + impl> ToSocketAddrs for HostAndPort { type Iter = SocketAddrs; diff --git a/tests/unit.rs b/tests/unit.rs index 6739956f..5955f361 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -13,7 +13,7 @@ extern crate url; use std::borrow::Cow; use std::net::{Ipv4Addr, Ipv6Addr}; use std::path::{Path, PathBuf}; -use url::{Host, Url, form_urlencoded}; +use url::{Host, HostAndPort, Url, form_urlencoded}; #[test] fn size() { @@ -254,6 +254,30 @@ fn test_form_serialize() { assert_eq!(encoded, "foo=%C3%A9%26&bar=&foo=%23"); } +#[test] +fn host_and_port_display() { + let data = [ + (HostAndPort{ host: Host::Domain("www.mozilla.org"), port: 80}, "www.mozilla.org:80"), + ( + HostAndPort{ host: Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)), port: 65535 }, + "1.35.33.49:65535" + ), + ( + HostAndPort{ + host: Host::Ipv6(Ipv6Addr::new( + 0x2001, 0x0db8, 0x85a3, 0x08d3, 0x1319, 0x8a2e, 0x0370, 0x7344 + )), + port: 1337 + }, + "[2001:db8:85a3:8d3:1319:8a2e:370:7344]:1337" + ) + ]; + + for &(ref input, result) in &data { + assert_eq!(format!("{}", input), result) + } +} + #[test] /// https://github.com/servo/rust-url/issues/25 fn issue_25() { From 8f6e4f9ff77f1fdb9b39809b6cea6ae43d301bff Mon Sep 17 00:00:00 2001 From: Tomasz Date: Tue, 9 May 2017 21:43:28 +0100 Subject: [PATCH 2/4] Address code review issues --- src/host.rs | 4 ++-- tests/unit.rs | 36 +++++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/host.rs b/src/host.rs index 15492fae..65ce8301 100644 --- a/src/host.rs +++ b/src/host.rs @@ -166,8 +166,8 @@ impl> fmt::Display for Host { Host::Domain(ref domain) => domain.as_ref().fmt(f), Host::Ipv4(ref addr) => addr.fmt(f), Host::Ipv6(ref addr) => { - try!(f.write_str("[")); - try!(write_ipv6(addr, f)); + f.write_str("[")?; + write_ipv6(addr, f)?; f.write_str("]") } } diff --git a/tests/unit.rs b/tests/unit.rs index 5955f361..ba1443ed 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -256,26 +256,32 @@ fn test_form_serialize() { #[test] fn host_and_port_display() { - let data = [ - (HostAndPort{ host: Host::Domain("www.mozilla.org"), port: 80}, "www.mozilla.org:80"), - ( - HostAndPort{ host: Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)), port: 65535 }, - "1.35.33.49:65535" + assert_eq!( + format!( + "{}", + HostAndPort{ host: Host::Domain("www.mozilla.org"), port: 80} + ), + "www.mozilla.org:80" + ); + assert_eq!( + format!( + "{}", + HostAndPort::{ host: Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)), port: 65535 } ), - ( - HostAndPort{ + "1.35.33.49:65535" + ); + assert_eq!( + format!( + "{}", + HostAndPort::{ host: Host::Ipv6(Ipv6Addr::new( 0x2001, 0x0db8, 0x85a3, 0x08d3, 0x1319, 0x8a2e, 0x0370, 0x7344 )), port: 1337 - }, - "[2001:db8:85a3:8d3:1319:8a2e:370:7344]:1337" - ) - ]; - - for &(ref input, result) in &data { - assert_eq!(format!("{}", input), result) - } + }) + , + "[2001:db8:85a3:8d3:1319:8a2e:370:7344]:1337" + ) } #[test] From cf77f2d94b7db18c981d94ef1bb22aab27a6e484 Mon Sep 17 00:00:00 2001 From: Tomasz Date: Tue, 9 May 2017 21:49:12 +0100 Subject: [PATCH 3/4] Add ? operator for Display for HostAndPort --- src/host.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/host.rs b/src/host.rs index 65ce8301..653f83d3 100644 --- a/src/host.rs +++ b/src/host.rs @@ -194,8 +194,8 @@ impl<'a> HostAndPort<&'a str> { impl> fmt::Display for HostAndPort { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - try!(self.host.fmt(f)); - try!(f.write_str(":")); + self.host.fmt(f)?; + f.write_str(":")?; self.port.fmt(f) } } From a6421f6868c9203ae793fe5654e8dd4595f2778d Mon Sep 17 00:00:00 2001 From: Tomasz Date: Tue, 9 May 2017 21:55:12 +0100 Subject: [PATCH 4/4] Revert `?` operator for Display for Host --- src/host.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/host.rs b/src/host.rs index 653f83d3..26ec0605 100644 --- a/src/host.rs +++ b/src/host.rs @@ -166,8 +166,8 @@ impl> fmt::Display for Host { Host::Domain(ref domain) => domain.as_ref().fmt(f), Host::Ipv4(ref addr) => addr.fmt(f), Host::Ipv6(ref addr) => { - f.write_str("[")?; - write_ipv6(addr, f)?; + try!(f.write_str("[")); + try!(write_ipv6(addr, f)); f.write_str("]") } }