pubky_homeserver/data_directory/quota_config/
http_method.rs1use std::{fmt::Display, str::FromStr};
2
3use axum::http::Method;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
8pub struct HttpMethod(pub Method);
9
10impl From<Method> for HttpMethod {
11 fn from(method: Method) -> Self {
12 HttpMethod(method)
13 }
14}
15
16impl From<HttpMethod> for Method {
17 fn from(method: HttpMethod) -> Self {
18 method.0
19 }
20}
21
22impl FromStr for HttpMethod {
23 type Err = String;
24
25 fn from_str(s: &str) -> Result<Self, Self::Err> {
26 Method::from_str(s.to_uppercase().as_str())
27 .map(HttpMethod)
28 .map_err(|_| format!("Invalid method: {}", s))
29 }
30}
31
32impl Display for HttpMethod {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 write!(f, "{}", self.0)
35 }
36}
37
38impl Serialize for HttpMethod {
39 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
40 where
41 S: serde::Serializer,
42 {
43 serializer.serialize_str(&self.to_string())
44 }
45}
46
47impl<'de> Deserialize<'de> for HttpMethod {
48 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
49 where
50 D: serde::Deserializer<'de>,
51 {
52 let s = String::deserialize(deserializer)?;
53 HttpMethod::from_str(&s).map_err(serde::de::Error::custom)
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_http_method_serde() {
63 let method = Method::GET;
64 let http_method = HttpMethod(method);
65 assert_eq!(http_method.to_string(), "GET");
66
67 let deserialized: HttpMethod = "GET".parse().unwrap();
68 assert_eq!(deserialized, http_method);
69 }
70}