feat(uri): implement fmt::Display for RequestUri (resolves #629)

This commit is contained in:
Manuel Woelker
2015-08-18 22:28:23 +02:00
parent da2d29309a
commit 80931cf4c3

View File

@@ -1,4 +1,5 @@
//! HTTP RequestUris //! HTTP RequestUris
use std::fmt::{Display, self};
use std::str::FromStr; use std::str::FromStr;
use url::Url; use url::Url;
use url::ParseError as UrlError; use url::ParseError as UrlError;
@@ -72,6 +73,17 @@ impl FromStr for RequestUri {
} }
} }
impl Display for RequestUri {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
RequestUri::AbsolutePath(ref path) => f.write_str(path),
RequestUri::AbsoluteUri(ref url) => write!(f, "{}", url),
RequestUri::Authority(ref path) => f.write_str(path),
RequestUri::Star => f.write_str("*")
}
}
}
#[test] #[test]
fn test_uri_fromstr() { fn test_uri_fromstr() {
fn read(s: &str, result: RequestUri) { fn read(s: &str, result: RequestUri) {
@@ -83,3 +95,16 @@ fn test_uri_fromstr() {
read("hyper.rs", RequestUri::Authority("hyper.rs".to_owned())); read("hyper.rs", RequestUri::Authority("hyper.rs".to_owned()));
read("/", RequestUri::AbsolutePath("/".to_owned())); read("/", RequestUri::AbsolutePath("/".to_owned()));
} }
#[test]
fn test_uri_display() {
fn assert_display(expected_string: &str, request_uri: RequestUri) {
assert_eq!(expected_string, format!("{}", request_uri));
}
assert_display("*", RequestUri::Star);
assert_display("http://hyper.rs/", RequestUri::AbsoluteUri(Url::parse("http://hyper.rs/").unwrap()));
assert_display("hyper.rs", RequestUri::Authority("hyper.rs".to_owned()));
assert_display("/", RequestUri::AbsolutePath("/".to_owned()));
}