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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use std::{any::Any, convert::TryFrom};
use crate::{
error::ErrorBodyHasBeenTaken,
http::{
header::{self, HeaderMap, HeaderName, HeaderValue},
Extensions, StatusCode, Version,
},
Body, Error, Result,
};
struct Parts {
status: StatusCode,
version: Version,
headers: HeaderMap,
extensions: Extensions,
}
pub struct Response {
status: StatusCode,
version: Version,
headers: HeaderMap,
extensions: Extensions,
body: Option<Body>,
}
impl Response {
pub(crate) fn into_hyper_response(self) -> hyper::Response<hyper::Body> {
let mut resp = hyper::Response::new(
self.body
.map(|body| body.0)
.unwrap_or_else(hyper::Body::empty),
);
*resp.status_mut() = self.status;
*resp.version_mut() = self.version;
*resp.headers_mut() = self.headers;
*resp.extensions_mut() = self.extensions;
resp
}
pub fn builder() -> ResponseBuilder {
ResponseBuilder(Ok(Parts {
status: StatusCode::OK,
version: Default::default(),
headers: Default::default(),
extensions: Default::default(),
}))
}
#[inline]
pub fn status(&self) -> StatusCode {
self.status
}
#[inline]
pub fn set_status(&mut self, status: StatusCode) {
self.status = status;
}
#[inline]
pub fn headers(&self) -> &HeaderMap {
&self.headers
}
#[inline]
pub fn headers_mut(&mut self) -> &mut HeaderMap {
&mut self.headers
}
#[inline]
pub fn version(&self) -> Version {
self.version
}
#[inline]
pub fn set_version(&mut self, version: Version) {
self.version = version;
}
#[inline]
pub fn extensions(&self) -> &Extensions {
&self.extensions
}
#[inline]
pub fn extensions_mut(&mut self) -> &mut Extensions {
&mut self.extensions
}
pub fn set_body(&mut self, body: Body) {
self.body = Some(body);
}
#[inline]
pub fn take_body(&mut self) -> Result<Body> {
self.body.take().ok_or_else(|| ErrorBodyHasBeenTaken.into())
}
}
pub struct ResponseBuilder(Result<Parts>);
impl ResponseBuilder {
#[must_use]
pub fn status(self, status: StatusCode) -> Self {
Self(self.0.map(|parts| Parts { status, ..parts }))
}
#[must_use]
pub fn version(self, version: Version) -> Self {
Self(self.0.map(|parts| Parts { version, ..parts }))
}
#[must_use]
pub fn header<K, V>(self, key: K, value: V) -> Self
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<Error>,
{
Self(self.0.and_then(move |mut parts| {
let key = <HeaderName as TryFrom<K>>::try_from(key).map_err(Into::into)?;
let value = <HeaderValue as TryFrom<V>>::try_from(value).map_err(Into::into)?;
parts.headers.append(key, value);
Ok(parts)
}))
}
#[must_use]
pub fn content_type(self, content_type: &str) -> Self {
Self(self.0.and_then(move |mut parts| {
let value = content_type.parse()?;
parts.headers.append(header::CONTENT_TYPE, value);
Ok(parts)
}))
}
#[must_use]
pub fn extension<T>(self, extension: T) -> Self
where
T: Any + Send + Sync + 'static,
{
Self(self.0.map(move |mut parts| {
parts.extensions.insert(extension);
parts
}))
}
pub fn body(self, body: Body) -> Result<Response> {
self.0.map(move |parts| Response {
status: parts.status,
version: parts.version,
headers: parts.headers,
extensions: parts.extensions,
body: Some(body),
})
}
}