Skip to main content

rama_http_hyperium/into_hyperium/
message.rs

1//! Message conversions (request, response, and their parts).
2
3use rama_http_types::{Request, Response, request, response};
4
5use super::TryIntoHyperiumHttp;
6use crate::HyperExtensions;
7
8impl<T> TryIntoHyperiumHttp for Request<T> {
9    type Output = http::Request<T>;
10    type Error = http::Error;
11
12    fn try_into_hyperium_http(self) -> Result<http::Request<T>, http::Error> {
13        let (parts, body) = self.into_parts();
14
15        let mut hyper_extensions = parts
16            .extensions
17            .get_ref::<HyperExtensions>()
18            .map(|ext| ext.0.clone())
19            .unwrap_or_default();
20        hyper_extensions.insert(parts.extensions);
21
22        let mut req = http::Request::new(body);
23        *req.method_mut() = parts.method.try_into_hyperium_http()?;
24        *req.uri_mut() = parts.uri.try_into_hyperium_http()?;
25        *req.version_mut() = parts.version.try_into_hyperium_http()?;
26        *req.headers_mut() = parts.headers.try_into_hyperium_http()?;
27        *req.extensions_mut() = hyper_extensions;
28        Ok(req)
29    }
30}
31
32impl<T> TryIntoHyperiumHttp for Response<T> {
33    type Output = http::Response<T>;
34    type Error = http::Error;
35
36    fn try_into_hyperium_http(self) -> Result<http::Response<T>, http::Error> {
37        let (parts, body) = self.into_parts();
38
39        let mut hyper_extensions = parts
40            .extensions
41            .get_ref::<HyperExtensions>()
42            .map(|ext| ext.0.clone())
43            .unwrap_or_default();
44        hyper_extensions.insert(parts.extensions);
45
46        let mut res = http::Response::new(body);
47        *res.status_mut() = parts.status.try_into_hyperium_http()?;
48        *res.version_mut() = parts.version.try_into_hyperium_http()?;
49        *res.headers_mut() = parts.headers.try_into_hyperium_http()?;
50        *res.extensions_mut() = hyper_extensions;
51        Ok(res)
52    }
53}
54
55impl TryIntoHyperiumHttp for request::Parts {
56    type Output = http::request::Parts;
57    type Error = http::Error;
58
59    fn try_into_hyperium_http(self) -> Result<http::request::Parts, http::Error> {
60        // `http::request::Parts` can't be built directly, so route through a
61        // `Request<()>` and split it back out.
62        Ok(Request::from_parts(self, ())
63            .try_into_hyperium_http()?
64            .into_parts()
65            .0)
66    }
67}
68
69impl TryIntoHyperiumHttp for response::Parts {
70    type Output = http::response::Parts;
71    type Error = http::Error;
72    fn try_into_hyperium_http(self) -> Result<http::response::Parts, http::Error> {
73        Ok(Response::from_parts(self, ())
74            .try_into_hyperium_http()?
75            .into_parts()
76            .0)
77    }
78}