axum_extra/response/
mod.rs

1//! Additional types for generating responses.
2
3#[cfg(feature = "erased-json")]
4mod erased_json;
5
6#[cfg(feature = "attachment")]
7mod attachment;
8
9#[cfg(feature = "multipart")]
10pub mod multiple;
11
12#[cfg(feature = "erased-json")]
13pub use erased_json::ErasedJson;
14
15/// _not_ public API
16#[cfg(feature = "erased-json")]
17#[doc(hidden)]
18pub use erased_json::private as __private_erased_json;
19
20#[cfg(feature = "json-lines")]
21#[doc(no_inline)]
22pub use crate::json_lines::JsonLines;
23
24#[cfg(feature = "attachment")]
25pub use attachment::Attachment;
26
27macro_rules! mime_response {
28    (
29        $(#[$m:meta])*
30        $ident:ident,
31        $mime:ident,
32    ) => {
33        mime_response! {
34            $(#[$m])*
35            $ident,
36            mime::$mime.as_ref(),
37        }
38    };
39
40    (
41        $(#[$m:meta])*
42        $ident:ident,
43        $mime:expr,
44    ) => {
45        $(#[$m])*
46        #[derive(Clone, Copy, Debug)]
47        #[must_use]
48        pub struct $ident<T>(pub T);
49
50        impl<T> axum::response::IntoResponse for $ident<T>
51        where
52            T: axum::response::IntoResponse,
53        {
54            fn into_response(self) -> axum::response::Response {
55                (
56                    [(
57                        http::header::CONTENT_TYPE,
58                        http::HeaderValue::from_static($mime),
59                    )],
60                    self.0,
61                )
62                    .into_response()
63            }
64        }
65
66        impl<T> From<T> for $ident<T> {
67            fn from(inner: T) -> Self {
68                Self(inner)
69            }
70        }
71    };
72}
73
74mime_response! {
75    /// A HTML response.
76    ///
77    /// Will automatically get `Content-Type: text/html; charset=utf-8`.
78    Html,
79    TEXT_HTML_UTF_8,
80}
81
82mime_response! {
83    /// A JavaScript response.
84    ///
85    /// Will automatically get `Content-Type: application/javascript; charset=utf-8`.
86    JavaScript,
87    APPLICATION_JAVASCRIPT_UTF_8,
88}
89
90mime_response! {
91    /// A CSS response.
92    ///
93    /// Will automatically get `Content-Type: text/css; charset=utf-8`.
94    Css,
95    TEXT_CSS_UTF_8,
96}
97
98mime_response! {
99    /// A WASM response.
100    ///
101    /// Will automatically get `Content-Type: application/wasm`.
102    Wasm,
103    "application/wasm",
104}
105
106#[cfg(feature = "typed-header")]
107#[doc(no_inline)]
108pub use crate::typed_header::TypedHeader;