Skip to main content

torus_http/
method.rs

1//! Contains safe wrappers for http methods, note that the conversion is case insensitive so custom methods might not behave as expected
2//!
3//! # example:
4//!
5//! ```rust
6//! use torus_http::method::HttpMethod;
7//! assert_eq!(HttpMethod::Get, HttpMethod::from_str_val("GET"));
8//! ```
9//!
10//! ## Note!!!
11//!
12//! due to how it is written right now, you cannot get a custom overwrite for the method (TODO: rephrase this I apparently don't know english)
13//!
14//! ```rust
15//! use torus_http::method::HttpMethod;
16//! assert!(HttpMethod::other("GET") != HttpMethod::from_str_val("GET"));
17//! ```
18
19impl HttpMethod {
20    /// Generate an http method from a string
21    #[must_use]
22    pub fn from_str_val(s: &str) -> Self {
23        match s.to_lowercase().as_str() {
24            "get" => HttpMethod::Get,
25            "post" => HttpMethod::Post,
26            "update" => HttpMethod::Update,
27            "delete" => HttpMethod::Delete,
28            "put" => HttpMethod::Put,
29            "patch" => HttpMethod::Patch,
30            "head" => HttpMethod::Head,
31            "options" => HttpMethod::Options,
32            other => HttpMethod::Other(other.to_string()),
33        }
34    }
35
36    /// Generate a non standard `HttpMethod`
37    #[must_use]
38    pub fn other(s: impl Into<String>) -> Self {
39        Self::Other(s.into())
40    }
41}
42
43/// Enum covering most standard http methods and also allowing for custom ones
44#[derive(Debug, Clone, Eq, PartialEq, Hash)]
45pub enum HttpMethod {
46    Get,
47    Post,
48    Delete,
49    Update,
50    Put,
51    Patch,
52    Head,
53    Options,
54    Other(String),
55}