From f4b3588ae1669617d7562894a9004536d25ee67e Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 2 Dec 2024 20:46:14 +0100 Subject: [PATCH] Switch from axum to axum-core and http This will hopefully keep dependency churn lower. --- .vscode/settings.json | 3 +++ Cargo.toml | 6 +++++- src/axum.rs | 23 +++++++++++++++++------ 3 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6f5c4ed --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "rust-analyzer.cargo.features": "all" +} diff --git a/Cargo.toml b/Cargo.toml index 0e9c8d6..17b46e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/axum.rs b/src/axum.rs index 0387ba6..44059bb 100644 --- a/src/axum.rs +++ b/src/axum.rs @@ -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(), } }