From 5fcf0c1e94e4ac1b7b97ba70411c3de0dea23529 Mon Sep 17 00:00:00 2001 From: Rob Wagner Date: Sat, 22 Jul 2023 17:47:48 -0400 Subject: [PATCH] Add remaining request header extractors --- src/request.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/request.rs b/src/request.rs index 7c2e167..8142812 100644 --- a/src/request.rs +++ b/src/request.rs @@ -93,6 +93,48 @@ where } } +pub struct HxHistoryRestoreRequest(pub bool); + +#[axum::async_trait] +impl FromRequestParts for HxHistoryRestoreRequest +where + S: Send + Sync, +{ + type Rejection = std::convert::Infallible; + + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { + if parts + .headers + .contains_key(HtmxRequestHeader::HistoryRestoreRequest.as_str()) + { + return Ok(HxHistoryRestoreRequest(true)); + } else { + return Ok(HxHistoryRestoreRequest(false)); + } + } +} + +#[derive(Debug, Clone)] +pub struct HxPrompt(pub Option); + +#[axum::async_trait] +impl FromRequestParts for HxPrompt +where + S: Send + Sync, +{ + type Rejection = std::convert::Infallible; + + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { + if let Some(prompt) = parts.headers.get(HtmxRequestHeader::Prompt.as_str()) { + if let Ok(prompt) = prompt.to_str() { + return Ok(HxPrompt(Some(prompt.to_string()))); + } + } + + return Ok(HxPrompt(None)); + } +} + #[derive(Debug, Clone, Copy)] pub struct HxRequest(pub bool);