shared/api/
method.rs

1#[cfg(feature = "backend")]
2use actix_web::{web, Route};
3
4/// Represents a http method.
5#[derive(Copy, Clone, Eq, PartialEq, Debug)]
6pub enum Method {
7    /// http `DELETE`, used for deleting resources.
8    Delete,
9
10    /// http `GET`, used to retrieving resources.
11    Get,
12
13    /// http `PATCH`, used to update resources.
14    Patch,
15
16    /// http `POST`, used to create resources.
17    Post,
18
19    /// http `PUT`, used to replace resources.
20    Put,
21}
22
23#[cfg(feature = "backend")]
24impl Method {
25    /// Gets a [`Route`](Route) based off of `Self`.
26    #[must_use]
27    pub fn route(self) -> Route {
28        match self {
29            Self::Delete => web::delete(),
30            Self::Get => web::get(),
31            Self::Patch => web::patch(),
32            Self::Post => web::post(),
33            Self::Put => web::put(),
34        }
35    }
36}
37
38impl Method {
39    /// Represents `Self` as a [`str`](str).
40    #[must_use]
41    pub const fn as_str(self) -> &'static str {
42        match self {
43            Self::Delete => "DELETE",
44            Self::Get => "GET",
45            Self::Patch => "PATCH",
46            Self::Post => "POST",
47            Self::Put => "PUT",
48        }
49    }
50}