mirror of
https://github.com/robertwayne/axum-htmx
synced 2024-11-26 05:09:35 +01:00
Define notifiers with macro rules
This commit is contained in:
parent
5220c8b861
commit
cdbd892f85
1 changed files with 28 additions and 72 deletions
|
@ -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,31 +26,19 @@ pub trait Notifier {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Notifier for HxRequestExtracted {
|
macro_rules! define_notifiers {
|
||||||
|
($($name:ident),*) => {
|
||||||
|
$(
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub(crate) struct $name(Option<Arc<Sender<()>>>);
|
||||||
|
|
||||||
|
impl Notifier for $name {
|
||||||
fn sender(&mut self) -> Option<Sender<()>> {
|
fn sender(&mut self) -> Option<Sender<()>> {
|
||||||
self.0.take().and_then(Arc::into_inner)
|
self.0.take().and_then(Arc::into_inner)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Notifier for HxTargetExtracted {
|
|
||||||
fn sender(&mut self) -> Option<Sender<()>> {
|
|
||||||
self.0.take().and_then(Arc::into_inner)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Notifier for HxTriggerExtracted {
|
impl $name {
|
||||||
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<()> {
|
fn insert_into_extensions(extensions: &mut Extensions) -> Receiver<()> {
|
||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
if extensions.insert(Self(Some(Arc::new(tx)))).is_some() {
|
if extensions.insert(Self(Some(Arc::new(tx)))).is_some() {
|
||||||
|
@ -70,37 +46,17 @@ impl HxRequestExtracted {
|
||||||
}
|
}
|
||||||
rx
|
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 {
|
define_notifiers!(
|
||||||
fn insert_into_extensions(extensions: &mut Extensions) -> Receiver<()> {
|
HxRequestExtracted,
|
||||||
let (tx, rx) = oneshot::channel();
|
HxTargetExtracted,
|
||||||
if extensions.insert(Self(Some(Arc::new(tx)))).is_some() {
|
HxTriggerExtracted,
|
||||||
panic!("{}", MIDDLEWARE_DOUBLE_USE);
|
HxTriggerNameExtracted
|
||||||
}
|
);
|
||||||
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());
|
||||||
|
|
Loading…
Reference in a new issue