From 91c2bbf80468c12d0663330cff185f1d7680c91e Mon Sep 17 00:00:00 2001 From: Bjørn Madsen Date: Wed, 29 Jan 2020 09:32:39 +0100 Subject: Add support for warp --- askama_warp/Cargo.toml | 21 +++++++++++++++++++++ askama_warp/src/lib.rs | 22 ++++++++++++++++++++++ askama_warp/templates/hello.html | 1 + askama_warp/tests/warp.rs | 18 ++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 askama_warp/Cargo.toml create mode 100644 askama_warp/src/lib.rs create mode 100644 askama_warp/templates/hello.html create mode 100644 askama_warp/tests/warp.rs (limited to 'askama_warp') diff --git a/askama_warp/Cargo.toml b/askama_warp/Cargo.toml new file mode 100644 index 0000000..557ab46 --- /dev/null +++ b/askama_warp/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "askama_warp" +version = "0.9.0" +authors = ["Dirkjan Ochtman ", "Bjørn Madsen "] +description = "Warp 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-warp", "mime", "mime_guess"] } +warp = "0.2" + +[dev-dependencies] +tokio-test = "0.2" +tokio = { version = "0.2", features = ["macros"] } diff --git a/askama_warp/src/lib.rs b/askama_warp/src/lib.rs new file mode 100644 index 0000000..e721ab4 --- /dev/null +++ b/askama_warp/src/lib.rs @@ -0,0 +1,22 @@ +pub use askama::*; +pub use warp; + +use warp::http::{self, header, StatusCode}; +use warp::hyper::Body; +use warp::reply::Response; + +pub fn reply(t: &T, ext: &str) -> Response { + match t.render() { + Ok(body) => http::Response::builder() + .status(StatusCode::OK) + .header( + header::CONTENT_TYPE, + mime::extension_to_mime_type(ext).to_string(), + ) + .body(body.into()), + Err(_) => http::Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(Body::empty()), + } + .unwrap() +} diff --git a/askama_warp/templates/hello.html b/askama_warp/templates/hello.html new file mode 100644 index 0000000..8149be7 --- /dev/null +++ b/askama_warp/templates/hello.html @@ -0,0 +1 @@ +Hello, {{ name }}! diff --git a/askama_warp/tests/warp.rs b/askama_warp/tests/warp.rs new file mode 100644 index 0000000..69a5f23 --- /dev/null +++ b/askama_warp/tests/warp.rs @@ -0,0 +1,18 @@ +use askama::Template; +use warp::Filter; + +#[derive(Template)] +#[template(path = "hello.html")] +struct HelloTemplate<'a> { + name: &'a str, +} + +#[tokio::test] +async fn test_warp() { + let filter = warp::get().map(|| HelloTemplate { name: "world" }); + + let res = warp::test::request().reply(&filter).await; + + assert_eq!(res.status(), 200); + assert_eq!(res.body(), "Hello, world!"); +} -- cgit