-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathhttp_method.rs
106 lines (90 loc) · 3.08 KB
/
http_method.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use http::Method;
use serde::{
de::{Deserialize, Deserializer, Error as DeError, Unexpected, Visitor},
ser::Serializer,
};
use std::fmt;
pub fn serialize<S: Serializer>(method: &Method, ser: S) -> Result<S::Ok, S::Error> {
ser.serialize_str(method.as_str())
}
struct MethodVisitor;
impl<'de> Visitor<'de> for MethodVisitor {
type Value = Method;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "valid method name")
}
fn visit_str<E: DeError>(self, val: &str) -> Result<Self::Value, E> {
if val.is_empty() {
Ok(Method::GET)
} else {
val.parse()
.map_err(|_| DeError::invalid_value(Unexpected::Str(val), &self))
}
}
}
pub fn deserialize<'de, D>(de: D) -> Result<Method, D::Error>
where
D: Deserializer<'de>,
{
de.deserialize_str(MethodVisitor)
}
pub fn deserialize_optional<'de, D>(deserializer: D) -> Result<Option<Method>, D::Error>
where
D: Deserializer<'de>,
{
let s: Option<String> = Option::deserialize(deserializer)?;
if let Some(val) = s {
let visitor = MethodVisitor {};
return visitor.visit_str(&val).map(Some);
}
Ok(None)
}
pub fn serialize_optional<S: Serializer>(method: &Option<Method>, ser: S) -> Result<S::Ok, S::Error> {
if let Some(method) = method {
return serialize(method, ser);
}
ser.serialize_none()
}
#[cfg(test)]
mod tests {
use super::*;
use serde::{Deserialize, Serialize};
#[test]
fn test_http_method_serializer() {
#[derive(Deserialize, Serialize)]
struct Test {
#[serde(with = "crate::custom_serde::http_method")]
pub method: http::Method,
}
let data = aws_lambda_json_impl::json!({
"method": "DELETE"
});
let decoded: Test = aws_lambda_json_impl::from_value(data.clone()).unwrap();
assert_eq!(http::Method::DELETE, decoded.method);
let recoded = aws_lambda_json_impl::to_value(decoded).unwrap();
assert_eq!(data, recoded);
}
#[test]
fn test_http_optional_method_serializer() {
#[derive(Deserialize, Serialize)]
struct Test {
#[serde(deserialize_with = "deserialize_optional")]
#[serde(serialize_with = "serialize_optional")]
#[serde(default)]
pub method: Option<http::Method>,
}
let data = aws_lambda_json_impl::json!({
"method": "DELETE"
});
let decoded: Test = aws_lambda_json_impl::from_value(data.clone()).unwrap();
assert_eq!(Some(http::Method::DELETE), decoded.method);
let recoded = aws_lambda_json_impl::to_value(decoded).unwrap();
assert_eq!(data, recoded);
let data = aws_lambda_json_impl::json!({ "method": null });
let decoded: Test = aws_lambda_json_impl::from_value(data).unwrap();
assert_eq!(None, decoded.method);
let data = aws_lambda_json_impl::json!({});
let decoded: Test = aws_lambda_json_impl::from_value(data).unwrap();
assert_eq!(None, decoded.method);
}
}