use crate::{ClientResponse, FromResponse};
use axum_core::response::{IntoResponse, Response};
use dioxus_fullstack_core::ServerFnError;
use send_wrapper::SendWrapper;
use std::future::Future;
pub struct Text<T>(pub T);
impl<T> Text<T> {
pub fn new(text: T) -> Self {
Self(text)
}
}
impl<T: Into<String>> IntoResponse for Text<T> {
fn into_response(self) -> Response {
Response::builder()
.header("Content-Type", "text/plain; charset=utf-8")
.body(axum_core::body::Body::from(self.0.into()))
.unwrap()
}
}
impl<T: From<String>> FromResponse for Text<T> {
fn from_response(res: ClientResponse) -> impl Future<Output = Result<Self, ServerFnError>> {
SendWrapper::new(async move {
let text = res.text().await?;
Ok(Text::new(text.into()))
})
}
}