Skip to content

Commit

Permalink
Tide support (#280)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
maxcountryman and lambda-fairy authored Aug 6, 2021
1 parent 3eb4254 commit ba9c7b5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 36 additions & 1 deletion docs/content/web-frameworks.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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(())
}
```
7 changes: 6 additions & 1 deletion doctest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions maud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
16 changes: 16 additions & 0 deletions maud/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}

0 comments on commit ba9c7b5

Please sign in to comment.