feat(method): implement AsRef<str> for Method

This lets us obtain the string representation of the method in a
convenient and efficient manner.
This commit is contained in:
Marko Lalic
2015-04-13 13:41:05 +02:00
committed by Sean McArthur
parent a1e59fc6f9
commit c29af72972

View File

@@ -1,6 +1,7 @@
//! The HTTP request method //! The HTTP request method
use std::fmt; use std::fmt;
use std::str::FromStr; use std::str::FromStr;
use std::convert::AsRef;
use error::HttpError; use error::HttpError;
use self::Method::{Options, Get, Post, Put, Delete, Head, Trace, Connect, Patch, use self::Method::{Options, Get, Post, Put, Delete, Head, Trace, Connect, Patch,
@@ -38,6 +39,23 @@ pub enum Method {
Extension(String) Extension(String)
} }
impl AsRef<str> for Method {
fn as_ref(&self) -> &str {
match *self {
Options => "OPTIONS",
Get => "GET",
Post => "POST",
Put => "PUT",
Delete => "DELETE",
Head => "HEAD",
Trace => "TRACE",
Connect => "CONNECT",
Patch => "PATCH",
Extension(ref s) => s.as_ref()
}
}
}
impl Method { impl Method {
/// Whether a method is considered "safe", meaning the request is /// Whether a method is considered "safe", meaning the request is
/// essentially read-only. /// essentially read-only.
@@ -147,4 +165,12 @@ mod tests {
counter.insert(Get, 1); counter.insert(Get, 1);
assert_eq!(Some(&1), counter.get(&Get)); assert_eq!(Some(&1), counter.get(&Get));
} }
#[test]
fn test_as_str() {
assert_eq!(Get.as_ref(), "GET");
assert_eq!(Post.as_ref(), "POST");
assert_eq!(Put.as_ref(), "PUT");
assert_eq!(Extension("MOVE".to_string()).as_ref(), "MOVE");
}
} }