1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
mod builder;
pub use builder::ResponseBuilder;
use crate::header::{ResponseHeader, StatusCode, Mime};
use crate::body::Body;
#[derive(Debug)]
pub struct Response {
pub header: ResponseHeader,
pub body: Body
}
impl Response {
pub fn new(header: ResponseHeader, body: Body) -> Self {
Self { header, body }
}
pub fn builder() -> ResponseBuilder {
ResponseBuilder::new()
}
pub fn header(&self) -> &ResponseHeader {
&self.header
}
pub fn take_body(&mut self) -> Body {
self.body.take()
}
pub fn text(body: impl Into<Body>) -> Self {
Self::builder()
.content_type(Mime::TEXT)
.body(body)
.build()
}
pub fn html(body: impl Into<Body>) -> Self {
Self::builder()
.content_type(Mime::HTML)
.body(body)
.build()
}
}
impl From<Body> for Response {
fn from(body: Body) -> Self {
Self::builder()
.body(body)
.build()
}
}
impl From<StatusCode> for Response {
fn from(status_code: StatusCode) -> Self {
Self::builder()
.status_code(status_code)
.build()
}
}