axum_extra/response/
mod.rs

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