diff options
-rw-r--r-- | askama/Cargo.toml | 7 | ||||
-rw-r--r-- | askama/src/lib.rs | 28 | ||||
-rw-r--r-- | askama_derive/src/generator.rs | 8 | ||||
-rw-r--r-- | testing/Cargo.toml | 8 | ||||
-rw-r--r-- | testing/tests/actix_web.rs | 39 |
5 files changed, 36 insertions, 54 deletions
diff --git a/askama/Cargo.toml b/askama/Cargo.toml index 6db3bdb..c325395 100644 --- a/askama/Cargo.toml +++ b/askama/Cargo.toml @@ -23,7 +23,7 @@ serde-json = ["askama_shared/serde_json"] serde-yaml = ["askama_shared/serde_yaml"] with-iron = ["iron", "askama_derive/iron"] with-rocket = ["rocket", "askama_derive/rocket"] -with-actix-web = ["actix-web", "askama_derive/actix-web", "mime", "mime_guess", "bytes"] +with-actix-web = ["actix-web", "askama_derive/actix-web", "mime", "mime_guess", "bytes", "futures"] with-gotham = ["gotham", "askama_derive/gotham", "hyper", "mime", "mime_guess"] [dependencies] @@ -32,12 +32,13 @@ askama_escape = { version = "0.2.0", path = "../askama_escape" } askama_shared = { version = "0.8.0", path = "../askama_shared" } iron = { version = ">= 0.5, < 0.7", optional = true } rocket = { version = "0.4", optional = true } -actix-web = { version = "0.7", optional = true } +actix-web = { version = "2", optional = true } +futures = { version = "0.3", optional = true } mime = { version = "0.3", optional = true } mime_guess = { version = "2.0.0-alpha", optional = true } gotham = { version = "0.3", optional = true } hyper = { version = "0.12", optional = true } -bytes = { version = "0.4", optional = true } +bytes = { version = "0.5", optional = true } [package.metadata.docs.rs] features = [ "serde-json" ] diff --git a/askama/src/lib.rs b/askama/src/lib.rs index f36288a..47f7739 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -577,32 +577,6 @@ pub mod actix_web { use std::fmt; - struct BytesWriter { - buf: bytes::BytesMut, - } - - impl BytesWriter { - #[inline] - pub fn with_capacity(size: usize) -> Self { - Self { - buf: bytes::BytesMut::with_capacity(size), - } - } - - #[inline] - pub fn freeze(self) -> bytes::Bytes { - self.buf.freeze() - } - } - - impl fmt::Write for BytesWriter { - #[inline] - fn write_str(&mut self, buf: &str) -> fmt::Result { - self.buf.extend_from_slice(buf.as_bytes()); - Ok(()) - } - } - // actix_web technically has this as a pub fn in later versions, fs::file_extension_to_mime. // Older versions that don't have it exposed are easier this way. If ext is empty or no // associated type was found, then this returns `application/octet-stream`, in line with how @@ -617,7 +591,7 @@ pub mod actix_web { impl<T: super::Template> TemplateIntoResponse for T { fn into_response(&self) -> Result<HttpResponse, Error> { - let mut buffer = BytesWriter::with_capacity(self.size_hint()); + let mut buffer = actix_web::web::BytesMut::with_capacity(self.size_hint()); self.render_into(&mut buffer) .map_err(|_| ErrorInternalServerError("Template parsing error"))?; diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 9f60847..f76f6c1 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -242,15 +242,15 @@ impl<'a> Generator<'a> { // Implement Actix-web's `Responder`. fn impl_actix_web_responder(&mut self, buf: &mut Buffer) { self.write_header(buf, "::askama::actix_web::Responder", None); - buf.writeln("type Item = ::askama::actix_web::HttpResponse;"); + buf.writeln("type Future = ::futures::future::Ready<::std::result::Result<::askama::actix_web::HttpResponse, Self::Error>>;"); buf.writeln("type Error = ::askama::actix_web::Error;"); buf.writeln( - "fn respond_to<S>(self, _req: &::askama::actix_web::HttpRequest<S>) \ - -> ::std::result::Result<Self::Item, Self::Error> {", + "fn respond_to(self, _req: &::askama::actix_web::HttpRequest) \ + -> Self::Future {", ); buf.writeln("use ::askama::actix_web::TemplateIntoResponse;"); - buf.writeln("self.into_response()"); + buf.writeln("::futures::future::ready(self.into_response())"); buf.writeln("}"); buf.writeln("}"); diff --git a/testing/Cargo.toml b/testing/Cargo.toml index 73e0a8a..4aa80fe 100644 --- a/testing/Cargo.toml +++ b/testing/Cargo.toml @@ -6,7 +6,7 @@ workspace = ".." edition = "2018" [features] -actix = ["actix-web", "bytes", "askama/with-actix-web"] +actix = ["actix-web", "actix-rt", "bytes", "askama/with-actix-web", "futures"] default = [] full = ["actix", "with-iron", "serde-json", "with-gotham"] serde-json = ["serde_json", "askama/serde-json"] @@ -15,9 +15,11 @@ with-iron = ["iron", "askama/with-iron"] with-gotham = ["gotham", "askama/with-gotham", "mime", "hyper"] [dependencies] -actix-web = { version = "0.7", optional = true } +actix-web = { version = "2", optional = true } +actix-rt = { version = "1", optional = true } +futures = { version = "0.3", optional = true } askama = { path = "../askama", version = "*" } -bytes = { version = "0.4", optional = true } +bytes = { version = "0.5", optional = true } iron = { version = "0.6", optional = true } rocket = { version = "0.4", optional = true } serde_json = { version = "1.0", optional = true } diff --git a/testing/tests/actix_web.rs b/testing/tests/actix_web.rs index e434ce3..8beef3d 100644 --- a/testing/tests/actix_web.rs +++ b/testing/tests/actix_web.rs @@ -1,7 +1,7 @@ #![cfg(feature = "actix")] use actix_web::http::header::CONTENT_TYPE; use actix_web::test; -use actix_web::HttpMessage; +use actix_web::web; use askama::{actix_web::TemplateIntoResponse, Template}; use bytes::Bytes; @@ -11,39 +11,44 @@ struct HelloTemplate<'a> { name: &'a str, } -#[test] -fn test_actix_web() { - let mut srv = test::TestServer::new(|app| app.handler(|_| HelloTemplate { name: "world" })); +#[actix_rt::test] +async fn test_actix_web() { + let srv = test::start(|| { + actix_web::App::new() + .service(web::resource("/").to(|| async { HelloTemplate { name: "world" } })) + }); - let request = srv.get().finish().unwrap(); - let response = srv.execute(request.send()).unwrap(); + let request = srv.get("/"); + let mut response = request.send().await.unwrap(); assert!(response.status().is_success()); assert_eq!( response.headers().get(CONTENT_TYPE).unwrap(), "text/html; charset=utf-8" ); - let bytes = srv.execute(response.body()).unwrap(); + let bytes = response.body().await.unwrap(); assert_eq!(bytes, Bytes::from_static("Hello, world!".as_ref())); } -#[test] -fn test_actix_web_responder() { - let mut srv = test::TestServer::new(|app| { - app.handler(|_| { - let name = "world".to_owned(); - HelloTemplate { name: &name }.into_response() - }) +#[actix_rt::test] +async fn test_actix_web_responder() { + let srv = test::start(|| { + actix_web::App::new().service(web::resource("/").to(|| { + async { + let name = "world".to_owned(); + HelloTemplate { name: &name }.into_response() + } + })) }); - let request = srv.get().finish().unwrap(); - let response = srv.execute(request.send()).unwrap(); + let request = srv.get("/"); + let mut response = request.send().await.unwrap(); assert!(response.status().is_success()); assert_eq!( response.headers().get(CONTENT_TYPE).unwrap(), "text/html; charset=utf-8" ); - let bytes = srv.execute(response.body()).unwrap(); + let bytes = response.body().await.unwrap(); assert_eq!(bytes, Bytes::from_static("Hello, world!".as_ref())); } |