Skip to main content

rama_http/service/web/endpoint/response/
mod.rs

1//! Types and traits for generating responses.
2//!
3//! See [`crate::response`] for more details.
4
5use crate::Response;
6
7mod append_headers;
8mod headers;
9mod into_response;
10mod into_response_parts;
11
12#[doc(inline)]
13pub use self::{
14    append_headers::AppendHeaders,
15    headers::Headers,
16    into_response::{IntoResponse, StaticResponseFactory},
17    into_response_parts::{IntoResponseParts, ResponseParts, TryIntoHeaderError},
18};
19
20mod html;
21#[doc(inline)]
22pub use html::Html;
23
24mod script;
25#[doc(inline)]
26pub use script::Script;
27
28mod datastar;
29#[doc(inline)]
30pub use datastar::{DatastarScript, DatastarSourceMap};
31
32mod css;
33#[doc(inline)]
34pub use css::Css;
35
36mod json;
37#[doc(inline)]
38pub use json::Json;
39
40mod json_lines;
41
42mod csv;
43#[doc(inline)]
44pub use csv::Csv;
45
46mod form;
47#[doc(inline)]
48pub use form::Form;
49
50pub mod robots_txt;
51
52mod octet_stream;
53#[doc(inline)]
54pub use octet_stream::OctetStream;
55
56pub mod redirect;
57#[doc(inline)]
58pub use redirect::Redirect;
59
60pub mod sse;
61pub use sse::Sse;
62
63#[cfg(feature = "html")]
64#[cfg_attr(docsrs, doc(cfg(feature = "html")))]
65pub mod partial_updates;
66#[cfg(feature = "html")]
67#[cfg_attr(docsrs, doc(cfg(feature = "html")))]
68pub use partial_updates::PartialUpdates;
69
70/// An [`IntoResponse`]-based result type that uses [`ErrorResponse`] as the error type.
71///
72/// All types which implement [`IntoResponse`] can be converted to an [`ErrorResponse`]. This makes
73/// it useful as a general purpose error type for functions which combine multiple distinct error
74/// types that all implement [`IntoResponse`].
75///
76/// # Example
77///
78/// ```
79/// use rama_http_types::{StatusCode, Response};
80/// use rama_http::service::web::response::IntoResponse;
81///
82/// // two fallible functions with different error types
83/// fn try_something() -> Result<(), ErrorA> {
84///     // ...
85///     # unimplemented!()
86/// }
87///
88/// fn try_something_else() -> Result<(), ErrorB> {
89///     // ...
90///     # unimplemented!()
91/// }
92///
93/// // each error type implements `IntoResponse`
94/// struct ErrorA;
95///
96/// impl IntoResponse for ErrorA {
97///     fn into_response(self) -> Response {
98///         // ...
99///         # unimplemented!()
100///     }
101/// }
102///
103/// enum ErrorB {
104///     SomethingWentWrong,
105/// }
106///
107/// impl IntoResponse for ErrorB {
108///     fn into_response(self) -> Response {
109///         // ...
110///         # unimplemented!()
111///     }
112/// }
113///
114/// // we can combine them using `rama_http::response::Result` and still use `?`
115/// async fn handler() -> rama_http::service::web::response::Result<&'static str> {
116///     // the errors are automatically converted to `ErrorResponse`
117///     try_something()?;
118///     try_something_else()?;
119///
120///     Ok("it worked!")
121/// }
122/// ```
123///
124/// # As a replacement for `std::result::Result`
125///
126/// Since `rama_http::response::Result` has a default error type you only have to specify the `Ok` type:
127///
128/// ```
129/// use rama_http_types::{Response, StatusCode};
130/// use rama_http::service::web::response::{IntoResponse, Result};
131///
132/// // `Result<T>` automatically uses `ErrorResponse` as the error type.
133/// async fn handler() -> Result<&'static str> {
134///     try_something()?;
135///
136///     Ok("it worked!")
137/// }
138///
139/// // You can still specify the error even if you've imported `rama_http::response::Result`
140/// fn try_something() -> Result<(), StatusCode> {
141///     // ...
142///     # unimplemented!()
143/// }
144/// ```
145pub type Result<T, E = ErrorResponse> = std::result::Result<T, E>;
146
147/// An [`IntoResponse`]-based error type
148///
149/// See [`Result`] for more details.
150#[derive(Debug)]
151pub struct ErrorResponse(Response);
152
153impl<T> From<T> for ErrorResponse
154where
155    T: IntoResponse,
156{
157    fn from(value: T) -> Self {
158        Self(value.into_response())
159    }
160}
161
162impl ErrorResponse {
163    pub fn into_response(self) -> Response {
164        self.0
165    }
166}