Skip to content

Commit 69b753e

Browse files
Add support for Salvo (#476)
* feat: add salvo support * fix: add http dep for salvo * fix: remove micro optimisation Co-authored-by: Chris Wong <lambda.fairy@gmail.com> --------- Co-authored-by: Chris Wong <lambda.fairy@gmail.com>
1 parent ac17637 commit 69b753e

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

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], [Axum] and [Poem].
3+
Maud includes support for these web frameworks: [Actix], [Rocket], [Rouille], [Tide], [Axum], [Poem], and [Salvo].
44

55
[Actix]: https://actix.rs/
66
[Rocket]: https://rocket.rs/
@@ -10,6 +10,7 @@ Maud includes support for these web frameworks: [Actix], [Rocket], [Rouille], [T
1010
[Warp]: https://seanmonstar.com/blog/warp/
1111
[Submillisecond]: https://github.com/lunatic-solutions/submillisecond
1212
[Poem]: https://github.com/poem-web/poem
13+
[Salvo]: https://salvo.rs
1314

1415
# Actix
1516

@@ -268,3 +269,38 @@ async fn main() -> Result<(), std::io::Error> {
268269
.await
269270
}
270271
```
272+
273+
# Salvo
274+
275+
Salvo support is available with the "salvo" feature:
276+
277+
```toml
278+
# ...
279+
[dependencies]
280+
maud = { version = "*", features = ["salvo"] }
281+
# ...
282+
```
283+
284+
This adds an implementation of `Scribe` for `Markup`/`PreEscaped<String>`.
285+
This then allows you to use it directly as a response!
286+
287+
```rust,no_run
288+
use maud::{html, Markup};
289+
use salvo::prelude::*;
290+
291+
#[handler]
292+
async fn hello_world(res: &mut Response) {
293+
res.render(html! {
294+
h1 { "Hello, World!" }
295+
});
296+
}
297+
298+
#[tokio::main]
299+
async fn main() {
300+
let app = Router::with_path("/").get(hello_world);
301+
302+
let listener = TcpListener::new("0.0.0.0:3000").bind().await;
303+
304+
Server::new(listener).serve(app).await;
305+
}
306+
```

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", "poem"] }
10+
maud = { path = "../maud", features = ["actix-web", "rocket", "tide", "axum", "warp", "submillisecond", "poem", "salvo"] }
1111
pulldown-cmark = "0.8"
1212
rocket = "0.5"
1313
rouille = "3"
@@ -17,6 +17,7 @@ tokio = { version = "1.9.0", features = ["rt", "macros", "rt-multi-thread"] }
1717
axum = "0.8"
1818
warp = "0.3.6"
1919
poem = "3"
20+
salvo = "0.78.0"
2021

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

maud/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ default = []
2020
# Web framework integrations
2121
actix-web = ["actix-web-dep", "futures-util"]
2222
axum = ["axum-core", "http"]
23+
salvo = ["salvo_core", "http"]
2324

2425
[dependencies]
2526
maud_macros = { version = "0.27.0", path = "../maud_macros" }
@@ -33,6 +34,7 @@ submillisecond = { version = "0.4.1", optional = true }
3334
http = { version = "1", optional = true }
3435
warp = { version = "0.3.6", optional = true }
3536
poem = { version = "3", optional = true }
37+
salvo_core = { version = "0.78.0", optional = true }
3638

3739
[dev-dependencies]
3840
trybuild = { version = "1.0.33", features = ["diff"] }

maud/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,24 @@ mod submillisecond_support {
424424
}
425425
}
426426

427+
#[cfg(feature = "salvo")]
428+
mod salvo_support {
429+
use crate::PreEscaped;
430+
use alloc::string::String;
431+
use http::{header::CONTENT_TYPE, HeaderValue};
432+
use salvo_core::{http::Response, writing::Scribe};
433+
434+
impl Scribe for PreEscaped<String> {
435+
fn render(self, res: &mut Response) {
436+
let _ = res.headers.insert(
437+
CONTENT_TYPE,
438+
HeaderValue::from_static("text/html; charset=utf-8"),
439+
);
440+
let _ = res.write_body(self.into_string());
441+
}
442+
}
443+
}
444+
427445
#[doc(hidden)]
428446
pub mod macro_private {
429447
use crate::{display, Render};

0 commit comments

Comments
 (0)