From c29af729726ae782bece5e790bce02b0d3ab9ef9 Mon Sep 17 00:00:00 2001 From: Marko Lalic Date: Mon, 13 Apr 2015 13:41:05 +0200 Subject: [PATCH] feat(method): implement `AsRef` for `Method` This lets us obtain the string representation of the method in a convenient and efficient manner. --- src/method.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/method.rs b/src/method.rs index ec58cf63..ab841c98 100644 --- a/src/method.rs +++ b/src/method.rs @@ -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, @@ -38,6 +39,23 @@ pub enum Method { Extension(String) } +impl AsRef 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. @@ -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"); + } }