mirror of
https://github.com/robertwayne/axum-htmx
synced 2024-11-27 13:44:55 +01:00
Add HX-Request and HX-Target
This commit is contained in:
parent
f5680852cd
commit
b3a0bec28c
1 changed files with 36 additions and 0 deletions
36
src/lib.rs
36
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<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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue