From cdbd892f85f88120821d0fff423f6caaf6ab6cb8 Mon Sep 17 00:00:00 2001 From: imbolc Date: Sat, 15 Jun 2024 13:34:27 +0600 Subject: [PATCH] Define notifiers with macro rules --- src/vary_middleware.rs | 100 ++++++++++++----------------------------- 1 file changed, 28 insertions(+), 72 deletions(-) diff --git a/src/vary_middleware.rs b/src/vary_middleware.rs index 9349008..0904c16 100644 --- a/src/vary_middleware.rs +++ b/src/vary_middleware.rs @@ -16,18 +16,6 @@ use crate::{ const MIDDLEWARE_DOUBLE_USE: &str = "Configuration error: `axum_httpx::vary_middleware` is used twice"; -#[derive(Clone)] -pub(crate) struct HxRequestExtracted(Option>>); - -#[derive(Clone)] -pub(crate) struct HxTargetExtracted(Option>>); - -#[derive(Clone)] -pub(crate) struct HxTriggerExtracted(Option>>); - -#[derive(Clone)] -pub(crate) struct HxTriggerNameExtracted(Option>>); - pub trait Notifier { fn sender(&mut self) -> Option>; @@ -38,69 +26,37 @@ pub trait Notifier { } } -impl Notifier for HxRequestExtracted { - fn sender(&mut self) -> Option> { - self.0.take().and_then(Arc::into_inner) +macro_rules! define_notifiers { + ($($name:ident),*) => { + $( + #[derive(Clone)] + pub(crate) struct $name(Option>>); + + impl Notifier for $name { + fn sender(&mut self) -> Option> { + self.0.take().and_then(Arc::into_inner) + } + } + + impl $name { + fn insert_into_extensions(extensions: &mut Extensions) -> Receiver<()> { + let (tx, rx) = oneshot::channel(); + if extensions.insert(Self(Some(Arc::new(tx)))).is_some() { + panic!("{}", MIDDLEWARE_DOUBLE_USE); + } + rx + } + } + )* } } -impl Notifier for HxTargetExtracted { - fn sender(&mut self) -> Option> { - self.0.take().and_then(Arc::into_inner) - } -} - -impl Notifier for HxTriggerExtracted { - fn sender(&mut self) -> Option> { - self.0.take().and_then(Arc::into_inner) - } -} - -impl Notifier for HxTriggerNameExtracted { - fn sender(&mut self) -> Option> { - self.0.take().and_then(Arc::into_inner) - } -} - -impl HxRequestExtracted { - fn insert_into_extensions(extensions: &mut Extensions) -> Receiver<()> { - let (tx, rx) = oneshot::channel(); - if extensions.insert(Self(Some(Arc::new(tx)))).is_some() { - panic!("{}", MIDDLEWARE_DOUBLE_USE); - } - rx - } -} - -impl HxTargetExtracted { - fn insert_into_extensions(extensions: &mut Extensions) -> Receiver<()> { - let (tx, rx) = oneshot::channel(); - if extensions.insert(Self(Some(Arc::new(tx)))).is_some() { - panic!("{}", MIDDLEWARE_DOUBLE_USE); - } - rx - } -} - -impl HxTriggerExtracted { - fn insert_into_extensions(extensions: &mut Extensions) -> Receiver<()> { - let (tx, rx) = oneshot::channel(); - if extensions.insert(Self(Some(Arc::new(tx)))).is_some() { - panic!("{}", MIDDLEWARE_DOUBLE_USE); - } - rx - } -} - -impl HxTriggerNameExtracted { - fn insert_into_extensions(extensions: &mut Extensions) -> Receiver<()> { - let (tx, rx) = oneshot::channel(); - if extensions.insert(Self(Some(Arc::new(tx)))).is_some() { - panic!("{}", MIDDLEWARE_DOUBLE_USE); - } - rx - } -} +define_notifiers!( + HxRequestExtracted, + HxTargetExtracted, + HxTriggerExtracted, + HxTriggerNameExtracted +); pub async fn vary_middleware(mut request: Request, next: Next) -> Response { let hx_request_rx = HxRequestExtracted::insert_into_extensions(request.extensions_mut());