aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar René Kijewski <kijewski@library.vetmed.fu-berlin.de>2022-01-05 11:38:36 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2022-01-05 13:57:53 +0100
commit783073b4fbc8bb974b04467b821c15e88d3c970e (patch)
tree722a24b76f50757b99ebc6dc0bf6faeedbb83f57
parent2f8bdba17c5602d5f19330fcb7db474c9f2b1662 (diff)
downloadaskama-783073b4fbc8bb974b04467b821c15e88d3c970e.tar.gz
askama-783073b4fbc8bb974b04467b821c15e88d3c970e.tar.bz2
askama-783073b4fbc8bb974b04467b821c15e88d3c970e.zip
Update for actix-web beta
-rw-r--r--askama_actix/Cargo.toml5
-rw-r--r--askama_actix/src/lib.rs36
-rw-r--r--askama_actix/tests/basic.rs6
-rw-r--r--askama_shared/src/generator.rs10
4 files changed, 25 insertions, 32 deletions
diff --git a/askama_actix/Cargo.toml b/askama_actix/Cargo.toml
index f5590cf..54329bb 100644
--- a/askama_actix/Cargo.toml
+++ b/askama_actix/Cargo.toml
@@ -14,10 +14,9 @@ edition = "2018"
[dependencies]
actix-web = { version = "4.0.0-beta.19", default-features = false }
-askama = { version = "0.11", path = "../askama", default-features = false, features = ["with-actix-web", "mime", "mime_guess"] }
-bytes = { version = "1" }
-futures-util = { version = "0.3" }
+askama = { version = "0.11.0", path = "../askama", default-features = false, features = ["with-actix-web", "mime", "mime_guess"] }
[dev-dependencies]
actix-rt = { version = "2", default-features = false }
actix-test = "=0.1.0-beta.11"
+bytes = { version = "1" }
diff --git a/askama_actix/src/lib.rs b/askama_actix/src/lib.rs
index d9ac62d..71af8cc 100644
--- a/askama_actix/src/lib.rs
+++ b/askama_actix/src/lib.rs
@@ -1,31 +1,29 @@
#![deny(elided_lifetimes_in_paths)]
+use actix_web::body::BoxBody;
+use actix_web::http::StatusCode;
+use actix_web::HttpResponseBuilder;
+use askama::mime::extension_to_mime_type;
pub use askama::*;
-use bytes::BytesMut;
-use actix_web::{error::ErrorInternalServerError, Error, HttpResponse};
+use actix_web::HttpResponse;
pub trait TemplateToResponse {
- fn to_response(&self) -> std::result::Result<HttpResponse, Error>;
+ fn to_response(&self) -> HttpResponse<BoxBody>;
}
impl<T: askama::Template> TemplateToResponse for T {
- fn to_response(&self) -> std::result::Result<HttpResponse, Error> {
- let mut buffer = BytesMut::with_capacity(T::SIZE_HINT);
- if self.render_into(&mut buffer).is_err() {
- return Err(ErrorInternalServerError("Template parsing error"));
+ fn to_response(&self) -> HttpResponse<BoxBody> {
+ match self.render() {
+ Ok(buffer) => {
+ let ctype = extension_to_mime_type(T::EXTENSION.unwrap_or("txt"));
+ HttpResponseBuilder::new(StatusCode::OK)
+ .content_type(ctype)
+ .body(buffer)
+ }
+ Err(err) => {
+ HttpResponse::from_error(Box::new(err) as Box<dyn std::error::Error + 'static>)
+ }
}
-
- let ctype = askama::mime::extension_to_mime_type(T::EXTENSION.unwrap_or("txt")).to_string();
- Ok(HttpResponse::Ok()
- .content_type(ctype.as_str())
- .body(buffer.freeze()))
}
}
-
-// Re-exported for use by generated code
-#[doc(hidden)]
-pub mod futures {
- pub use futures_util::future::ready;
- pub use futures_util::future::Ready;
-}
diff --git a/askama_actix/tests/basic.rs b/askama_actix/tests/basic.rs
index c540076..070cbf6 100644
--- a/askama_actix/tests/basic.rs
+++ b/askama_actix/tests/basic.rs
@@ -11,7 +11,7 @@ struct HelloTemplate<'a> {
#[actix_rt::test]
async fn test_actix_web() {
- let srv = actix_web::test::start(|| {
+ let srv = actix_test::start(|| {
actix_web::App::new()
.service(web::resource("/").to(|| async { HelloTemplate { name: "world" } }))
});
@@ -30,10 +30,10 @@ async fn test_actix_web() {
#[actix_rt::test]
async fn test_actix_web_responder() {
- let srv = actix_web::test::start(|| {
+ let srv = actix_test::start(|| {
actix_web::App::new().service(web::resource("/").to(|| async {
let name = "world".to_owned();
- HelloTemplate { name: &name }.to_response()
+ HelloTemplate { name: &name }.to_response().await
}))
});
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs
index 398c106..ad413ea 100644
--- a/askama_shared/src/generator.rs
+++ b/askama_shared/src/generator.rs
@@ -201,16 +201,12 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
// Implement Actix-web's `Responder`.
fn impl_actix_web_responder(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
self.write_header(buf, "::actix_web::Responder", None)?;
- buf.writeln("type Future = ::askama_actix::futures::Ready<::std::result::Result<::actix_web::HttpResponse, Self::Error>>;")?;
- buf.writeln("type Error = ::actix_web::Error;")?;
+ buf.writeln("type Body = ::actix_web::body::BoxBody;")?;
buf.writeln(
"fn respond_to(self, _req: &::actix_web::HttpRequest) \
- -> Self::Future {",
+ -> ::actix_web::web::HttpResponse<Self::Body> {",
)?;
-
- buf.writeln("use ::askama_actix::TemplateToResponse;")?;
- buf.writeln("::askama_actix::futures::ready(self.to_response())")?;
-
+ buf.writeln("<Self as ::askama_actix::TemplateToResponse>::to_response(&self)")?;
buf.writeln("}")?;
buf.writeln("}")
}