elif_http/request/
method.rs

1//! HTTP method utilities and wrappers
2
3use std::fmt;
4use std::str::FromStr;
5use crate::errors::ParseError;
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!(self.0, axum::http::Method::GET | axum::http::Method::HEAD | 
38                          axum::http::Method::OPTIONS | axum::http::Method::TRACE)
39    }
40
41    /// Check if method is idempotent (GET, HEAD, PUT, DELETE, OPTIONS, TRACE)
42    pub fn is_idempotent(&self) -> bool {
43        matches!(self.0, axum::http::Method::GET | axum::http::Method::HEAD | 
44                          axum::http::Method::PUT | axum::http::Method::DELETE |
45                          axum::http::Method::OPTIONS | axum::http::Method::TRACE)
46    }
47
48    /// Internal method to convert to axum Method (for framework internals only)
49    pub(crate) fn to_axum(&self) -> &axum::http::Method {
50        &self.0
51    }
52
53    /// Internal method to create from axum Method (for framework internals only)
54    pub(crate) fn from_axum(method: axum::http::Method) -> Self {
55        Self(method)
56    }
57}
58
59impl FromStr for ElifMethod {
60    type Err = ParseError;
61
62    fn from_str(s: &str) -> Result<Self, Self::Err> {
63        axum::http::Method::from_str(s)
64            .map(Self)
65            .map_err(ParseError::from)
66    }
67}
68
69impl fmt::Display for ElifMethod {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        write!(f, "{}", self.0)
72    }
73}