Skip to content

Commit c29af72

Browse files
mlalicseanmonstar
authored andcommitted
feat(method): implement AsRef<str> for Method
This lets us obtain the string representation of the method in a convenient and efficient manner.
1 parent a1e59fc commit c29af72

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/method.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! The HTTP request method
22
use std::fmt;
33
use std::str::FromStr;
4+
use std::convert::AsRef;
45

56
use error::HttpError;
67
use self::Method::{Options, Get, Post, Put, Delete, Head, Trace, Connect, Patch,
@@ -38,6 +39,23 @@ pub enum Method {
3839
Extension(String)
3940
}
4041

42+
impl AsRef<str> for Method {
43+
fn as_ref(&self) -> &str {
44+
match *self {
45+
Options => "OPTIONS",
46+
Get => "GET",
47+
Post => "POST",
48+
Put => "PUT",
49+
Delete => "DELETE",
50+
Head => "HEAD",
51+
Trace => "TRACE",
52+
Connect => "CONNECT",
53+
Patch => "PATCH",
54+
Extension(ref s) => s.as_ref()
55+
}
56+
}
57+
}
58+
4159
impl Method {
4260
/// Whether a method is considered "safe", meaning the request is
4361
/// essentially read-only.
@@ -147,4 +165,12 @@ mod tests {
147165
counter.insert(Get, 1);
148166
assert_eq!(Some(&1), counter.get(&Get));
149167
}
168+
169+
#[test]
170+
fn test_as_str() {
171+
assert_eq!(Get.as_ref(), "GET");
172+
assert_eq!(Post.as_ref(), "POST");
173+
assert_eq!(Put.as_ref(), "PUT");
174+
assert_eq!(Extension("MOVE".to_string()).as_ref(), "MOVE");
175+
}
150176
}

0 commit comments

Comments
 (0)