mirror of
https://github.com/tokio-rs/axum.git
synced 2024-12-29 07:48:39 +01:00
Add Html
, Css
, JavaScript
, and Wasm
response types (#1921)
This commit is contained in:
parent
018d65da14
commit
e97462d452
2 changed files with 85 additions and 1 deletions
|
@ -7,7 +7,12 @@ and this project adheres to [Semantic Versioning].
|
||||||
|
|
||||||
# Unreleased
|
# Unreleased
|
||||||
|
|
||||||
- None.
|
- **added:** Add `Html` response type ([#1921])
|
||||||
|
- **added:** Add `Css` response type ([#1921])
|
||||||
|
- **added:** Add `JavaScript` response type ([#1921])
|
||||||
|
- **added:** Add `Wasm` response type ([#1921])
|
||||||
|
|
||||||
|
[#1921]: https://github.com/tokio-rs/axum/pull/1921
|
||||||
|
|
||||||
# 0.7.3 (11. April, 2023)
|
# 0.7.3 (11. April, 2023)
|
||||||
|
|
||||||
|
|
|
@ -9,3 +9,82 @@ pub use erased_json::ErasedJson;
|
||||||
#[cfg(feature = "json-lines")]
|
#[cfg(feature = "json-lines")]
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use crate::json_lines::JsonLines;
|
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",
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue