Skip to main content

g_server/http/
response.rs

1use super::{body::Body, header::Header, status::Status};
2
3/// An HTTP response.
4#[derive(Debug)]
5pub struct Response {
6    status: Status,
7    headers: Header,
8    body: Body,
9}
10
11impl Response {
12    /// Creates a new HTTP response.
13    pub fn new(status: impl Into<Status>, headers: Header, body: impl Into<Body>) -> Self {
14        Self {
15            status: status.into(),
16            headers,
17            body: body.into(),
18        }
19    }
20
21    /// Returns the response status.
22    pub fn status(&self) -> &Status {
23        &self.status
24    }
25
26    /// Returns the response headers.
27    pub fn headers(&self) -> &Header {
28        &self.headers
29    }
30
31    /// Returns the response body.
32    pub fn body(&self) -> &Body {
33        &self.body
34    }
35
36    /// Returns a mutable reference to the response status.
37    pub fn status_mut(&mut self) -> &mut Status {
38        &mut self.status
39    }
40
41    /// Returns a mutable reference to the response headers.
42    pub fn headers_mut(&mut self) -> &mut Header {
43        &mut self.headers
44    }
45
46    /// Returns a mutable reference to the response body.
47    pub fn body_mut(&mut self) -> &mut Body {
48        &mut self.body
49    }
50
51    /// Consumes the response and returns its body.
52    pub fn into_body(self) -> Body {
53        self.body
54    }
55
56    /// Consumes the response and returns its components.
57    pub fn into_parts(self) -> (Status, Header, Body) {
58        (self.status, self.headers, self.body)
59    }
60}