From d3b048544eebf7b8402fb585b4e2531de9b28a33 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Wed, 24 Jan 2024 20:23:18 +0000 Subject: Add poem integration --- askama_poem/Cargo.toml | 32 ++++++++++++++++++++++++++++++++ askama_poem/LICENSE-APACHE | 1 + askama_poem/LICENSE-MIT | 1 + askama_poem/README.md | 9 +++++++++ askama_poem/src/lib.rs | 17 +++++++++++++++++ askama_poem/templates/hello.html | 1 + askama_poem/tests/basic.rs | 23 +++++++++++++++++++++++ 7 files changed, 84 insertions(+) create mode 100644 askama_poem/Cargo.toml create mode 120000 askama_poem/LICENSE-APACHE create mode 120000 askama_poem/LICENSE-MIT create mode 100644 askama_poem/README.md create mode 100644 askama_poem/src/lib.rs create mode 100644 askama_poem/templates/hello.html create mode 100644 askama_poem/tests/basic.rs (limited to 'askama_poem') diff --git a/askama_poem/Cargo.toml b/askama_poem/Cargo.toml new file mode 100644 index 0000000..20c720b --- /dev/null +++ b/askama_poem/Cargo.toml @@ -0,0 +1,32 @@ +[package] +name = "askama_poem" +version = "0.14.0" +description = "Poem 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 = ".." +readme = "README.md" +edition = "2021" +rust-version = "1.65" + +[dependencies] +poem = { version = "2.0.0", default-features = false } +askama = { version = "0.12", path = "../askama", default-features = false, features = ["with-poem"] } + +[dev-dependencies] +poem = { version = "2.0.0", features = ["test"] } +tokio = "1.0" + +[features] +default = ["askama/default"] +config = ["askama/config"] +humansize = ["askama/humansize"] +markdown = ["askama/markdown"] +num-traits = ["askama/num-traits"] +serde-json = ["askama/serde-json"] +serde-yaml = ["askama/serde-yaml"] +urlencode = ["askama/urlencode"] diff --git a/askama_poem/LICENSE-APACHE b/askama_poem/LICENSE-APACHE new file mode 120000 index 0000000..965b606 --- /dev/null +++ b/askama_poem/LICENSE-APACHE @@ -0,0 +1 @@ +../LICENSE-APACHE \ No newline at end of file diff --git a/askama_poem/LICENSE-MIT b/askama_poem/LICENSE-MIT new file mode 120000 index 0000000..76219eb --- /dev/null +++ b/askama_poem/LICENSE-MIT @@ -0,0 +1 @@ +../LICENSE-MIT \ No newline at end of file diff --git a/askama_poem/README.md b/askama_poem/README.md new file mode 100644 index 0000000..0aecdea --- /dev/null +++ b/askama_poem/README.md @@ -0,0 +1,9 @@ +# askama_poem: Askama integration with Poem + +[![Documentation](https://docs.rs/askama_poem/badge.svg)](https://docs.rs/askama_poem/) +[![Latest version](https://img.shields.io/crates/v/askama_poem.svg)](https://crates.io/crates/askama_poem) +[![Build Status](https://github.com/djc/askama/workflows/CI/badge.svg)](https://github.com/djc/askama/actions?query=workflow%3ACI) +[![Chat](https://img.shields.io/discord/976380008299917365?logo=discord)](https://discord.gg/ZucwjE6bmT) + +Integration of the [Askama](https://github.com/djc/askama) templating engine in +code building on the poem web framework. diff --git a/askama_poem/src/lib.rs b/askama_poem/src/lib.rs new file mode 100644 index 0000000..de69e09 --- /dev/null +++ b/askama_poem/src/lib.rs @@ -0,0 +1,17 @@ +#![forbid(unsafe_code)] +#![deny(elided_lifetimes_in_paths)] +#![deny(unreachable_pub)] + +pub use askama::*; +use poem::http::StatusCode; +pub use poem::{web::IntoResponse, Response}; + +pub fn into_response(t: &T) -> Response { + match t.render() { + Ok(body) => Response::builder() + .status(StatusCode::OK) + .content_type(T::MIME_TYPE) + .body(body), + Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into(), + } +} diff --git a/askama_poem/templates/hello.html b/askama_poem/templates/hello.html new file mode 100644 index 0000000..8149be7 --- /dev/null +++ b/askama_poem/templates/hello.html @@ -0,0 +1 @@ +Hello, {{ name }}! diff --git a/askama_poem/tests/basic.rs b/askama_poem/tests/basic.rs new file mode 100644 index 0000000..406c45f --- /dev/null +++ b/askama_poem/tests/basic.rs @@ -0,0 +1,23 @@ +use askama::Template; +use poem::{handler, test::TestClient, Route}; + +#[derive(Template)] +#[template(path = "hello.html")] +struct HelloTemplate<'a> { + name: &'a str, +} + +#[handler] +fn hello() -> HelloTemplate<'static> { + HelloTemplate { name: "world" } +} + +#[tokio::test] +async fn test_poem() { + let app = Route::new().at("/", hello); + let cli = TestClient::new(app); + + let resp = cli.get("/").send().await; + resp.assert_status_is_ok(); + resp.assert_text("Hello, world!").await; +} -- cgit