Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tide support #280

Merged
merged 9 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No action needed here, but it would be nice to standardize on an import style at some point.

Filed #281 for this.


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()
}
}
}