diff options
-rw-r--r-- | askama/src/lib.rs | 21 | ||||
-rw-r--r-- | askama_derive/src/generator.rs | 7 | ||||
-rw-r--r-- | testing/tests/actix_web.rs | 4 |
3 files changed, 10 insertions, 22 deletions
diff --git a/askama/src/lib.rs b/askama/src/lib.rs index faf2f24..dea5833 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -500,25 +500,16 @@ pub mod actix_web { }; use self::mime_guess::get_mime_type; - pub fn respond<T: super::Template>(t: &T, ext: &str) -> Result<HttpResponse, Error> { - let rsp = t - .render() - .map_err(|_| ErrorInternalServerError("Template parsing error"))?; - let ctype = get_mime_type(ext).to_string(); - Ok(HttpResponse::Ok().content_type(ctype.as_str()).body(rsp)) + pub trait TemplateIntoResponse { + fn into_response(&self) -> Result<HttpResponse, Error>; } - pub trait TemplateResponder { - fn responder(&self) -> Result<HttpResponse, Error>; - } - - impl<T: super::Template> TemplateResponder for T { - fn responder(&self) -> Result<HttpResponse, Error> { + impl<T: super::Template> TemplateIntoResponse for T { + fn into_response(&self) -> Result<HttpResponse, Error> { let rsp = self .render() - .map_err(|_| ErrorInternalServerError("Template render error"))?; - let ext = T::extension().unwrap_or("txt"); - let ctype = get_mime_type(ext).to_string(); + .map_err(|_| ErrorInternalServerError("Template parsing error"))?; + let ctype = get_mime_type(T::extension().unwrap_or("txt")).to_string(); Ok(HttpResponse::Ok().content_type(ctype.as_str()).body(rsp)) } } diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 8f2866e..4a6eb89 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -234,11 +234,8 @@ impl<'a> Generator<'a> { -> ::std::result::Result<Self::Item, Self::Error> {", ); - let ext = match self.input.path.extension() { - Some(s) => s.to_str().unwrap(), - None => "txt", - }; - buf.writeln(&format!("::askama::actix_web::respond(&self, {:?})", ext)); + buf.writeln("use ::askama::actix_web::TemplateIntoResponse;"); + buf.writeln("self.into_response()"); buf.writeln("}"); buf.writeln("}"); diff --git a/testing/tests/actix_web.rs b/testing/tests/actix_web.rs index 267736d..7f2d835 100644 --- a/testing/tests/actix_web.rs +++ b/testing/tests/actix_web.rs @@ -2,7 +2,7 @@ use actix_web::http::header::CONTENT_TYPE; use actix_web::test; use actix_web::HttpMessage; -use askama::{actix_web::TemplateResponder, Template}; +use askama::{actix_web::TemplateIntoResponse, Template}; use bytes::Bytes; #[derive(Template)] @@ -29,7 +29,7 @@ fn test_actix_web_responder() { let mut srv = test::TestServer::new(|app| { app.handler(|_| { let name = "world".to_owned(); - HelloTemplate { name: &name }.responder() + HelloTemplate { name: &name }.into_response() }) }); |