http/
method.rs

1use std::{fmt::Display, str::FromStr};
2
3use crate::{HttpError, Result, err};
4
5/// Request Method
6///
7/// Represents the method of the HTTP request
8#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
9pub enum HttpMethod {
10    GET,
11    POST,
12    PUT,
13    DELETE,
14    HEAD,
15    PATCH,
16    CONNECT,
17    OPTIONS,
18    TRACE,
19}
20
21impl FromStr for HttpMethod {
22    type Err = HttpError;
23    fn from_str(t: &str) -> Result<Self> {
24        match t {
25            "GET" => Ok(Self::GET),
26            "POST" => Ok(Self::POST),
27            "PUT" => Ok(Self::PUT),
28            "DELETE" => Ok(Self::DELETE),
29            "HEAD" => Ok(Self::HEAD),
30            "PATCH" => Ok(Self::PATCH),
31            "CONNECT" => Ok(Self::CONNECT),
32            "OPTIONS" => Ok(Self::OPTIONS),
33            "TRACE" => Ok(Self::TRACE),
34            _ => err!("Couldn't parse request method \"{t}\""),
35        }
36    }
37}
38
39impl Display for HttpMethod {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        write!(f, "{self:?}")
42    }
43}