Skip to content

Commit 09b97ad

Browse files
authored
feat: Add support for web framework poem@3 (#466)
* feat: Add support for web framework poem@3 Support is gated behind the poem-3 feature. * style: run cargo +nightly fmt * test: Add test support in doctest * fix: Change feature name from poem-3 to poem
1 parent 99ff3de commit 09b97ad

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44

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

78
## [0.26.0] - 2024-01-15
89

docs/content/web-frameworks.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Web framework integration
22

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

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

1314
# Actix
1415

@@ -232,3 +233,38 @@ fn helloworld() -> Markup {
232233
}
233234
}
234235
```
236+
237+
# Poem
238+
239+
Poem support is available with the "poem" feature:
240+
241+
```toml
242+
# ...
243+
[dependencies]
244+
maud = { version = "*", features = ["poem"] }
245+
# ...
246+
```
247+
248+
This adds an implementation of `poem::IntoResponse` for `Markup`/`PreEscaped<String>`.
249+
This then allows you to use it directly as a response!
250+
251+
```rust,no_run
252+
use maud::{html, Markup};
253+
use poem::{get, handler, listener::TcpListener, Route, Server};
254+
255+
#[handler]
256+
fn hello_world() -> Markup {
257+
html! {
258+
h1 { "Hello, World!" }
259+
}
260+
}
261+
262+
#[tokio::main]
263+
async fn main() -> Result<(), std::io::Error> {
264+
let app = Route::new().at("/hello", get(hello_world));
265+
Server::new(TcpListener::bind("0.0.0.0:3000"))
266+
.name("hello-world")
267+
.run(app)
268+
.await
269+
}
270+
```

doctest/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77
[dependencies]
88
actix-web = { version = "4.0.0-rc.2", default-features = false, features = ["macros"] }
99
ammonia = "3"
10-
maud = { path = "../maud", features = ["actix-web", "rocket", "tide", "axum", "warp", "submillisecond"] }
10+
maud = { path = "../maud", features = ["actix-web", "rocket", "tide", "axum", "warp", "submillisecond", "poem"] }
1111
pulldown-cmark = "0.8"
1212
rocket = "0.5"
1313
rouille = "3"
@@ -16,6 +16,7 @@ tide = "0.16"
1616
tokio = { version = "1.9.0", features = ["rt", "macros", "rt-multi-thread"] }
1717
axum = "0.8"
1818
warp = "0.3.6"
19+
poem = "3"
1920

2021
[dependencies.async-std]
2122
version = "1.9.0"

maud/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ axum-core = { version = "0.5", optional = true }
3030
submillisecond = { version = "0.4.1", optional = true }
3131
http = { version = "1", optional = true }
3232
warp = { version = "0.3.6", optional = true }
33+
poem = { version = "3", optional = true }
3334

3435
[dev-dependencies]
3536
trybuild = { version = "1.0.33", features = ["diff"] }

maud/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,19 @@ mod tide_support {
359359
}
360360
}
361361

362+
#[cfg(feature = "poem")]
363+
mod poem_support {
364+
use crate::PreEscaped;
365+
use alloc::string::String;
366+
use poem::{web::Html, IntoResponse, Response};
367+
368+
impl IntoResponse for PreEscaped<String> {
369+
fn into_response(self) -> Response {
370+
Html(self.into_string()).into_response()
371+
}
372+
}
373+
}
374+
362375
#[cfg(feature = "axum")]
363376
mod axum_support {
364377
use crate::PreEscaped;

0 commit comments

Comments
 (0)