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

Warp Integration: Yet another web framework support #404

Merged
merged 6 commits into from
Jan 4, 2024
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 @@ -10,6 +10,8 @@
[#396](https://github.com/lambda-fairy/maud/pull/396)
- Support `axum` v0.7 through `axum-core` v0.4 and `http` v1
[#401](https://github.com/lambda-fairy/maud/pull/401)
- Add support for `warp` v0.3.6
[#404](https://github.com/lambda-fairy/maud/pull/404)
- Support `rocket` v0.5
[#406](https://github.com/lambda-fairy/maud/pull/406)

Expand Down
26 changes: 26 additions & 0 deletions docs/content/web-frameworks.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Maud includes support for these web frameworks: [Actix], [Rocket], [Rouille], [T
[Rouille]: https://github.com/tomaka/rouille
[Tide]: https://docs.rs/tide/
[Axum]: https://docs.rs/axum/
[Warp]: https://seanmonstar.com/blog/warp/

# Actix

Expand Down Expand Up @@ -171,3 +172,28 @@ async fn main() {
axum::serve(listener, app.into_make_service()).await.unwrap();
}
```

# Warp

Warp support is available with the "warp" feature:

```toml
# ...
[dependencies]
maud = { version = "*", features = ["warp"] }
# ...
```

This enables `Markup` to be of type `warp::Reply`, making it possible to return it
immediately from a handler.

```rust,no_run
use maud::html;
use warp::Filter;

#[tokio::main]
async fn main() {
let hello = warp::any().map(|| html! { h1 { "Hello, world!" } });
warp::serve(hello).run(([127, 0, 0, 1], 8000)).await;
}
```
3 changes: 2 additions & 1 deletion doctest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ edition = "2021"
[dependencies]
actix-web = { version = "4.0.0-rc.2", default-features = false, features = ["macros"] }
ammonia = "3"
maud = { path = "../maud", features = ["actix-web", "rocket", "tide", "axum"] }
maud = { path = "../maud", features = ["actix-web", "rocket", "tide", "axum", "warp"] }
pulldown-cmark = "0.8"
rocket = "0.5"
rouille = "3"
tide = "0.16"
tokio = { version = "1.9.0", features = ["rt", "macros", "rt-multi-thread"] }
axum = "0.7"
warp = "0.3.6"

[dependencies.async-std]
version = "1.9.0"
Expand Down
1 change: 1 addition & 0 deletions maud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ actix-web-dep = { package = "actix-web", version = "4", optional = true, default
tide = { version = "0.16.0", optional = true, default-features = false }
axum-core = { version = "0.4", optional = true }
http = { version = "1", optional = true }
Copy link
Owner

Choose a reason for hiding this comment

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

Hmm, looks like Axum uses http 1.0, but Warp uses http 0.2. But since the Warp integration doesn't use the http dependency directly, it still works out? Still something to look out for in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted, I'll have a close eye on it. 🙇

warp = { version = "0.3.6", optional = true }

[dev-dependencies]
trybuild = { version = "1.0.33", features = ["diff"] }
Expand Down
13 changes: 13 additions & 0 deletions maud/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,19 @@ mod axum_support {
}
}

#[cfg(feature = "warp")]
mod warp_support {
use crate::PreEscaped;
use alloc::string::String;
use warp::reply::{self, Reply, Response};

impl Reply for PreEscaped<String> {
fn into_response(self) -> Response {
reply::html(self.into_string()).into_response()
}
}
}

#[doc(hidden)]
pub mod macro_private {
use crate::{display, Render};
Expand Down
Loading