From aebae11c8289def05428ac82b6e71f1c143063de Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 7 Jun 2024 00:53:30 +0000 Subject: [PATCH] reintroduce the variadic macro for ruma handler. Signed-off-by: Jason Volk --- src/api/ruma_wrapper/handler.rs | 48 ++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/api/ruma_wrapper/handler.rs b/src/api/ruma_wrapper/handler.rs index f769d2bb..73361989 100644 --- a/src/api/ruma_wrapper/handler.rs +++ b/src/api/ruma_wrapper/handler.rs @@ -1,6 +1,7 @@ use std::future::Future; use axum::{ + extract::FromRequestParts, response::IntoResponse, routing::{on, MethodFilter}, Router, @@ -32,27 +33,38 @@ pub(in super::super) trait RumaHandler { fn add_route(&self, router: Router, path: &str) -> Router; } -impl RumaHandler> for F -where - Req: IncomingRequest + Send + 'static, - F: FnOnce(Ruma) -> Fut + Clone + Send + Sync + 'static, - Fut: Future> + Send, - E: IntoResponse, -{ - fn add_routes(&self, router: Router) -> Router { - Req::METADATA - .history - .all_paths() - .fold(router, |router, path| self.add_route(router, path)) - } +macro_rules! ruma_handler { + ( $($tx:ident),* $(,)? ) => { + #[allow(non_snake_case)] + impl RumaHandler<($($tx,)* Ruma,)> for Fun + where + Req: IncomingRequest + Send + 'static, + Ret: IntoResponse, + Fut: Future> + Send, + Fun: FnOnce($($tx,)* Ruma) -> Fut + Clone + Send + Sync + 'static, + $( $tx: FromRequestParts<()> + Send + 'static, )* + { + fn add_routes(&self, router: Router) -> Router { + Req::METADATA + .history + .all_paths() + .fold(router, |router, path| self.add_route(router, path)) + } - fn add_route(&self, router: Router, path: &str) -> Router { - let handle = self.clone(); - let method = method_to_filter(&Req::METADATA.method); - let action = |req| async { handle(req).await.map(RumaResponse) }; - router.route(path, on(method, action)) + fn add_route(&self, router: Router, path: &str) -> Router { + let handle = self.clone(); + let method = method_to_filter(&Req::METADATA.method); + let action = |$($tx,)* req| async { handle($($tx,)* req).await.map(RumaResponse) }; + router.route(path, on(method, action)) + } + } } } +ruma_handler!(); +ruma_handler!(T1); +ruma_handler!(T1, T2); +ruma_handler!(T1, T2, T3); +ruma_handler!(T1, T2, T3, T4); const fn method_to_filter(method: &Method) -> MethodFilter { match *method {