perf(method): improve logic of parsing a Method

This commit is contained in:
Sean McArthur
2017-06-23 18:56:14 -07:00
parent c165db495e
commit 42f5f78de0

View File

@@ -87,25 +87,49 @@ impl Method {
} }
} }
macro_rules! from_str {
($s:ident, { $($n:pat => { $($text:pat => $var:ident,)* },)* }) => ({
let s = $s;
match s.len() {
$(
$n => match s {
$(
$text => return Ok($var),
)*
_ => {},
},
)*
0 => return Err(::Error::Method),
_ => {},
}
Ok(Extension(s.to_owned()))
})
}
impl FromStr for Method { impl FromStr for Method {
type Err = Error; type Err = Error;
fn from_str(s: &str) -> Result<Method, Error> { fn from_str(s: &str) -> Result<Method, Error> {
if s == "" { from_str!(s, {
Err(Error::Method) 3 => {
} else {
Ok(match s {
"OPTIONS" => Options,
"GET" => Get, "GET" => Get,
"POST" => Post,
"PUT" => Put, "PUT" => Put,
"DELETE" => Delete, },
4 => {
"HEAD" => Head, "HEAD" => Head,
"TRACE" => Trace, "POST" => Post,
"CONNECT" => Connect, },
5 => {
"PATCH" => Patch, "PATCH" => Patch,
_ => Extension(s.to_owned()) "TRACE" => Trace,
}) },
} 6 => {
"DELETE" => Delete,
},
7 => {
"OPTIONS" => Options,
"CONNECT" => Connect,
},
})
} }
} }