Skip to main content

openapi_nexus_core/
serde.rs

1//! Custom serde serialization/deserialization helpers
2
3use std::str::FromStr;
4
5use serde::{Deserialize, Serializer};
6
7/// Module for serializing/deserializing `http::Method` as a string
8pub mod http_method {
9    use super::*;
10
11    /// Serialize `http::Method` as a string
12    pub fn serialize<S>(method: &http::Method, serializer: S) -> Result<S::Ok, S::Error>
13    where
14        S: Serializer,
15    {
16        serializer.serialize_str(method.as_str())
17    }
18
19    /// Deserialize `http::Method` from a string
20    pub fn deserialize<'de, D>(deserializer: D) -> Result<http::Method, D::Error>
21    where
22        D: serde::Deserializer<'de>,
23    {
24        let s = String::deserialize(deserializer)?;
25        http::Method::from_str(&s).map_err(serde::de::Error::custom)
26    }
27}