use std::str::FromStr;
use url::Url;
use url::ParseError as UrlError;
use Error;
#[derive(Debug, PartialEq, Clone)]
pub enum RequestUri {
AbsolutePath(String),
AbsoluteUri(Url),
Authority(String),
Star,
}
impl FromStr for RequestUri {
type Err = Error;
fn from_str(s: &str) -> Result<RequestUri, Error> {
let bytes = s.as_bytes();
if bytes == [] {
Err(Error::Uri(UrlError::InvalidCharacter))
} else if bytes == b"*" {
Ok(RequestUri::Star)
} else if bytes.starts_with(b"/") {
Ok(RequestUri::AbsolutePath(s.to_string()))
} else if bytes.contains(&b'/') {
Ok(RequestUri::AbsoluteUri(try!(Url::parse(s))))
} else {
let mut temp = "http://".to_string();
temp.push_str(s);
try!(Url::parse(&temp[..]));
todo!("compare vs u.authority()");
Ok(RequestUri::Authority(s.to_string()))
}
}
}
#[test]
fn test_uri_fromstr() {
fn read(s: &str, result: RequestUri) {
assert_eq!(s.parse::<RequestUri>().unwrap(), result);
}
read("*", RequestUri::Star);
read("http://hyper.rs/", RequestUri::AbsoluteUri(Url::parse("http://hyper.rs/").unwrap()));
read("hyper.rs", RequestUri::Authority("hyper.rs".to_string()));
read("/", RequestUri::AbsolutePath("/".to_string()));
}