xitca_web/handler/types/
text.rs1use core::convert::Infallible;
4
5use crate::{
6 body::{BodyStream, ResponseBody},
7 context::WebContext,
8 error::{Error, forward_blank_bad_request},
9 handler::{FromRequest, Responder},
10 http::{WebResponse, const_header_value::TEXT_UTF8, header::CONTENT_TYPE},
11 service::Service,
12};
13
14impl<'a, 'r, C, B> FromRequest<'a, WebContext<'r, C, B>> for String
15where
16 B: BodyStream + Default,
17{
18 type Type<'b> = String;
19 type Error = Error;
20
21 #[inline]
22 async fn from_request(ctx: &'a WebContext<'r, C, B>) -> Result<Self, Self::Error> {
23 let vec = Vec::from_request(ctx).await?;
24 String::from_utf8(vec).map_err(Error::from_service)
25 }
26}
27
28forward_blank_bad_request!(std::string::FromUtf8Error);
29
30macro_rules! text_utf8 {
31 ($type: ty) => {
32 impl<'r, C, B> Responder<WebContext<'r, C, B>> for $type {
33 type Response = WebResponse;
34 type Error = Error;
35
36 #[inline]
37 async fn respond(self, ctx: WebContext<'r, C, B>) -> Result<Self::Response, Self::Error> {
38 Text(self).respond(ctx).await
39 }
40
41 #[inline]
42 fn map(self, res: Self::Response) -> Result<Self::Response, Self::Error> {
43 Responder::<WebContext<'r, C, B>>::map(Text(self), res)
44 }
45 }
46 };
47}
48
49text_utf8!(&'static str);
50text_utf8!(String);
51text_utf8!(Box<str>);
52text_utf8!(std::borrow::Cow<'static, str>);
53
54#[derive(Clone)]
56pub struct Text<T>(pub T);
57
58impl<'r, C, B, T> Responder<WebContext<'r, C, B>> for Text<T>
59where
60 T: Into<ResponseBody>,
61{
62 type Response = WebResponse;
63 type Error = Error;
64
65 async fn respond(self, ctx: WebContext<'r, C, B>) -> Result<Self::Response, Self::Error> {
66 let mut res = ctx.into_response(self.0.into());
67 res.headers_mut().insert(CONTENT_TYPE, TEXT_UTF8);
68 Ok(res)
69 }
70
71 fn map(self, res: Self::Response) -> Result<Self::Response, Self::Error> {
72 let mut res = res.map(|_| self.0.into());
73 res.headers_mut().insert(CONTENT_TYPE, TEXT_UTF8);
74 Ok(res)
75 }
76}
77
78impl<T> Service for Text<T>
79where
80 T: Clone,
81{
82 type Response = Self;
83 type Error = Infallible;
84
85 async fn call(&self, _: ()) -> Result<Self::Response, Self::Error> {
86 Ok(self.clone())
87 }
88}
89
90impl<'r, C, B, T> Service<WebContext<'r, C, B>> for Text<T>
91where
92 T: Into<ResponseBody> + Clone,
93{
94 type Response = WebResponse;
95 type Error = Error;
96
97 #[inline]
98 async fn call(&self, ctx: WebContext<'r, C, B>) -> Result<Self::Response, Self::Error> {
99 self.clone().respond(ctx).await
100 }
101}