Skip to content

Commit

Permalink
feat: Add support for web framework poem@3
Browse files Browse the repository at this point in the history
Support is gated behind the poem-3 feature.
  • Loading branch information
glennib committed Jan 15, 2025
1 parent 99ff3de commit 89b8a7b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
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 behind the `poem-3` feature.

## [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-3" feature:

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

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
}
```
2 changes: 2 additions & 0 deletions maud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ default = []
# Web framework integrations
actix-web = ["actix-web-dep", "futures-util"]
axum = ["axum-core", "http"]
poem-3 = ["poem"]

[dependencies]
maud_macros = { version = "0.26.0", path = "../maud_macros" }
Expand All @@ -30,6 +31,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
15 changes: 15 additions & 0 deletions maud/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,21 @@ mod tide_support {
}
}

#[cfg(feature = "poem-3")]
mod poem_3_support {
use crate::PreEscaped;
use alloc::string::String;
use poem::web::Html;
use poem::IntoResponse;
use poem::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

0 comments on commit 89b8a7b

Please sign in to comment.