From ff24eef1ff7dcf3849fd1e6d9efec0de2bc57005 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 28 Jan 2020 22:49:04 +0100 Subject: Move Iron integration into a separate askama_iron crate --- .github/workflows/rust.yml | 18 +++++++++--------- Cargo.toml | 1 + askama/Cargo.toml | 3 +-- askama/src/lib.rs | 7 ------- askama_derive/src/generator.rs | 8 ++++---- askama_iron/Cargo.toml | 17 +++++++++++++++++ askama_iron/src/lib.rs | 3 +++ askama_iron/templates/hello.html | 1 + askama_iron/templates/hello.txt | 1 + askama_iron/tests/basic.rs | 36 ++++++++++++++++++++++++++++++++++++ testing/Cargo.toml | 6 +----- testing/templates/hello.txt | 1 - testing/tests/iron.rs | 37 ------------------------------------- 13 files changed, 74 insertions(+), 65 deletions(-) create mode 100644 askama_iron/Cargo.toml create mode 100644 askama_iron/src/lib.rs create mode 100644 askama_iron/templates/hello.html create mode 100644 askama_iron/templates/hello.txt create mode 100644 askama_iron/tests/basic.rs delete mode 100644 testing/templates/hello.txt delete mode 100644 testing/tests/iron.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index c8ae1ea..3c7fab9 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -34,7 +34,7 @@ jobs: with: command: test - stable-integrations: + Actix-Web: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 @@ -44,10 +44,10 @@ jobs: toolchain: stable override: true - run: | - cd testing - cargo test --features full + cargo test --package askama_actix --all-targets + cargo clippy --package askama_actix --all-targets -- -D warnings - Actix-Web: + Gotham: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 @@ -58,10 +58,10 @@ jobs: override: true components: clippy - run: | - cargo test --package askama_actix --all-targets - cargo clippy --package askama_actix --all-targets -- -D warnings + cargo test --package askama_gotham --all-targets + cargo clippy --package askama_gotham --all-targets -- -D warnings - Gotham: + Iron: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 @@ -72,8 +72,8 @@ jobs: override: true components: clippy - run: | - cargo test --package askama_gotham --all-targets - cargo clippy --package askama_gotham --all-targets -- -D warnings + cargo test --package askama_iron --all-targets + cargo clippy --package askama_iron --all-targets -- -D warnings Rocket: runs-on: ubuntu-latest diff --git a/Cargo.toml b/Cargo.toml index 07f1889..6ff87a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ members = [ "askama_gotham", "askama_derive", "askama_escape", + "askama_iron", "askama_rocket", "askama_shared", "testing", diff --git a/askama/Cargo.toml b/askama/Cargo.toml index 78be3d4..a9749ba 100644 --- a/askama/Cargo.toml +++ b/askama/Cargo.toml @@ -24,7 +24,7 @@ humansize = ["askama_shared/humansize"] serde-json = ["askama_shared/json"] serde-yaml = ["askama_shared/yaml"] num-traits = ["askama_shared/num-traits"] -with-iron = ["iron", "askama_derive/iron"] +with-iron = ["askama_derive/iron"] with-rocket = ["askama_derive/rocket"] with-actix-web = ["askama_derive/actix-web"] with-gotham = ["askama_derive/gotham"] @@ -33,7 +33,6 @@ with-gotham = ["askama_derive/gotham"] askama_derive = { version = "0.9.0", path = "../askama_derive" } askama_escape = { version = "0.3.0", path = "../askama_escape" } askama_shared = { version = "0.9.0", path = "../askama_shared", default-features = false } -iron = { version = ">= 0.5, < 0.7", optional = true } mime = { version = "0.3", optional = true } mime_guess = { version = "2.0.0-alpha", optional = true } diff --git a/askama/src/lib.rs b/askama/src/lib.rs index f3a7a22..4e4611b 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -516,13 +516,6 @@ pub use crate::shared::helpers; pub use crate::shared::{read_config_file, Error, MarkupDisplay, Result}; pub use askama_derive::*; -#[cfg(feature = "with-iron")] -pub mod iron { - pub use iron::headers::ContentType; - pub use iron::modifier::Modifier; - pub use iron::response::Response; -} - pub mod mime { #[cfg(all(feature = "mime_guess", feature = "mime"))] pub fn extension_to_mime_type(ext: &str) -> mime_guess::Mime { diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 949019b..9532057 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -191,12 +191,12 @@ impl<'a> Generator<'a> { fn impl_modifier_response(&mut self, buf: &mut Buffer) { self.write_header( buf, - "::askama::iron::Modifier<::askama::iron::Response>", + "::askama_iron::Modifier<::askama_iron::Response>", None, ); - buf.writeln("fn modify(self, res: &mut ::askama::iron::Response) {"); + buf.writeln("fn modify(self, res: &mut ::askama_iron::Response) {"); buf.writeln( - "res.body = Some(Box::new(::askama::Template::render(&self).unwrap().into_bytes()));", + "res.body = Some(Box::new(::askama_iron::Template::render(&self).unwrap().into_bytes()));", ); let ext = self @@ -206,7 +206,7 @@ impl<'a> Generator<'a> { .map_or("", |s| s.to_str().unwrap_or("")); match ext { "html" | "htm" => { - buf.writeln("::askama::iron::ContentType::html().0.modify(res);"); + buf.writeln("::askama_iron::ContentType::html().0.modify(res);"); } _ => (), }; diff --git a/askama_iron/Cargo.toml b/askama_iron/Cargo.toml new file mode 100644 index 0000000..35c585d --- /dev/null +++ b/askama_iron/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "askama_iron" +version = "0.9.0" +authors = ["Dirkjan Ochtman "] +description = "Iron 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-iron"] } +iron = { version = ">= 0.5, < 0.7" } diff --git a/askama_iron/src/lib.rs b/askama_iron/src/lib.rs new file mode 100644 index 0000000..37e79cc --- /dev/null +++ b/askama_iron/src/lib.rs @@ -0,0 +1,3 @@ +pub use askama::*; + +pub use iron::{headers::ContentType, modifier::Modifier, Response}; diff --git a/askama_iron/templates/hello.html b/askama_iron/templates/hello.html new file mode 100644 index 0000000..8149be7 --- /dev/null +++ b/askama_iron/templates/hello.html @@ -0,0 +1 @@ +Hello, {{ name }}! diff --git a/askama_iron/templates/hello.txt b/askama_iron/templates/hello.txt new file mode 100644 index 0000000..8149be7 --- /dev/null +++ b/askama_iron/templates/hello.txt @@ -0,0 +1 @@ +Hello, {{ name }}! diff --git a/askama_iron/tests/basic.rs b/askama_iron/tests/basic.rs new file mode 100644 index 0000000..32ca839 --- /dev/null +++ b/askama_iron/tests/basic.rs @@ -0,0 +1,36 @@ +use askama::Template; +use iron::{status, Response}; + +#[derive(Template)] +#[template(path = "hello.html")] +struct HelloTemplate<'a> { + name: &'a str, +} + +#[derive(Template)] +#[template(path = "hello.txt")] +struct HelloTextTemplate<'a> { + name: &'a str, +} + +#[test] +fn test_iron() { + let rsp = Response::with((status::Ok, HelloTemplate { name: "world" })); + let mut buf = Vec::new(); + let _ = rsp.body.unwrap().write_body(&mut buf); + assert_eq!(buf, b"Hello, world!"); + + let content_type = rsp.headers.get::().unwrap(); + assert_eq!(format!("{}", content_type), "text/html; charset=utf-8"); +} + +#[test] +fn test_iron_non_html() { + let rsp = Response::with((status::Ok, HelloTextTemplate { name: "world" })); + let mut buf = Vec::new(); + let _ = rsp.body.unwrap().write_body(&mut buf); + assert_eq!(buf, b"Hello, world!"); + + let content_type = rsp.headers.get::(); + assert_eq!(content_type, None); +} diff --git a/testing/Cargo.toml b/testing/Cargo.toml index 5517047..ff2b137 100644 --- a/testing/Cargo.toml +++ b/testing/Cargo.toml @@ -6,14 +6,10 @@ workspace = ".." edition = "2018" [features] -default = [] -full = ["with-iron", "serde-json"] -serde-json = ["serde_json", "askama/serde-json"] -with-iron = ["iron", "askama/with-iron"] +default = ["serde_json", "askama/serde-json"] [dependencies] askama = { path = "../askama", version = "*" } -iron = { version = "0.6", optional = true } serde_json = { version = "1.0", optional = true } [dev-dependencies] diff --git a/testing/templates/hello.txt b/testing/templates/hello.txt deleted file mode 100644 index 8149be7..0000000 --- a/testing/templates/hello.txt +++ /dev/null @@ -1 +0,0 @@ -Hello, {{ name }}! diff --git a/testing/tests/iron.rs b/testing/tests/iron.rs deleted file mode 100644 index f58a609..0000000 --- a/testing/tests/iron.rs +++ /dev/null @@ -1,37 +0,0 @@ -#![cfg(feature = "iron")] -use askama::Template; -use iron::{status, Response}; - -#[derive(Template)] -#[template(path = "hello.html")] -struct HelloTemplate<'a> { - name: &'a str, -} - -#[derive(Template)] -#[template(path = "hello.txt")] -struct HelloTextTemplate<'a> { - name: &'a str, -} - -#[test] -fn test_iron() { - let rsp = Response::with((status::Ok, HelloTemplate { name: "world" })); - let mut buf = Vec::new(); - let _ = rsp.body.unwrap().write_body(&mut buf); - assert_eq!(buf, b"Hello, world!"); - - let content_type = rsp.headers.get::().unwrap(); - assert_eq!(format!("{}", content_type), "text/html; charset=utf-8"); -} - -#[test] -fn test_iron_non_html() { - let rsp = Response::with((status::Ok, HelloTextTemplate { name: "world" })); - let mut buf = Vec::new(); - let _ = rsp.body.unwrap().write_body(&mut buf); - assert_eq!(buf, b"Hello, world!"); - - let content_type = rsp.headers.get::(); - assert_eq!(content_type, None); -} -- cgit