From 75f32d3967e4d13b86b0d720ebc808c6fd9caa05 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 28 Jan 2020 22:17:22 +0100 Subject: Move Gotham integration into separate askama_gotham crate --- .github/workflows/rust.yml | 14 +++++++++++++ Cargo.toml | 1 + askama/Cargo.toml | 4 +--- askama/src/lib.rs | 24 ---------------------- askama_derive/src/generator.rs | 8 ++++---- askama_gotham/Cargo.toml | 21 +++++++++++++++++++ askama_gotham/src/lib.rs | 22 ++++++++++++++++++++ askama_gotham/templates/hello.html | 1 + askama_gotham/tests/basic.rs | 40 ++++++++++++++++++++++++++++++++++++ testing/Cargo.toml | 6 +----- testing/tests/gotham.rs | 42 -------------------------------------- 11 files changed, 105 insertions(+), 78 deletions(-) create mode 100644 askama_gotham/Cargo.toml create mode 100644 askama_gotham/src/lib.rs create mode 100644 askama_gotham/templates/hello.html create mode 100644 askama_gotham/tests/basic.rs delete mode 100644 testing/tests/gotham.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index fd0c6b4..c8ae1ea 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -61,6 +61,20 @@ jobs: cargo test --package askama_actix --all-targets cargo clippy --package askama_actix --all-targets -- -D warnings + Gotham: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + components: clippy + - run: | + cargo test --package askama_gotham --all-targets + cargo clippy --package askama_gotham --all-targets -- -D warnings + Rocket: runs-on: ubuntu-latest steps: diff --git a/Cargo.toml b/Cargo.toml index 55dd818..07f1889 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "askama", "askama_actix", + "askama_gotham", "askama_derive", "askama_escape", "askama_rocket", diff --git a/askama/Cargo.toml b/askama/Cargo.toml index fa0978a..78be3d4 100644 --- a/askama/Cargo.toml +++ b/askama/Cargo.toml @@ -27,7 +27,7 @@ num-traits = ["askama_shared/num-traits"] with-iron = ["iron", "askama_derive/iron"] with-rocket = ["askama_derive/rocket"] with-actix-web = ["askama_derive/actix-web"] -with-gotham = ["gotham", "askama_derive/gotham", "hyper", "mime", "mime_guess"] +with-gotham = ["askama_derive/gotham"] [dependencies] askama_derive = { version = "0.9.0", path = "../askama_derive" } @@ -36,8 +36,6 @@ askama_shared = { version = "0.9.0", path = "../askama_shared", default-features iron = { version = ">= 0.5, < 0.7", optional = true } mime = { version = "0.3", optional = true } mime_guess = { version = "2.0.0-alpha", optional = true } -gotham = { version = "0.3", default-features = false, optional = true } -hyper = { version = "0.12", optional = true } [package.metadata.docs.rs] features = ["config", "humansize", "num-traits", "serde-json", "serde-yaml"] diff --git a/askama/src/lib.rs b/askama/src/lib.rs index 8e9eebe..f3a7a22 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -552,30 +552,6 @@ pub mod mime { ]; } -#[cfg(feature = "with-gotham")] -pub mod gotham { - pub use gotham::handler::IntoResponse; - use gotham::helpers::http::response::{create_empty_response, create_response}; - pub use gotham::state::State; - pub use hyper::{Body, Response, StatusCode}; - - pub fn respond(t: &T, ext: &str) -> Response { - let mime_type = super::mime::extension_to_mime_type(ext).to_string(); - - match t.render() { - Ok(body) => Response::builder() - .status(StatusCode::OK) - .header("content-type", mime_type) - .body(body.into()) - .unwrap(), - Err(_) => Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .body(vec![].into()) - .unwrap(), - } - } -} - /// Old build script helper to rebuild crates if contained templates have changed /// /// This function is now deprecated and does nothing. diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 0d77c16..949019b 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -258,16 +258,16 @@ impl<'a> Generator<'a> { // Implement gotham's `IntoResponse`. fn impl_gotham_into_response(&mut self, buf: &mut Buffer) { - self.write_header(buf, "::askama::gotham::IntoResponse", None); + self.write_header(buf, "::askama_gotham::IntoResponse", None); buf.writeln( - "fn into_response(self, _state: &::askama::gotham::State)\ - -> ::askama::gotham::Response<::askama::gotham::Body> {", + "fn into_response(self, _state: &::askama_gotham::State)\ + -> ::askama_gotham::Response<::askama_gotham::Body> {", ); let ext = match self.input.path.extension() { Some(s) => s.to_str().unwrap(), None => "txt", }; - buf.writeln(&format!("::askama::gotham::respond(&self, {:?})", ext)); + buf.writeln(&format!("::askama_gotham::respond(&self, {:?})", ext)); buf.writeln("}"); buf.writeln("}"); } diff --git a/askama_gotham/Cargo.toml b/askama_gotham/Cargo.toml new file mode 100644 index 0000000..efd872d --- /dev/null +++ b/askama_gotham/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "askama_gotham" +version = "0.9.0" +authors = ["Dirkjan Ochtman "] +description = "Gotham integration for Askama templates" +documentation = "https://docs.rs/askama" +keywords = ["markup", "template", "jinja2", "html"] +categories = ["template-engine"] +homepage = "https://github.com/djc/askama" +repository = "https://github.com/djc/askama" +license = "MIT OR Apache-2.0" +workspace = ".." +edition = "2018" + +[dependencies] +askama = { version = "0.9.0", path = "../askama", features = ["with-gotham", "mime", "mime_guess"] } +gotham = { version = "0.3", default-features = false } +hyper = "0.12" + +[dev-dependencies] +mime = "0.3" diff --git a/askama_gotham/src/lib.rs b/askama_gotham/src/lib.rs new file mode 100644 index 0000000..c929a40 --- /dev/null +++ b/askama_gotham/src/lib.rs @@ -0,0 +1,22 @@ +pub use askama::*; + +pub use gotham::handler::IntoResponse; +pub use gotham::state::State; +pub use hyper::{Body, Response, StatusCode}; + +pub fn respond(t: &T, ext: &str) -> Response { + match t.render() { + Ok(body) => Response::builder() + .status(StatusCode::OK) + .header( + "content-type", + mime::extension_to_mime_type(ext).to_string(), + ) + .body(body.into()) + .unwrap(), + Err(_) => Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(vec![].into()) + .unwrap(), + } +} diff --git a/askama_gotham/templates/hello.html b/askama_gotham/templates/hello.html new file mode 100644 index 0000000..8149be7 --- /dev/null +++ b/askama_gotham/templates/hello.html @@ -0,0 +1 @@ +Hello, {{ name }}! diff --git a/askama_gotham/tests/basic.rs b/askama_gotham/tests/basic.rs new file mode 100644 index 0000000..fbb61a0 --- /dev/null +++ b/askama_gotham/tests/basic.rs @@ -0,0 +1,40 @@ +use askama::Template; +use gotham::state::State; +use gotham::test::TestServer; +use hyper::StatusCode; + +#[derive(Template)] +#[template(path = "hello.html")] +struct HelloTemplate<'a> { + name: &'a str, +} + +fn hello(state: State) -> (State, HelloTemplate<'static>) { + (state, HelloTemplate { name: "world" }) +} + +#[test] +fn test_gotham() { + let test_server = TestServer::new(|| Ok(hello)).expect("Failed to mount test router"); + + let res = test_server + .client() + .get("http://localhost/") + .perform() + .expect("Failed to send request to gotham"); + + assert_eq!(res.status(), StatusCode::OK); + { + let headers = res.headers(); + let content_type = headers + .get("content-type") + .expect("Response did not contain content-type header"); + assert_eq!( + content_type.to_str().unwrap(), + mime::TEXT_HTML_UTF_8.to_string() + ); + } + + let body = res.read_utf8_body().expect("failed to read response body"); + assert_eq!(&body, "Hello, world!"); +} diff --git a/testing/Cargo.toml b/testing/Cargo.toml index 424dd73..5517047 100644 --- a/testing/Cargo.toml +++ b/testing/Cargo.toml @@ -7,18 +7,14 @@ edition = "2018" [features] default = [] -full = ["with-iron", "serde-json", "with-gotham"] +full = ["with-iron", "serde-json"] serde-json = ["serde_json", "askama/serde-json"] with-iron = ["iron", "askama/with-iron"] -with-gotham = ["gotham", "askama/with-gotham", "mime", "hyper"] [dependencies] askama = { path = "../askama", version = "*" } iron = { version = "0.6", optional = true } serde_json = { version = "1.0", optional = true } -gotham = { version = "0.3", default-features = false, optional = true } -mime = { version = "0.3", optional = true } -hyper = { version = "0.12", optional = true } [dev-dependencies] criterion = "0.3" diff --git a/testing/tests/gotham.rs b/testing/tests/gotham.rs deleted file mode 100644 index 225a0ef..0000000 --- a/testing/tests/gotham.rs +++ /dev/null @@ -1,42 +0,0 @@ -#![cfg(feature = "with-gotham")] - -use askama::Template; -use gotham::state::State; -use gotham::test::TestServer; -use hyper::StatusCode; - -#[derive(Template)] -#[template(path = "hello.html")] -struct HelloTemplate<'a> { - name: &'a str, -} - -fn hello(state: State) -> (State, HelloTemplate<'static>) { - (state, HelloTemplate { name: "world" }) -} - -#[test] -fn test_gotham() { - let test_server = TestServer::new(|| Ok(hello)).expect("Failed to mount test router"); - - let res = test_server - .client() - .get("http://localhost/") - .perform() - .expect("Failed to send request to gotham"); - - assert_eq!(res.status(), StatusCode::OK); - { - let headers = res.headers(); - let content_type = headers - .get("content-type") - .expect("Response did not contain content-type header"); - assert_eq!( - content_type.to_str().unwrap(), - mime::TEXT_HTML_UTF_8.to_string() - ); - } - - let body = res.read_utf8_body().expect("failed to read response body"); - assert_eq!(&body, "Hello, world!"); -} -- cgit