Skip to content

Commit e305a2e

Browse files
committed
Merge pull request #632 from manuel-woelker/issue-629
feat(uri): implement fmt::Display for RequestUri (resolves #629)
2 parents da2d293 + 80931cf commit e305a2e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/uri.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! HTTP RequestUris
2+
use std::fmt::{Display, self};
23
use std::str::FromStr;
34
use url::Url;
45
use url::ParseError as UrlError;
@@ -72,6 +73,17 @@ impl FromStr for RequestUri {
7273
}
7374
}
7475

76+
impl Display for RequestUri {
77+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78+
match *self {
79+
RequestUri::AbsolutePath(ref path) => f.write_str(path),
80+
RequestUri::AbsoluteUri(ref url) => write!(f, "{}", url),
81+
RequestUri::Authority(ref path) => f.write_str(path),
82+
RequestUri::Star => f.write_str("*")
83+
}
84+
}
85+
}
86+
7587
#[test]
7688
fn test_uri_fromstr() {
7789
fn read(s: &str, result: RequestUri) {
@@ -83,3 +95,16 @@ fn test_uri_fromstr() {
8395
read("hyper.rs", RequestUri::Authority("hyper.rs".to_owned()));
8496
read("/", RequestUri::AbsolutePath("/".to_owned()));
8597
}
98+
99+
#[test]
100+
fn test_uri_display() {
101+
fn assert_display(expected_string: &str, request_uri: RequestUri) {
102+
assert_eq!(expected_string, format!("{}", request_uri));
103+
}
104+
105+
assert_display("*", RequestUri::Star);
106+
assert_display("http://hyper.rs/", RequestUri::AbsoluteUri(Url::parse("http://hyper.rs/").unwrap()));
107+
assert_display("hyper.rs", RequestUri::Authority("hyper.rs".to_owned()));
108+
assert_display("/", RequestUri::AbsolutePath("/".to_owned()));
109+
110+
}

0 commit comments

Comments
 (0)