diff options
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/windmark.rs | 4 | ||||
| -rw-r--r-- | src/response/macros.rs | 65 |
3 files changed, 53 insertions, 18 deletions
@@ -38,3 +38,5 @@ regex = "1.5.5" matchit = "0.6.0" tree_magic = { version = "0.2.3", optional = true } # MIME + +paste = "1.0.12" # Token Pasting diff --git a/examples/windmark.rs b/examples/windmark.rs index 9e23d6e..7c46d9d 100644 --- a/examples/windmark.rs +++ b/examples/windmark.rs @@ -214,6 +214,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { router.mount("/async-nothing", |_| { async { Response::success("This is an async route.") } }); + router.mount( + "/async-macro", + windmark::success_async!(async { "hi" }.await), + ); router.run().await } diff --git a/src/response/macros.rs b/src/response/macros.rs index 339da07..76bc950 100644 --- a/src/response/macros.rs +++ b/src/response/macros.rs @@ -16,7 +16,7 @@ // Copyright (C) 2022-2022 Fuwn <[email protected]> // SPDX-License-Identifier: GPL-3.0-only -macro_rules! response { +macro_rules! sync_response { ($($name:tt),*) => { $( /// Trailing commas are not supported at the moment! @@ -33,25 +33,54 @@ macro_rules! response { }; } -response!(input); -response!(sensitive_input); -response!(success); +macro_rules! async_response { + ($($name:tt),*) => { + $(::paste::paste! { + /// Trailing commas are not supported at the moment! + #[macro_export] + macro_rules! [< $name _async >] { + ($body:expr /* $(,)? */) => { + |_: ::windmark::context::RouteContext<'_>| async { ::windmark::Response::$name($body) } + }; + ($context:ident, $body:expr /* $(,)? */) => { + |$context: ::windmark::context::RouteContext<'_>| async { ::windmark::Response::$name($body) } + }; + } + })* + }; +} + +macro_rules! response { + ($($name:tt),* $(,)?) => { + $( + sync_response!($name); + async_response!($name); + )* + }; +} + +response!( + input, + sensitive_input, + success, + temporary_redirect, + permanent_redirect, + temporary_failure, + server_unavailable, + cgi_error, + proxy_error, + slow_down, + permanent_failure, + not_found, + gone, + proxy_refused, + bad_request, + client_certificate_required, + certificate_not_valid, +); + #[cfg(feature = "auto-deduce-mime")] response!(binary_success_auto); -response!(temporary_redirect); -response!(permanent_redirect); -response!(temporary_failure); -response!(server_unavailable); -response!(cgi_error); -response!(proxy_error); -response!(slow_down); -response!(permanent_failure); -response!(not_found); -response!(gone); -response!(proxy_refused); -response!(bad_request); -response!(client_certificate_required); -response!(certificate_not_valid); /// Trailing commas are not supported at the moment! #[macro_export] |