Define notifiers with macro rules

This commit is contained in:
imbolc 2024-06-15 13:34:27 +06:00
parent 5220c8b861
commit cdbd892f85

View file

@ -16,18 +16,6 @@ use crate::{
const MIDDLEWARE_DOUBLE_USE: &str = const MIDDLEWARE_DOUBLE_USE: &str =
"Configuration error: `axum_httpx::vary_middleware` is used twice"; "Configuration error: `axum_httpx::vary_middleware` is used twice";
#[derive(Clone)]
pub(crate) struct HxRequestExtracted(Option<Arc<Sender<()>>>);
#[derive(Clone)]
pub(crate) struct HxTargetExtracted(Option<Arc<Sender<()>>>);
#[derive(Clone)]
pub(crate) struct HxTriggerExtracted(Option<Arc<Sender<()>>>);
#[derive(Clone)]
pub(crate) struct HxTriggerNameExtracted(Option<Arc<Sender<()>>>);
pub trait Notifier { pub trait Notifier {
fn sender(&mut self) -> Option<Sender<()>>; fn sender(&mut self) -> Option<Sender<()>>;
@ -38,69 +26,37 @@ pub trait Notifier {
} }
} }
impl Notifier for HxRequestExtracted { macro_rules! define_notifiers {
fn sender(&mut self) -> Option<Sender<()>> { ($($name:ident),*) => {
self.0.take().and_then(Arc::into_inner) $(
#[derive(Clone)]
pub(crate) struct $name(Option<Arc<Sender<()>>>);
impl Notifier for $name {
fn sender(&mut self) -> Option<Sender<()>> {
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 { define_notifiers!(
fn sender(&mut self) -> Option<Sender<()>> { HxRequestExtracted,
self.0.take().and_then(Arc::into_inner) HxTargetExtracted,
} HxTriggerExtracted,
} HxTriggerNameExtracted
);
impl Notifier for HxTriggerExtracted {
fn sender(&mut self) -> Option<Sender<()>> {
self.0.take().and_then(Arc::into_inner)
}
}
impl Notifier for HxTriggerNameExtracted {
fn sender(&mut self) -> Option<Sender<()>> {
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
}
}
pub async fn vary_middleware(mut request: Request, next: Next) -> Response { pub async fn vary_middleware(mut request: Request, next: Next) -> Response {
let hx_request_rx = HxRequestExtracted::insert_into_extensions(request.extensions_mut()); let hx_request_rx = HxRequestExtracted::insert_into_extensions(request.extensions_mut());