roa_core/
response.rs

1//! A module for Response and its body
2use std::ops::{Deref, DerefMut};
3
4use http::{HeaderMap, HeaderValue, StatusCode, Version};
5
6pub use crate::Body;
7
8/// Http response type of roa.
9pub struct Response {
10    /// Status code.
11    pub status: StatusCode,
12
13    /// Version of HTTP protocol.
14    pub version: Version,
15
16    /// Raw header map.
17    pub headers: HeaderMap<HeaderValue>,
18
19    /// Response body.
20    pub body: Body,
21}
22
23impl Response {
24    #[inline]
25    pub(crate) fn new() -> Self {
26        Self {
27            status: StatusCode::default(),
28            version: Version::default(),
29            headers: HeaderMap::default(),
30            body: Body::default(),
31        }
32    }
33
34    #[inline]
35    fn into_resp(self) -> http::Response<hyper::Body> {
36        let (mut parts, _) = http::Response::new(()).into_parts();
37        let Response {
38            status,
39            version,
40            headers,
41            body,
42        } = self;
43        parts.status = status;
44        parts.version = version;
45        parts.headers = headers;
46        http::Response::from_parts(parts, body.into())
47    }
48}
49
50impl Deref for Response {
51    type Target = Body;
52    #[inline]
53    fn deref(&self) -> &Self::Target {
54        &self.body
55    }
56}
57
58impl DerefMut for Response {
59    #[inline]
60    fn deref_mut(&mut self) -> &mut Self::Target {
61        &mut self.body
62    }
63}
64
65impl From<Response> for http::Response<hyper::Body> {
66    #[inline]
67    fn from(value: Response) -> Self {
68        value.into_resp()
69    }
70}
71
72impl Default for Response {
73    #[inline]
74    fn default() -> Self {
75        Self::new()
76    }
77}