use std::mem; use http::{response::Builder, HeaderMap, StatusCode, Version}; use serde::{de, Deserialize, Serialize}; type Type = http::Response; const STRUCT_NAME: &str = "Response"; #[derive(Serialize)] struct BorrowedHead<'a> { #[serde(with = "crate::status_code")] status: StatusCode, #[serde(with = "crate::header_map")] headers: &'a HeaderMap, #[serde(with = "crate::version")] version: Version, } impl<'a, T> From<&'a Type> for BorrowedHead<'a> { fn from(val: &'a Type) -> Self { Self { status: val.status(), headers: val.headers(), version: val.version(), } } } #[derive(Deserialize)] struct Head { #[serde(with = "crate::status_code")] status: StatusCode, #[serde(with = "crate::header_map")] headers: HeaderMap, #[serde(with = "crate::version")] version: Version, } impl Head { fn try_into_with_body(mut self, body: T) -> Result, E> where E: de::Error, { let mut builder = Builder::new().status(self.status).version(self.version); if let Some(headers) = builder.headers_mut() { mem::swap(&mut self.headers, headers); } else { return Err(de::Error::custom("builder doesn't have headers")); } builder.body(body).map_err(de::Error::custom) } } serde_request_response!(Type, STRUCT_NAME, Head, BorrowedHead); derive_extension_types!(super::Type, T);