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

feat: Add support for web framework poem@3 #466

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

- Support `axum` v0.8 through `axum-core` v0.5
- Add support for `poem` version 3.

## [0.26.0] - 2024-01-15

Expand Down
38 changes: 37 additions & 1 deletion docs/content/web-frameworks.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Web framework integration

Maud includes support for these web frameworks: [Actix], [Rocket], [Rouille], [Tide] and [Axum].
Maud includes support for these web frameworks: [Actix], [Rocket], [Rouille], [Tide], [Axum] and [Poem].

[Actix]: https://actix.rs/
[Rocket]: https://rocket.rs/
Expand All @@ -9,6 +9,7 @@ Maud includes support for these web frameworks: [Actix], [Rocket], [Rouille], [T
[Axum]: https://docs.rs/axum/
[Warp]: https://seanmonstar.com/blog/warp/
[Submillisecond]: https://github.com/lunatic-solutions/submillisecond
[Poem]: https://github.com/poem-web/poem

# Actix

Expand Down Expand Up @@ -232,3 +233,38 @@ fn helloworld() -> Markup {
}
}
```

# Poem

Poem support is available with the "poem" feature:

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

This adds an implementation of `poem::IntoResponse` for `Markup`/`PreEscaped<String>`.
This then allows you to use it directly as a response!

```rust,no_run
use maud::{html, Markup};
use poem::{get, handler, listener::TcpListener, Route, Server};

#[handler]
fn hello_world() -> Markup {
html! {
h1 { "Hello, World!" }
}
}

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let app = Route::new().at("/hello", get(hello_world));
Server::new(TcpListener::bind("0.0.0.0:3000"))
.name("hello-world")
.run(app)
.await
}
```
3 changes: 2 additions & 1 deletion doctest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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", "warp", "submillisecond"] }
maud = { path = "../maud", features = ["actix-web", "rocket", "tide", "axum", "warp", "submillisecond", "poem"] }
pulldown-cmark = "0.8"
rocket = "0.5"
rouille = "3"
Expand All @@ -16,6 +16,7 @@ tide = "0.16"
tokio = { version = "1.9.0", features = ["rt", "macros", "rt-multi-thread"] }
axum = "0.8"
warp = "0.3.6"
poem = "3"

[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 @@ -30,6 +30,7 @@ axum-core = { version = "0.5", optional = true }
submillisecond = { version = "0.4.1", optional = true }
http = { version = "1", optional = true }
warp = { version = "0.3.6", optional = true }
poem = { version = "3", 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 @@ -359,6 +359,19 @@ mod tide_support {
}
}

#[cfg(feature = "poem")]
mod poem_support {
use crate::PreEscaped;
use alloc::string::String;
use poem::{web::Html, IntoResponse, Response};

impl IntoResponse for PreEscaped<String> {
fn into_response(self) -> Response {
Html(self.into_string()).into_response()
}
}
}

#[cfg(feature = "axum")]
mod axum_support {
use crate::PreEscaped;
Expand Down
Loading