axum_extra/response/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! Additional types for generating responses.

#[cfg(feature = "erased-json")]
mod erased_json;

#[cfg(feature = "erased-json")]
pub use erased_json::ErasedJson;

#[cfg(feature = "json-lines")]
#[doc(no_inline)]
pub use crate::json_lines::JsonLines;

macro_rules! mime_response {
    (
        $(#[$m:meta])*
        $ident:ident,
        $mime:ident,
    ) => {
        mime_response! {
            $(#[$m])*
            $ident,
            mime::$mime.as_ref(),
        }
    };

    (
        $(#[$m:meta])*
        $ident:ident,
        $mime:expr,
    ) => {
        $(#[$m])*
        #[derive(Clone, Copy, Debug)]
        #[must_use]
        pub struct $ident<T>(pub T);

        impl<T> axum::response::IntoResponse for $ident<T>
        where
            T: axum::response::IntoResponse,
        {
            fn into_response(self) -> axum::response::Response {
                (
                    [(
                        http::header::CONTENT_TYPE,
                        http::HeaderValue::from_static($mime),
                    )],
                    self.0,
                )
                    .into_response()
            }
        }

        impl<T> From<T> for $ident<T> {
            fn from(inner: T) -> Self {
                Self(inner)
            }
        }
    };
}

mime_response! {
    /// A HTML response.
    ///
    /// Will automatically get `Content-Type: text/html; charset=utf-8`.
    Html,
    TEXT_HTML_UTF_8,
}

mime_response! {
    /// A JavaScript response.
    ///
    /// Will automatically get `Content-Type: application/javascript; charset=utf-8`.
    JavaScript,
    APPLICATION_JAVASCRIPT_UTF_8,
}

mime_response! {
    /// A CSS response.
    ///
    /// Will automatically get `Content-Type: text/css; charset=utf-8`.
    Css,
    TEXT_CSS_UTF_8,
}

mime_response! {
    /// A WASM response.
    ///
    /// Will automatically get `Content-Type: application/wasm`.
    Wasm,
    "application/wasm",
}

#[cfg(feature = "typed-header")]
#[doc(no_inline)]
pub use crate::typed_header::TypedHeader;