oak_http_server/
enums.rs

1use std::fmt;
2
3/// A HTTP status to include in a [`Response`](crate::Response)
4#[derive(PartialEq, Debug)]
5#[non_exhaustive]
6pub enum Status {
7    /// `200 OK`
8    OK,
9    /// `201 Created`
10    Created,
11    /// `202 Accepted`
12    Accepted,
13    /// `203 No Content`
14    NoContent,
15
16    /// `400 Bad Request`
17    BadRequest,
18    /// `404 Not Found`
19    NotFound,
20
21    /// `500 Internal Server Error`
22    InternalError,
23    /// `501 Not Implemented`
24    NotImplemented,
25}
26
27impl Status {
28    /// Returns an [`Option`] containing [`Status`] by passing a [`usize`] corresponding to the HTTP status code to it
29    ///
30    /// If the status provided is a valid HTTP status, this function will evaluate to [`Some`] containing [`Self`]
31    ///
32    /// If the status provided isn't valid or implemented yet, this function will return [`None`]
33    ///
34    /// # Example
35    ///
36    /// ```
37    /// # use oak_http_server::Status;
38    ///
39    /// fn main() {
40    /// 	// Generate a new HTTP Status instance (in our case, Status::OK)
41    /// 	let status: Option<Status> = Status::new(200);
42    ///
43    /// 	assert_eq!(status, Some(Status::OK));
44    /// }
45    /// ```
46    pub fn new(status: usize) -> Option<Self> {
47        match status {
48            200 => Some(Self::OK),
49            201 => Some(Self::Created),
50            202 => Some(Self::Accepted),
51            204 => Some(Self::NoContent),
52
53            400 => Some(Self::BadRequest),
54            404 => Some(Self::NotFound),
55
56            500 => Some(Self::InternalError),
57            501 => Some(Self::NotImplemented),
58            _ => None,
59        }
60    }
61}
62
63impl fmt::Display for Status {
64    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65        write!(
66            f,
67            "{}",
68            match self {
69                Self::OK => 200,
70                Self::Created => 201,
71                Self::Accepted => 202,
72                Self::NoContent => 204,
73
74                Self::BadRequest => 400,
75                Self::NotFound => 404,
76
77                Self::InternalError => 500,
78                Self::NotImplemented => 501,
79            }
80        )
81    }
82}
83
84/// A HTTP method that is provided by the client
85#[derive(PartialEq, Clone, Debug)]
86#[non_exhaustive]
87pub enum Method {
88    /// The `GET` method requests a representation of the specified resource.
89    /// Requests using `GET` should only retrieve data.
90    GET,
91    /// The `HEAD` method asks for a response identical to a `GET` request, but without the response body.
92    HEAD,
93    /// The `POST` method submits an entity to the specified resource, often causing a change in state or side effects on the server.
94    POST,
95    /// The `PUT` method replaces all current representations of the target resource with the request payload.
96    PUT,
97    /// The `DELETE` method deletes the specified resource.
98    DELETE,
99}
100
101impl Method {
102    /// Returns an [`Option`] containing [`Method`] by passing a [`&str`] or [`String`] corresponding to a HTTP method
103    ///
104    /// If the method provided is a valid HTTP method, this function will evaluate to [`Some`] containing [`Self`]
105    ///
106    /// If the method provided isn't valid or implemented yet, this function will return [`None`]
107    ///
108    /// # Example
109    ///
110    /// ```
111    /// # use oak_http_server::Method;
112    ///
113    /// fn main() {
114    /// 	// Create a new HTTP Method instance (in our case, Method::GET)
115    /// 	let method: Option<Method> = Method::new("GET");
116    ///
117    /// 	assert_eq!(method, Some(Method::GET));
118    /// }
119    /// ```
120    pub fn new<S>(method: S) -> Option<Self>
121    where
122        S: Into<String>,
123    {
124        match method.into().as_str() {
125            "GET" => Some(Self::GET),
126            "HEAD" => Some(Self::HEAD),
127            "POST" => Some(Self::POST),
128            "PUT" => Some(Self::PUT),
129            "DELETE" => Some(Self::DELETE),
130            _ => None,
131        }
132    }
133}
134
135impl fmt::Display for Method {
136    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
137        write!(
138            f,
139            "{}",
140            match self {
141                Self::GET => "GET",
142                Self::HEAD => "HEAD",
143                Self::POST => "POST",
144                Self::PUT => "PUT",
145                Self::DELETE => "DELETE",
146            }
147        )
148    }
149}