reintroduce the variadic macro for ruma handler.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-07 00:53:30 +00:00
parent f871d8fd4e
commit aebae11c82

View file

@ -1,6 +1,7 @@
use std::future::Future; use std::future::Future;
use axum::{ use axum::{
extract::FromRequestParts,
response::IntoResponse, response::IntoResponse,
routing::{on, MethodFilter}, routing::{on, MethodFilter},
Router, Router,
@ -32,13 +33,17 @@ pub(in super::super) trait RumaHandler<T> {
fn add_route(&self, router: Router, path: &str) -> Router; fn add_route(&self, router: Router, path: &str) -> Router;
} }
impl<Req, E, F, Fut> RumaHandler<Ruma<Req>> for F macro_rules! ruma_handler {
where ( $($tx:ident),* $(,)? ) => {
#[allow(non_snake_case)]
impl<Req, Ret, Fut, Fun, $($tx,)*> RumaHandler<($($tx,)* Ruma<Req>,)> for Fun
where
Req: IncomingRequest + Send + 'static, Req: IncomingRequest + Send + 'static,
F: FnOnce(Ruma<Req>) -> Fut + Clone + Send + Sync + 'static, Ret: IntoResponse,
Fut: Future<Output = Result<Req::OutgoingResponse, E>> + Send, Fut: Future<Output = Result<Req::OutgoingResponse, Ret>> + Send,
E: IntoResponse, Fun: FnOnce($($tx,)* Ruma<Req>) -> Fut + Clone + Send + Sync + 'static,
{ $( $tx: FromRequestParts<()> + Send + 'static, )*
{
fn add_routes(&self, router: Router) -> Router { fn add_routes(&self, router: Router) -> Router {
Req::METADATA Req::METADATA
.history .history
@ -49,10 +54,17 @@ where
fn add_route(&self, router: Router, path: &str) -> Router { fn add_route(&self, router: Router, path: &str) -> Router {
let handle = self.clone(); let handle = self.clone();
let method = method_to_filter(&Req::METADATA.method); let method = method_to_filter(&Req::METADATA.method);
let action = |req| async { handle(req).await.map(RumaResponse) }; let action = |$($tx,)* req| async { handle($($tx,)* req).await.map(RumaResponse) };
router.route(path, on(method, action)) 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 { const fn method_to_filter(method: &Method) -> MethodFilter {
match *method { match *method {