argan_core/response/
mod.rs

1//! Response types and conversion traits into them.
2
3// ----------
4
5use http::{HeaderName, HeaderValue};
6
7use crate::body::Body;
8
9// ----------
10
11pub use http::response::Builder;
12
13// --------------------------------------------------
14
15mod error;
16pub use error::{ErrorResponse, ResponseError};
17
18mod impls;
19
20// --------------------------------------------------------------------------------
21// --------------------------------------------------------------------------------
22
23pub type Response<B = Body> = http::response::Response<B>;
24pub type ResponseHeadParts = http::response::Parts;
25
26pub type BoxedErrorResponse = Box<dyn ErrorResponse + Send + Sync>;
27pub type ResponseResult = Result<Response, BoxedErrorResponse>;
28
29// --------------------------------------------------------------------------------
30
31// --------------------------------------------------
32// IntoResponseHeadParts trait
33
34/// Implemented by types that form or can be converted into a type that forms the
35/// [`ResponseHeadParts`].
36pub trait IntoResponseHeadParts {
37	fn into_response_head(
38		self,
39		head: ResponseHeadParts,
40	) -> Result<ResponseHeadParts, BoxedErrorResponse>;
41}
42
43// --------------------------------------------------
44// IntoResponse trait
45
46/// Implemented by types that can be converted into the [`Response`] type.
47pub trait IntoResponse {
48	fn into_response(self) -> Response;
49}
50
51impl IntoResponse for Response {
52	fn into_response(self) -> Response {
53		self
54	}
55}
56
57// --------------------------------------------------
58// IntoResponseResult trait
59
60/// Implemented by types that can be converted into the [`ResponseResult`] type.
61pub trait IntoResponseResult {
62	fn into_response_result(self) -> Result<Response, BoxedErrorResponse>;
63}
64
65impl<R, E> IntoResponseResult for Result<R, E>
66where
67	R: IntoResponseResult,
68	E: Into<BoxedErrorResponse>,
69{
70	fn into_response_result(self) -> Result<Response, BoxedErrorResponse> {
71		self
72			.map_err(Into::into)
73			.and_then(IntoResponseResult::into_response_result)
74	}
75}
76
77impl<R> IntoResponseResult for R
78where
79	R: IntoResponse,
80{
81	fn into_response_result(self) -> Result<Response, BoxedErrorResponse> {
82		Ok(self.into_response())
83	}
84}
85
86// --------------------------------------------------------------------------------