1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
use async_trait::async_trait; use crate::http::{Request, Response}; use crate::http::header::HeaderValue; use crate::Depot; #[async_trait] pub trait Writer: Send { #[must_use = "future must be used"] async fn write(mut self, req: &mut Request, depot: &mut Depot, res: &mut Response); } pub struct HtmlTextContent<T>(T); #[async_trait] impl<T> Writer for HtmlTextContent<T> where T: AsRef<str> + Send, { async fn write(mut self, _req: &mut Request, _depot: &mut Depot, res: &mut Response) { res.render_binary(HeaderValue::from_static("text/html"), self.0.as_ref().as_bytes()); } } pub struct JsonTextContent<T>(T); #[async_trait] impl<T> Writer for JsonTextContent<T> where T: AsRef<str> + Send, { async fn write(mut self, _req: &mut Request, _depot: &mut Depot, res: &mut Response) { res.render_binary(HeaderValue::from_static("application/json"), self.0.as_ref().as_bytes()); } } pub struct PlainTextContent<T>(T); #[async_trait] impl<T> Writer for PlainTextContent<T> where T: AsRef<str> + Send, { async fn write(mut self, _req: &mut Request, _depot: &mut Depot, res: &mut Response) { res.render_binary(HeaderValue::from_static("text/plain"), self.0.as_ref().as_bytes()); } } pub struct XmlTextContent<T>(T); #[async_trait] impl<T> Writer for XmlTextContent<T> where T: AsRef<str> + Send, { async fn write(mut self, _req: &mut Request, _depot: &mut Depot, res: &mut Response) { res.render_binary(HeaderValue::from_static("text/xml"), self.0.as_ref().as_bytes()); } } #[allow(clippy::unit_arg)] #[async_trait] impl Writer for () { async fn write(mut self, _req: &mut Request, _depot: &mut Depot, _resp: &mut Response) {} }