dioxus_fullstack/payloads/
text.rs

1use crate::{ClientResponse, FromResponse};
2use axum_core::response::{IntoResponse, Response};
3use dioxus_fullstack_core::ServerFnError;
4use send_wrapper::SendWrapper;
5use std::future::Future;
6
7/// A simple text response type.
8///
9/// The `T` parameter can be anything that converts to and from `String`, such as `Rc<str>` or `String`.
10///
11/// Unlike `Json` or plain `String`, this uses the `text/plain` content type. The `text/plain` header
12/// will be set on the request.
13pub struct Text<T>(pub T);
14
15impl<T> Text<T> {
16    /// Create a new text response.
17    pub fn new(text: T) -> Self {
18        Self(text)
19    }
20}
21
22impl<T: Into<String>> IntoResponse for Text<T> {
23    fn into_response(self) -> Response {
24        Response::builder()
25            .header("Content-Type", "text/plain; charset=utf-8")
26            .body(axum_core::body::Body::from(self.0.into()))
27            .unwrap()
28    }
29}
30
31impl<T: From<String>> FromResponse for Text<T> {
32    fn from_response(res: ClientResponse) -> impl Future<Output = Result<Self, ServerFnError>> {
33        SendWrapper::new(async move {
34            let text = res.text().await?;
35            Ok(Text::new(text.into()))
36        })
37    }
38}