allframe_core/router/
method.rs

1//! HTTP method types for type-safe routing
2//!
3//! This module provides a type-safe representation of HTTP methods
4//! to replace stringly-typed method handling.
5
6use std::fmt;
7
8/// HTTP method for REST routes
9///
10/// Represents standard HTTP methods with compile-time type safety.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum Method {
13    /// GET method
14    GET,
15    /// POST method
16    POST,
17    /// PUT method
18    PUT,
19    /// DELETE method
20    DELETE,
21    /// PATCH method
22    PATCH,
23    /// HEAD method
24    HEAD,
25    /// OPTIONS method
26    OPTIONS,
27}
28
29impl Method {
30    /// Convert Method to string representation
31    pub fn as_str(&self) -> &'static str {
32        match self {
33            Method::GET => "GET",
34            Method::POST => "POST",
35            Method::PUT => "PUT",
36            Method::DELETE => "DELETE",
37            Method::PATCH => "PATCH",
38            Method::HEAD => "HEAD",
39            Method::OPTIONS => "OPTIONS",
40        }
41    }
42}
43
44impl fmt::Display for Method {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        write!(f, "{}", self.as_str())
47    }
48}
49
50impl From<Method> for String {
51    fn from(method: Method) -> String {
52        method.as_str().to_string()
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_method_as_str() {
62        assert_eq!(Method::GET.as_str(), "GET");
63        assert_eq!(Method::POST.as_str(), "POST");
64        assert_eq!(Method::PUT.as_str(), "PUT");
65        assert_eq!(Method::DELETE.as_str(), "DELETE");
66        assert_eq!(Method::PATCH.as_str(), "PATCH");
67        assert_eq!(Method::HEAD.as_str(), "HEAD");
68        assert_eq!(Method::OPTIONS.as_str(), "OPTIONS");
69    }
70
71    #[test]
72    fn test_method_display() {
73        assert_eq!(format!("{}", Method::GET), "GET");
74        assert_eq!(format!("{}", Method::POST), "POST");
75    }
76
77    #[test]
78    fn test_method_into_string() {
79        let method: String = Method::GET.into();
80        assert_eq!(method, "GET");
81    }
82
83    #[test]
84    fn test_method_equality() {
85        assert_eq!(Method::GET, Method::GET);
86        assert_ne!(Method::GET, Method::POST);
87    }
88
89    #[test]
90    fn test_method_clone() {
91        let method1 = Method::POST;
92        let method2 = method1;
93        assert_eq!(method1, method2);
94    }
95}