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.

Co-authored-by: Chris Wong <lambda.fairy@gmail.com>
This commit is contained in:
Max Countryman 2021-08-05 21:22:05 -07:00 committed by GitHub
parent 3eb4254ffb
commit ba9c7b523c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 2 deletions

View file

@ -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

View file

@ -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
@ -142,3 +143,37 @@ fn main() {
});
}
```
# Tide
Tide support is available with the "tide" feature:
```toml
# ...
[dependencies]
maud = { version = "*", features = ["tide"] }
# ...
```
This adds an implementation of `From<PreEscaped<String>>` 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 move {
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(())
}
```

View file

@ -8,10 +8,15 @@ edition = "2018"
actix-web = "3"
ammonia = "3"
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"
tide = "0.16"
[dependencies.async-std]
version = "1.9.0"
features = ["attributes"]
[lib]
path = "lib.rs"

View file

@ -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"] }

View file

@ -237,3 +237,19 @@ mod actix_support {
}
}
}
#[cfg(feature = "tide")]
mod tide_support {
use crate::PreEscaped;
use alloc::string::String;
use tide::{http::mime, Response, StatusCode};
impl From<PreEscaped<String>> for Response {
fn from(markup: PreEscaped<String>) -> Response {
Response::builder(StatusCode::Ok)
.body(markup.into_string())
.content_type(mime::HTML)
.build()
}
}
}