elif_http/request/
method.rs

1//! HTTP method utilities and wrappers
2
3use crate::errors::ParseError;
4use std::fmt;
5use std::str::FromStr;
6
7/// Framework-native HTTP method wrapper that hides Axum internals
8#[derive(Debug, Clone, PartialEq, Eq, Hash)]
9pub struct ElifMethod(axum::http::Method);
10
11impl ElifMethod {
12    /// Common HTTP methods as constants
13    pub const GET: Self = Self(axum::http::Method::GET);
14    pub const POST: Self = Self(axum::http::Method::POST);
15    pub const PUT: Self = Self(axum::http::Method::PUT);
16    pub const DELETE: Self = Self(axum::http::Method::DELETE);
17    pub const PATCH: Self = Self(axum::http::Method::PATCH);
18    pub const HEAD: Self = Self(axum::http::Method::HEAD);
19    pub const OPTIONS: Self = Self(axum::http::Method::OPTIONS);
20    pub const TRACE: Self = Self(axum::http::Method::TRACE);
21    pub const CONNECT: Self = Self(axum::http::Method::CONNECT);
22
23    /// Create method from string
24    pub fn from_str(method: &str) -> Result<Self, ParseError> {
25        axum::http::Method::from_str(method)
26            .map(Self)
27            .map_err(ParseError::from)
28    }
29
30    /// Get method as string
31    pub fn as_str(&self) -> &str {
32        self.0.as_str()
33    }
34
35    /// Check if method is safe (GET, HEAD, OPTIONS, TRACE)
36    pub fn is_safe(&self) -> bool {
37        matches!(
38            self.0,
39            axum::http::Method::GET
40                | axum::http::Method::HEAD
41                | axum::http::Method::OPTIONS
42                | axum::http::Method::TRACE
43        )
44    }
45
46    /// Check if method is idempotent (GET, HEAD, PUT, DELETE, OPTIONS, TRACE)
47    pub fn is_idempotent(&self) -> bool {
48        matches!(
49            self.0,
50            axum::http::Method::GET
51                | axum::http::Method::HEAD
52                | axum::http::Method::PUT
53                | axum::http::Method::DELETE
54                | axum::http::Method::OPTIONS
55                | axum::http::Method::TRACE
56        )
57    }
58
59    /// Internal method to convert to axum Method (for framework internals only)
60    pub(crate) fn to_axum(&self) -> &axum::http::Method {
61        &self.0
62    }
63
64    /// Internal method to create from axum Method (for framework internals only)
65    pub(crate) fn from_axum(method: axum::http::Method) -> Self {
66        Self(method)
67    }
68}
69
70impl FromStr for ElifMethod {
71    type Err = ParseError;
72
73    fn from_str(s: &str) -> Result<Self, Self::Err> {
74        axum::http::Method::from_str(s)
75            .map(Self)
76            .map_err(ParseError::from)
77    }
78}
79
80impl fmt::Display for ElifMethod {
81    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82        write!(f, "{}", self.0)
83    }
84}