Skip to content

Commit

Permalink
feat(method): implement AsRef<str> for Method
Browse files Browse the repository at this point in the history
This lets us obtain the string representation of the method in a
convenient and efficient manner.
  • Loading branch information
mlalic authored and seanmonstar committed May 5, 2015
1 parent a1e59fc commit c29af72
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/method.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! The HTTP request method
use std::fmt;
use std::str::FromStr;
use std::convert::AsRef;

use error::HttpError;
use self::Method::{Options, Get, Post, Put, Delete, Head, Trace, Connect, Patch,
Expand Down Expand Up @@ -38,6 +39,23 @@ pub enum Method {
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 {
/// Whether a method is considered "safe", meaning the request is
/// essentially read-only.
Expand Down Expand Up @@ -147,4 +165,12 @@ mod tests {
counter.insert(Get, 1);
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");
}
}

0 comments on commit c29af72

Please sign in to comment.