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