Add HX-Request and HX-Target

This commit is contained in:
Rob Wagner 2023-07-22 17:19:55 -04:00
parent f5680852cd
commit b3a0bec28c
No known key found for this signature in database
GPG key ID: 53CCB4497B15CF61

View file

@ -153,3 +153,39 @@ where
return Ok(HxCurrentUrl("".to_string())); return Ok(HxCurrentUrl("".to_string()));
} }
} }
#[derive(Debug, Clone, Copy)]
pub struct HxRequest(pub bool);
#[axum::async_trait]
impl<S> FromRequestParts<S> for HxRequest
where
S: Send + Sync,
{
type Rejection = std::convert::Infallible;
async fn from_request_parts(_: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
return Ok(HxRequest(true));
}
}
#[derive(Debug, Clone)]
pub struct HxTarget(pub Option<String>);
#[axum::async_trait]
impl<S> FromRequestParts<S> for HxTarget
where
S: Send + Sync,
{
type Rejection = std::convert::Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
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));
}
}