fire_http_representation/response/
mod.rs

1mod builder;
2pub use builder::ResponseBuilder;
3
4use crate::body::Body;
5use crate::header::{Mime, ResponseHeader, StatusCode};
6
7/// The response created from a server.
8#[derive(Debug)]
9pub struct Response {
10	pub header: ResponseHeader,
11	// if you overide the body
12	// you should pobably reset the content-length
13	pub body: Body,
14}
15
16impl Response {
17	/// Creates a new `Response`.
18	pub fn new(header: ResponseHeader, body: Body) -> Self {
19		Self { header, body }
20	}
21
22	/// Creates a new `Response` with a builder.
23	pub fn builder() -> ResponseBuilder {
24		ResponseBuilder::new()
25	}
26
27	/// Get the response header by reference.
28	pub fn header(&self) -> &ResponseHeader {
29		&self.header
30	}
31
32	/// Takes the body replacing it with an empty one.
33	///
34	/// ## Note
35	/// If you used the builder to create a `Response`
36	/// you should probably reset the `content-length` header.
37	pub fn take_body(&mut self) -> Body {
38		self.body.take()
39	}
40
41	pub fn text(body: impl Into<Body>) -> Self {
42		Self::builder().content_type(Mime::TEXT).body(body).build()
43	}
44
45	pub fn html(body: impl Into<Body>) -> Self {
46		Self::builder().content_type(Mime::HTML).body(body).build()
47	}
48}
49
50impl From<Body> for Response {
51	fn from(body: Body) -> Self {
52		Self::builder().body(body).build()
53	}
54}
55
56impl From<StatusCode> for Response {
57	fn from(status_code: StatusCode) -> Self {
58		Self::builder().status_code(status_code).build()
59	}
60}