use http::{HeaderMap, StatusCode};
use hyper_body_utils::HttpBody;
pub struct ResponseBuilder {
status: StatusCode,
version: http::Version,
headers: Option<HeaderMap>,
}
impl ResponseBuilder {
pub fn status(mut self, status: http::StatusCode) -> Self {
self.status = status;
self
}
pub fn version(mut self, version: http::Version) -> Self {
self.version = version;
self
}
pub fn header<K>(mut self, key: K, value: http::header::HeaderValue) -> Self
where
K: http::header::IntoHeaderName,
{
if self
.headers
.is_none()
{
self.headers = Some(HeaderMap::new());
}
self.headers
.as_mut()
.unwrap()
.append(key, value);
self
}
pub fn headers(mut self, headers: HeaderMap) -> Self {
self.headers = Some(headers);
self
}
pub fn empty(self) -> Response {
self.body(HttpBody::empty())
}
pub fn text(self, text: &str) -> Response {
self.body(HttpBody::from_text(text))
}
pub fn bytes(self, bytes: &[u8]) -> Response {
self.body(HttpBody::from_bytes(bytes))
}
pub fn body(self, body: HttpBody) -> Response {
let response = http::Response::new(body);
let (mut parts, body) = response.into_parts();
parts.status = self.status;
parts.version = self.version;
if let Some(headers) = self.headers {
parts.headers = headers;
}
let response = http::Response::from_parts(parts, body);
Response { inner: response }
}
}
pub struct Response {
pub(crate) inner: http::Response<HttpBody>,
}
impl Response {
pub fn builder() -> ResponseBuilder {
ResponseBuilder {
status: http::StatusCode::OK,
version: http::Version::HTTP_11,
headers: None,
}
}
pub fn into_inner(self) -> http::Response<HttpBody> {
self.inner
}
}