From da6aba6ad99da3e8aeb9a493ba3f0a86cb9655be Mon Sep 17 00:00:00 2001 From: Max Countryman Date: Sat, 31 Jul 2021 14:19:42 -0700 Subject: [PATCH 1/9] provide tide support This introduces support for Tide by implementing `From` over markup. Unlike other frameworks, Tide does not seem to provide e.g. a trait which could instead be implemented. However as demonstrated, this alternative results in a simple, ergonomic interface. Consumers may leverage Tide support by using the "tide" feature. --- docs/content/web-frameworks.md | 34 ++++++++++++++++++++++++++++++++++ maud/Cargo.toml | 1 + maud/src/lib.rs | 16 ++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/docs/content/web-frameworks.md b/docs/content/web-frameworks.md index 916b4fc3..e2056eb9 100644 --- a/docs/content/web-frameworks.md +++ b/docs/content/web-frameworks.md @@ -142,3 +142,37 @@ fn main() { }); } ``` + +# Tide + +Tide support is available with the "tide" feature: + +```toml +# ... +[dependencies] +maud = { version = "*", features = ["tide"] } +# ... +``` + +This adds an implementation of `From>` for the `Response` struct. +Once provided, callers may return results of `html!` directly as responses: + +```rust,no_run +use maud::html; +use tide::Request; +use tide::prelude::*; + +#[async_std::main] +async fn main() -> tide::Result<()> { + let mut app = tide::new(); + app.at("/hello/:name").get(|req: Request<()>| async { + let name: String = req.param("name")?.parse()?; + Ok(html! { + h1 { "Hello, " (name) "!" } + p { "Nice to meet you!" } + }) + }); + app.listen("127.0.0.1:8080").await?; + Ok(()) +} +``` diff --git a/maud/Cargo.toml b/maud/Cargo.toml index 3fa34fa0..eb04fb54 100644 --- a/maud/Cargo.toml +++ b/maud/Cargo.toml @@ -24,6 +24,7 @@ iron = { version = ">= 0.5.1, < 0.7.0", optional = true } rocket = { version = ">= 0.3, < 0.5", optional = true } futures-util = { version = "0.3.0", optional = true, default-features = false } actix-web-dep = { package = "actix-web", version = ">= 2, < 4", optional = true, default-features = false } +tide = { version = "0.16.0", optional = true } [dev-dependencies] trybuild = { version = "1.0.33", features = ["diff"] } diff --git a/maud/src/lib.rs b/maud/src/lib.rs index a2f14995..aa947caa 100644 --- a/maud/src/lib.rs +++ b/maud/src/lib.rs @@ -237,3 +237,19 @@ mod actix_support { } } } + +#[cfg(feature = "tide")] +mod tide_support { + use crate::PreEscaped; + use alloc::{format, string::String}; + use tide::{http::mime, Response, StatusCode}; + + impl From> for Response { + fn from(markup: PreEscaped) -> Response { + Response::builder(StatusCode::Ok) + .body(format!("{}", markup.0)) + .content_type(mime::HTML) + .build() + } + } +} From 1940e54d1bbcdfb61d32b5ab487b035765cc6514 Mon Sep 17 00:00:00 2001 From: Max Countryman Date: Sat, 31 Jul 2021 14:32:05 -0700 Subject: [PATCH 2/9] additional doc updates --- docs/content/web-frameworks.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/content/web-frameworks.md b/docs/content/web-frameworks.md index e2056eb9..c4990c6c 100644 --- a/docs/content/web-frameworks.md +++ b/docs/content/web-frameworks.md @@ -1,12 +1,13 @@ # Web framework integration Maud includes support for these web frameworks: -[Actix], [Iron], [Rocket], and [Rouille]. +[Actix], [Iron], [Rocket], [Rouille], and [Tide]. [Actix]: https://actix.rs/ [Iron]: http://ironframework.io [Rocket]: https://rocket.rs/ [Rouille]: https://github.com/tomaka/rouille +[Tide]: https://docs.rs/tide/ # Actix From ec0fa2281c8ad63fa9cef7b217c2e6780658872e Mon Sep 17 00:00:00 2001 From: Chris Wong Date: Sun, 1 Aug 2021 22:53:14 +1000 Subject: [PATCH 3/9] Add tide to doctest package --- doctest/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/doctest/Cargo.toml b/doctest/Cargo.toml index 078fbe71..4d55d52c 100644 --- a/doctest/Cargo.toml +++ b/doctest/Cargo.toml @@ -12,6 +12,7 @@ maud = { path = "../maud", features = ["actix-web", "iron", "rocket"] } pulldown-cmark = "0.8" rocket = "0.4" rouille = "3" +tide = "0.16" [lib] path = "lib.rs" From 634b93aa59f97e196798452d8985317d329a1b8f Mon Sep 17 00:00:00 2001 From: Chris Wong Date: Sun, 1 Aug 2021 23:40:36 +1000 Subject: [PATCH 4/9] Add async-std and tide feature to doctest package --- doctest/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doctest/Cargo.toml b/doctest/Cargo.toml index 4d55d52c..f27a0a45 100644 --- a/doctest/Cargo.toml +++ b/doctest/Cargo.toml @@ -7,8 +7,9 @@ edition = "2018" [dependencies] actix-web = "3" ammonia = "3" +async-std = "1" iron = "0.6" -maud = { path = "../maud", features = ["actix-web", "iron", "rocket"] } +maud = { path = "../maud", features = ["actix-web", "iron", "rocket", "tide"] } pulldown-cmark = "0.8" rocket = "0.4" rouille = "3" From 33aef5f24bf98e33dad5fc3a9e9b482654cd8f94 Mon Sep 17 00:00:00 2001 From: Max Countryman Date: Sun, 1 Aug 2021 07:53:50 -0700 Subject: [PATCH 5/9] fix async block ownership --- docs/content/web-frameworks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/web-frameworks.md b/docs/content/web-frameworks.md index c4990c6c..0eb52d0a 100644 --- a/docs/content/web-frameworks.md +++ b/docs/content/web-frameworks.md @@ -166,7 +166,7 @@ use tide::prelude::*; #[async_std::main] async fn main() -> tide::Result<()> { let mut app = tide::new(); - app.at("/hello/:name").get(|req: Request<()>| async { + app.at("/hello/:name").get(|req: Request<()>| async move { let name: String = req.param("name")?.parse()?; Ok(html! { h1 { "Hello, " (name) "!" } From 6b4bcbd49675c2916e3778e476188221f87b5579 Mon Sep 17 00:00:00 2001 From: Max Countryman Date: Sun, 1 Aug 2021 08:00:19 -0700 Subject: [PATCH 6/9] provide async_std attributes feature --- doctest/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doctest/Cargo.toml b/doctest/Cargo.toml index f27a0a45..1c960609 100644 --- a/doctest/Cargo.toml +++ b/doctest/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] actix-web = "3" ammonia = "3" -async-std = "1" +async-std = {version = "1.9.0", features = ["attributes"]} iron = "0.6" maud = { path = "../maud", features = ["actix-web", "iron", "rocket", "tide"] } pulldown-cmark = "0.8" From 4766c4d79aa7c497b936a544598f514658f38b9c Mon Sep 17 00:00:00 2001 From: Max Countryman Date: Wed, 4 Aug 2021 07:49:19 -0700 Subject: [PATCH 7/9] prefer into_string --- maud/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maud/src/lib.rs b/maud/src/lib.rs index aa947caa..96d7c9da 100644 --- a/maud/src/lib.rs +++ b/maud/src/lib.rs @@ -241,13 +241,13 @@ mod actix_support { #[cfg(feature = "tide")] mod tide_support { use crate::PreEscaped; - use alloc::{format, string::String}; + use alloc::string::String; use tide::{http::mime, Response, StatusCode}; impl From> for Response { fn from(markup: PreEscaped) -> Response { Response::builder(StatusCode::Ok) - .body(format!("{}", markup.0)) + .body(markup.into_string()) .content_type(mime::HTML) .build() } From ca7e9d421a6d2e73781bfd7dba4a6ea0dd0a1555 Mon Sep 17 00:00:00 2001 From: Max Countryman Date: Wed, 4 Aug 2021 07:52:20 -0700 Subject: [PATCH 8/9] breakout async-std for formatting purposes --- doctest/Cargo.toml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doctest/Cargo.toml b/doctest/Cargo.toml index 1c960609..25d575ff 100644 --- a/doctest/Cargo.toml +++ b/doctest/Cargo.toml @@ -7,7 +7,6 @@ edition = "2018" [dependencies] actix-web = "3" ammonia = "3" -async-std = {version = "1.9.0", features = ["attributes"]} iron = "0.6" maud = { path = "../maud", features = ["actix-web", "iron", "rocket", "tide"] } pulldown-cmark = "0.8" @@ -15,5 +14,9 @@ rocket = "0.4" rouille = "3" tide = "0.16" +[dependencies.async-std] +version = "1.9.0" +features = ["attributes"] + [lib] path = "lib.rs" From cffd9b706f23620504331315cdc457a5feb4d0aa Mon Sep 17 00:00:00 2001 From: Max Countryman Date: Wed, 4 Aug 2021 07:58:09 -0700 Subject: [PATCH 9/9] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8106746..9364c499 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - Support `no_std` + `alloc`. [#278](https://github.com/lambda-fairy/maud/issues/278) +- Provide Tide support. + [#280](https://github.com/lambda-fairy/maud/pull/280) ## [0.22.2] - 2021-01-09