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 62 63
pub mod file; pub use file::*; use async_trait::async_trait; use std::sync::Arc; use crate::http::{Request, Response}; use crate::{Depot, ServerConfig}; #[async_trait] pub trait Writer: Send { #[must_use = "future must be used"] async fn write(mut self, conf: Arc<ServerConfig>, 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, _conf: Arc<ServerConfig>, _req: &mut Request, _depot: &mut Depot, res: &mut Response) { res.render("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, _conf: Arc<ServerConfig>, _req: &mut Request, _depot: &mut Depot, res: &mut Response) { res.render("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, _conf: Arc<ServerConfig>, _req: &mut Request, _depot: &mut Depot, res: &mut Response) { res.render("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, _conf: Arc<ServerConfig>, _req: &mut Request, _depot: &mut Depot, res: &mut Response) { res.render("text/xml", self.0.as_ref().as_bytes()); } } #[async_trait] impl Writer for () { async fn write(mut self, _conf: Arc<ServerConfig>, _req: &mut Request, _depot: &mut Depot, _resp: &mut Response) {} }