// only enables the `doc_cfg` feature when // the `docsrs` configuration attribute is defined #![cfg_attr(docsrs, feature(doc_cfg))] use axum::{http::StatusCode, response::IntoResponse}; use axum_extra::{headers::ContentType, TypedHeader}; use rstml_component::{HtmlContent, HtmlFormatter}; pub struct Html(pub C); impl Html where C: FnOnce(&mut HtmlFormatter) -> std::fmt::Result, { pub fn from_fn(f: C) -> Self { Html(f) } } impl From for Html { fn from(value: C) -> Self { Self(value) } } impl HtmlContent for Html { fn fmt(self, formatter: &mut HtmlFormatter) -> std::fmt::Result { self.0.fmt(formatter) } } impl IntoResponse for Html { fn into_response(self) -> axum::response::Response { match self.0.into_bytes() { Ok(bytes) => (TypedHeader(ContentType::html()), bytes).into_response(), Err(_e) => (StatusCode::INTERNAL_SERVER_ERROR, "Internal Server Error").into_response(), } } } pub trait HtmlContentAxiosExt: Sized { fn into_html(self) -> Html; fn into_response(self) -> axum::response::Response; } impl HtmlContentAxiosExt for C { fn into_html(self) -> Html { Html(self) } fn into_response(self) -> axum::response::Response { IntoResponse::into_response(self.into_html()) } }