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,27 +87,51 @@ 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 {
type Err = Error;
fn from_str(s: &str) -> Result<Method, Error> {
if s == "" {
Err(Error::Method)
} else {
Ok(match s {
"OPTIONS" => Options,
from_str!(s, {
3 => {
"GET" => Get,
"POST" => Post,
"PUT" => Put,
"DELETE" => Delete,
},
4 => {
"HEAD" => Head,
"TRACE" => Trace,
"CONNECT" => Connect,
"POST" => Post,
},
5 => {
"PATCH" => Patch,
_ => Extension(s.to_owned())
"TRACE" => Trace,
},
6 => {
"DELETE" => Delete,
},
7 => {
"OPTIONS" => Options,
"CONNECT" => Connect,
},
})
}
}
}
impl fmt::Display for Method {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {