shared/api/
method.rs

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
#[cfg(feature = "backend")]
use actix_web::{web, Route};

/// Represents a http method.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Method {
    /// http `DELETE`, used for deleting resources.
    Delete,

    /// http `GET`, used to retrieving resources.
    Get,

    /// http `PATCH`, used to update resources.
    Patch,

    /// http `POST`, used to create resources.
    Post,

    /// http `PUT`, used to replace resources.
    Put,
}

#[cfg(feature = "backend")]
impl Method {
    /// Gets a [`Route`](Route) based off of `Self`.
    #[must_use]
    pub fn route(self) -> Route {
        match self {
            Self::Delete => web::delete(),
            Self::Get => web::get(),
            Self::Patch => web::patch(),
            Self::Post => web::post(),
            Self::Put => web::put(),
        }
    }
}

impl Method {
    /// Represents `Self` as a [`str`](str).
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Delete => "DELETE",
            Self::Get => "GET",
            Self::Patch => "PATCH",
            Self::Post => "POST",
            Self::Put => "PUT",
        }
    }
}