Switch from axum to axum-core and http

This will hopefully keep dependency churn lower.
This commit is contained in:
Joscha 2024-12-02 20:46:14 +01:00
parent cbc313ec52
commit f4b3588ae1
3 changed files with 25 additions and 7 deletions

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"rust-analyzer.cargo.features": "all"
}

View file

@ -9,8 +9,12 @@ license = "MIT OR Apache-2.0"
keywords = ["html", "svg", "mathml", "hiccup"]
categories = ["web-programming", "template-engine"]
[features]
axum = ["dep:axum-core", "dep:http"]
[dependencies]
axum = { version = "0.7.9", optional = true }
axum-core = { version = "0.4.5", optional = true }
http = { version = "1.1.0", optional = true }
[lints]
rust.unsafe_code = { level = "forbid", priority = 1 }

View file

@ -1,14 +1,25 @@
use axum::{
http::StatusCode,
response::{Html, IntoResponse},
};
use axum_core::response::IntoResponse;
use http::{header, HeaderValue, StatusCode};
use crate::{Document, Render};
// https://github.com/hyperium/mime/blob/ce5062d216bf757a0ed3fc70f0fe255d1c8d74ae/src/lib.rs#L753
const TEXT_HTML_UTF_8: &str = "text/html; charset=utf-8";
impl IntoResponse for Document {
fn into_response(self) -> axum::response::Response {
fn into_response(self) -> axum_core::response::Response {
match self.render_to_string() {
Ok(html) => Html(html).into_response(),
// Keeping dependency churn low by manually reimplementing
// https://github.com/tokio-rs/axum/blob/b5a01092216d0fa5ab950cbd7030ebcc925ceb33/axum/src/response/mod.rs#L40-L54
Ok(html) => (
[(
header::CONTENT_TYPE,
HeaderValue::from_static(TEXT_HTML_UTF_8),
)],
html,
)
.into_response(),
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response(),
}
}