Skip to content

Commit

Permalink
Warp Integration: Yet another web framework support (#404)
Browse files Browse the repository at this point in the history
* Add warp integration.

* Add warp to docs

* Update changelog

* Please the mighty rustfmt \o/

* Enable warp feature in doctest

---------

Co-authored-by: Chris Wong <lambda.fairy@gmail.com>
  • Loading branch information
thatguydoru and lambda-fairy authored Jan 4, 2024
1 parent b3a98c9 commit 320add8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
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 }
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

0 comments on commit 320add8

Please sign in to comment.