From b3a0bec28c082060f23b93725cafec16ef141781 Mon Sep 17 00:00:00 2001 From: Rob Wagner Date: Sat, 22 Jul 2023 17:19:55 -0400 Subject: [PATCH] Add HX-Request and HX-Target --- src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7554ebd..584fc87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -153,3 +153,39 @@ where return Ok(HxCurrentUrl("".to_string())); } } + +#[derive(Debug, Clone, Copy)] +pub struct HxRequest(pub bool); + +#[axum::async_trait] +impl FromRequestParts for HxRequest +where + S: Send + Sync, +{ + type Rejection = std::convert::Infallible; + + async fn from_request_parts(_: &mut Parts, _: &S) -> Result { + return Ok(HxRequest(true)); + } +} + +#[derive(Debug, Clone)] +pub struct HxTarget(pub Option); + +#[axum::async_trait] +impl FromRequestParts for HxTarget +where + S: Send + Sync, +{ + type Rejection = std::convert::Infallible; + + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { + if let Some(target) = parts.headers.get(HtmxRequestHeader::Target.as_str()) { + if let Ok(target) = target.to_str() { + return Ok(HxTarget(Some(target.to_string()))); + } + } + + return Ok(HxTarget(None)); + } +}