diff options
author | Sergey Pashinin <sergey@pashinin.com> | 2021-11-11 12:55:09 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-11 10:55:09 +0100 |
commit | a8503e0fa2d6065b1c471becf76dde68571b7984 (patch) | |
tree | e893f04e6e5e01397ff675a2779515a621acd2a6 | |
parent | cfe83bcb73b9e0cfd8a2d3151e4cab38327eabea (diff) | |
download | askama-a8503e0fa2d6065b1c471becf76dde68571b7984.tar.gz askama-a8503e0fa2d6065b1c471becf76dde68571b7984.tar.bz2 askama-a8503e0fa2d6065b1c471becf76dde68571b7984.zip |
Prepare for actix-web v4 (#553)
Diffstat (limited to '')
-rw-r--r-- | askama_actix/Cargo.toml | 7 | ||||
-rw-r--r-- | askama_actix/src/lib.rs | 15 | ||||
-rw-r--r-- | askama_actix/tests/basic.rs | 7 | ||||
-rw-r--r-- | askama_shared/src/generator.rs | 6 |
4 files changed, 17 insertions, 18 deletions
diff --git a/askama_actix/Cargo.toml b/askama_actix/Cargo.toml index 253fbd0..8514a9b 100644 --- a/askama_actix/Cargo.toml +++ b/askama_actix/Cargo.toml @@ -14,10 +14,11 @@ readme = "README.md" edition = "2018" [dependencies] -actix-web = { version = "3", default-features = false } +actix-web = { version = "4.0.0-beta.10", default-features = false } askama = { version = "0.11.0-beta.1", path = "../askama", default-features = false, features = ["with-actix-web", "mime", "mime_guess"] } -bytes = { version = "0.5" } +bytes = { version = "1" } futures-util = { version = "0.3" } [dev-dependencies] -actix-rt = { version = "1", default-features = false } +actix-rt = { version = "2", default-features = false } +actix-test = "0.1.0-beta.5" diff --git a/askama_actix/src/lib.rs b/askama_actix/src/lib.rs index bb58174..2496d92 100644 --- a/askama_actix/src/lib.rs +++ b/askama_actix/src/lib.rs @@ -3,23 +3,24 @@ pub use askama::*; use bytes::BytesMut; -use actix_web::{error::ErrorInternalServerError, Error, HttpResponse}; +use actix_web::{error::ErrorInternalServerError, HttpResponse}; pub trait TemplateToResponse { - fn to_response(&self) -> ::std::result::Result<HttpResponse, Error>; + fn to_response(&self) -> HttpResponse; } impl<T: askama::Template> TemplateToResponse for T { - fn to_response(&self) -> ::std::result::Result<HttpResponse, Error> { + fn to_response(&self) -> HttpResponse { let mut buffer = BytesMut::with_capacity(self.size_hint()); - self.render_into(&mut buffer) - .map_err(|_| ErrorInternalServerError("Template parsing error"))?; + if self.render_into(&mut buffer).is_err() { + return ErrorInternalServerError("Template parsing error").error_response(); + } let ctype = askama::mime::extension_to_mime_type(self.extension().unwrap_or("txt")).to_string(); - Ok(HttpResponse::Ok() + HttpResponse::Ok() .content_type(ctype.as_str()) - .body(buffer.freeze())) + .body(buffer.freeze()) } } diff --git a/askama_actix/tests/basic.rs b/askama_actix/tests/basic.rs index 125270f..070cbf6 100644 --- a/askama_actix/tests/basic.rs +++ b/askama_actix/tests/basic.rs @@ -1,5 +1,4 @@ use actix_web::http::header::CONTENT_TYPE; -use actix_web::test; use actix_web::web; use askama_actix::{Template, TemplateToResponse}; use bytes::Bytes; @@ -12,7 +11,7 @@ struct HelloTemplate<'a> { #[actix_rt::test] async fn test_actix_web() { - let srv = test::start(|| { + let srv = actix_test::start(|| { actix_web::App::new() .service(web::resource("/").to(|| async { HelloTemplate { name: "world" } })) }); @@ -31,10 +30,10 @@ async fn test_actix_web() { #[actix_rt::test] async fn test_actix_web_responder() { - let srv = 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 7500cc7..6c1b151 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -210,15 +210,13 @@ 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( "fn respond_to(self, _req: &::actix_web::HttpRequest) \ - -> Self::Future {", + -> ::actix_web::HttpResponse {", )?; buf.writeln("use ::askama_actix::TemplateToResponse;")?; - buf.writeln("::askama_actix::futures::ready(self.to_response())")?; + buf.writeln("self.to_response()")?; buf.writeln("}")?; buf.writeln("}") |