Skip to main content

http_mel/
method.rs

1use melodium_core::{executive::*, *};
2use melodium_macro::{mel_data, mel_function};
3use trillium::Method;
4
5/// HTTP request method.
6///
7/// Implements `ToString` and `Display`, which return the standard uppercase method name
8/// (e.g. `"GET"`, `"POST"`).
9#[mel_data(traits(ToString PartialEquality Equality Display))]
10#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
11pub struct HttpMethod(pub Method);
12
13impl ToString for HttpMethod {
14    fn to_string(&self) -> String {
15        self.0.to_string()
16    }
17}
18
19impl Display for HttpMethod {
20    fn display(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
21        write!(f, "{}", melodium_core::executive::ToString::to_string(self))
22    }
23}
24
25/// Parse an HTTP method by name.
26///
27/// Returns `None` if `name` is not a recognised HTTP method string.
28#[mel_function]
29pub fn method(name: string) -> Option<HttpMethod> {
30    match Method::try_from(name.as_str()) {
31        Ok(method) => Some(HttpMethod(method)),
32        Err(_) => None,
33    }
34}
35
36/// Return the `DELETE` HTTP method.
37#[mel_function]
38pub fn delete() -> HttpMethod {
39    HttpMethod(Method::Delete)
40}
41
42/// Return the `GET` HTTP method.
43#[mel_function]
44pub fn get() -> HttpMethod {
45    HttpMethod(Method::Get)
46}
47
48/// Return the `HEAD` HTTP method.
49#[mel_function]
50pub fn head() -> HttpMethod {
51    HttpMethod(Method::Head)
52}
53
54/// Return the `OPTIONS` HTTP method.
55#[mel_function]
56pub fn options() -> HttpMethod {
57    HttpMethod(Method::Options)
58}
59
60/// Return the `PATCH` HTTP method.
61#[mel_function]
62pub fn patch() -> HttpMethod {
63    HttpMethod(Method::Patch)
64}
65
66/// Return the `POST` HTTP method.
67#[mel_function]
68pub fn post() -> HttpMethod {
69    HttpMethod(Method::Post)
70}
71
72/// Return the `PUT` HTTP method.
73#[mel_function]
74pub fn put() -> HttpMethod {
75    HttpMethod(Method::Put)
76}
77
78/// Return the `TRACE` HTTP method.
79#[mel_function]
80pub fn trace() -> HttpMethod {
81    HttpMethod(Method::Trace)
82}