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
//! NewType of [Response](http::Response)
use std::ops::{Deref, DerefMut};
use crate::result::InternalResult;
pub use http::response::Builder as HttpResponseBuilder;
use http::response::Parts;
pub use http::Response as HttpResponse;
/// NewType of [Response](http::Response)
#[derive(Debug)]
pub struct Response<T>(HttpResponse<T>);
impl<T> Deref for Response<T> {
type Target = http::Response<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for Response<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> From<http::Response<T>> for Response<T> {
fn from(value: http::Response<T>) -> Self {
Response(value)
}
}
impl<T> From<Response<T>> for http::Response<T> {
fn from(value: Response<T>) -> Self {
value.0
}
}
impl Response<()> {
/// Create a new instance of [Builder](http::response::Builder)
pub fn builder() -> HttpResponseBuilder {
HttpResponseBuilder::new()
}
}
impl<T> Response<T> {
/// Create a new instance of [Response]
pub fn new(value: T) -> Self {
Self(HttpResponse::new(value))
}
/// Creates a new `Response` with the given head and body
///
/// Just a dummy to [from_parts](http::Response::from_parts) function
#[inline]
pub fn from_parts(parts: Parts, body: T) -> Response<T> {
Response(HttpResponse::from_parts(parts, body))
}
/// Transform the `Body` of a [Response] using a callback that
/// returns a result
pub fn and_then<BodyType>(
self,
callback: impl FnOnce(T) -> InternalResult<BodyType>,
) -> InternalResult<Response<BodyType>> {
let (parts, body) = self.0.into_parts();
callback(body).map(|body| Response(HttpResponse::from_parts(parts, body)))
}
/// Consume [NewType](crate::response::Response) and Return the original
/// [Response](http::Response)
pub fn into_inner(self) -> HttpResponse<T> {
self.0
}
/// Consumes the response, returning just the body.
///
/// Just a dummy to [into_body](http::Response::into_body) function
#[inline]
pub fn into_body(self) -> T {
self.0.into_body()
}
/// Consumes the response returning the head and body parts.
///
/// Just a dummy to [into_parts](http::Response::into_parts) function
#[inline]
pub fn into_parts(self) -> (Parts, T) {
self.0.into_parts()
}
/// Consumes the response returning a new response with body mapped to the
/// return type of the passed in function.
///
/// Just a dummy to [map](http::Response::map) function
#[inline]
pub fn map<F, U>(self, f: F) -> Response<U>
where
F: FnOnce(T) -> U,
{
Response(self.0.map(f))
}
}
impl From<Response<String>> for InternalResult<Response<String>> {
fn from(val: Response<String>) -> Self {
Ok(val)
}
}