1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
use std::{convert::TryFrom, error::Error, fmt, str::FromStr}; #[derive(Copy, Clone, PartialEq, Eq)] pub enum Method { GET, HEAD, POST, PUT, DELETE, PATCH, } impl Method { pub fn as_str(&self) -> &str { match self { Method::GET => "GET", Method::HEAD => "HEAD", Method::POST => "POST", Method::PUT => "PUT", Method::DELETE => "DELETE", Method::PATCH => "PATCH", } } pub fn as_bytes(&self) -> &[u8] { match self { Method::GET => b"GET", Method::HEAD => b"HEAD", Method::POST => b"POST", Method::PUT => b"PUT", Method::DELETE => b"DELETE", Method::PATCH => b"PATCH", } } } impl TryFrom<&str> for Method { type Error = InvalidMethod; fn try_from(value: &str) -> Result<Self, Self::Error> { match value { "GET" => Ok(Method::GET), "HEAD" => Ok(Method::HEAD), "POST" => Ok(Method::POST), "PUT" => Ok(Method::PUT), "DELETE" => Ok(Method::DELETE), "PATCH" => Ok(Method::PATCH), _ => Err(InvalidMethod), } } } impl TryFrom<&[u8]> for Method { type Error = InvalidMethod; fn try_from(value: &[u8]) -> Result<Self, Self::Error> { match value { b"GET" => Ok(Method::GET), b"HEAD" => Ok(Method::HEAD), b"POST" => Ok(Method::POST), b"PUT" => Ok(Method::PUT), b"DELETE" => Ok(Method::DELETE), b"PATCH" => Ok(Method::PATCH), _ => Err(InvalidMethod), } } } impl FromStr for Method { type Err = InvalidMethod; fn from_str(value: &str) -> Result<Self, Self::Err> { match value { "GET" => Ok(Method::GET), "HEAD" => Ok(Method::HEAD), "POST" => Ok(Method::POST), "PUT" => Ok(Method::PUT), "DELETE" => Ok(Method::DELETE), "PATCH" => Ok(Method::PATCH), _ => Err(InvalidMethod), } } } impl AsRef<str> for Method { fn as_ref(&self) -> &str { self.as_str() } } impl<'a> PartialEq<&'a Method> for Method { #[inline] fn eq(&self, other: &&'a Method) -> bool { self == *other } } impl<'a> PartialEq<Method> for &'a Method { #[inline] fn eq(&self, other: &Method) -> bool { *self == other } } impl PartialEq<str> for Method { #[inline] fn eq(&self, other: &str) -> bool { self.as_ref() == other } } impl PartialEq<Method> for str { #[inline] fn eq(&self, other: &Method) -> bool { self == other.as_ref() } } impl<'a> PartialEq<&'a str> for Method { #[inline] fn eq(&self, other: &&'a str) -> bool { self.as_ref() == *other } } impl<'a> PartialEq<Method> for &'a str { #[inline] fn eq(&self, other: &Method) -> bool { *self == other.as_ref() } } impl fmt::Debug for Method { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str(self.as_ref()) } } impl fmt::Display for Method { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.write_str(self.as_ref()) } } impl Default for Method { #[inline] fn default() -> Method { Method::GET } } impl<'a> From<&'a Method> for Method { #[inline] fn from(t: &'a Method) -> Self { *t } } #[derive(Debug)] pub struct InvalidMethod; impl fmt::Display for InvalidMethod { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description()) } } impl Error for InvalidMethod { fn description(&self) -> &str { "Invalid HTTP method" } }