oxedyne/fe2o3/fe2o3_text/tests/detect_corpus/rust/enums_match.txt
1.7 KiB, 1 run
created by r1870400018:12046, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | /// HTTP request method. |
| 2 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 3 | pub enum Method { |
| 4 | Get, |
| 5 | Post, |
| 6 | Put, |
| 7 | Delete, |
| 8 | Patch, |
| 9 | Head, |
| 10 | Options, |
| 11 | } |
| 12 | |
| 13 | impl Method { |
| 14 | /// Parse a method from a string slice. |
| 15 | pub fn from_str(s: &str) -> Option<Self> { |
| 16 | match s { |
| 17 | "GET" => Some(Self::Get), |
| 18 | "POST" => Some(Self::Post), |
| 19 | "PUT" => Some(Self::Put), |
| 20 | "DELETE" => Some(Self::Delete), |
| 21 | "PATCH" => Some(Self::Patch), |
| 22 | "HEAD" => Some(Self::Head), |
| 23 | "OPTIONS" => Some(Self::Options), |
| 24 | _ => None, |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /// Whether this method conventionally carries a request body. |
| 29 | pub fn has_body(&self) -> bool { |
| 30 | match self { |
| 31 | Self::Post | Self::Put | Self::Patch => true, |
| 32 | _ => false, |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /// The method as an uppercase string. |
| 37 | pub fn as_str(&self) -> &'static str { |
| 38 | match self { |
| 39 | Self::Get => "GET", |
| 40 | Self::Post => "POST", |
| 41 | Self::Put => "PUT", |
| 42 | Self::Delete => "DELETE", |
| 43 | Self::Patch => "PATCH", |
| 44 | Self::Head => "HEAD", |
| 45 | Self::Options => "OPTIONS", |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// HTTP status code category. |
| 51 | pub enum StatusCategory { |
| 52 | Informational, |
| 53 | Success, |
| 54 | Redirection, |
| 55 | ClientError, |
| 56 | ServerError, |
| 57 | Unknown, |
| 58 | } |
| 59 | |
| 60 | pub fn categorise(code: u16) -> StatusCategory { |
| 61 | match code { |
| 62 | 100..=199 => StatusCategory::Informational, |
| 63 | 200..=299 => StatusCategory::Success, |
| 64 | 300..=399 => StatusCategory::Redirection, |
| 65 | 400..=499 => StatusCategory::ClientError, |
| 66 | 500..=599 => StatusCategory::ServerError, |
| 67 | _ => StatusCategory::Unknown, |
| 68 | } |
| 69 | } |