use axum_core::extract::FromRequestParts;
use http::request::Parts;
use crate::{
HX_BOOSTED, HX_CURRENT_URL, HX_HISTORY_RESTORE_REQUEST, HX_PROMPT, HX_REQUEST, HX_TARGET,
HX_TRIGGER, HX_TRIGGER_NAME,
};
#[derive(Debug, Clone, Copy)]
pub struct HxBoosted(pub bool);
impl<S> FromRequestParts<S> for HxBoosted
where
S: Send + Sync,
{
type Rejection = std::convert::Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
if parts.headers.contains_key(HX_BOOSTED) {
Ok(HxBoosted(true))
} else {
Ok(HxBoosted(false))
}
}
}
#[derive(Debug, Clone)]
pub struct HxCurrentUrl(pub Option<http::Uri>);
impl<S> FromRequestParts<S> for HxCurrentUrl
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(url) = parts.headers.get(HX_CURRENT_URL) {
let url = url
.to_str()
.ok()
.and_then(|url| url.parse::<http::Uri>().ok());
return Ok(HxCurrentUrl(url));
}
Ok(HxCurrentUrl(None))
}
}
#[derive(Debug, Clone, Copy)]
pub struct HxHistoryRestoreRequest(pub bool);
impl<S> FromRequestParts<S> for HxHistoryRestoreRequest
where
S: Send + Sync,
{
type Rejection = std::convert::Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
if parts.headers.contains_key(HX_HISTORY_RESTORE_REQUEST) {
Ok(HxHistoryRestoreRequest(true))
} else {
Ok(HxHistoryRestoreRequest(false))
}
}
}
#[derive(Debug, Clone)]
pub struct HxPrompt(pub Option<String>);
impl<S> FromRequestParts<S> for HxPrompt
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(prompt) = parts.headers.get(HX_PROMPT) {
if let Ok(prompt) = prompt.to_str() {
return Ok(HxPrompt(Some(prompt.to_string())));
}
}
Ok(HxPrompt(None))
}
}
#[derive(Debug, Clone, Copy)]
pub struct HxRequest(pub bool);
impl<S> FromRequestParts<S> for HxRequest
where
S: Send + Sync,
{
type Rejection = std::convert::Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
#[cfg(feature = "auto-vary")]
parts
.extensions
.get_mut::<crate::auto_vary::HxRequestExtracted>()
.map(crate::auto_vary::Notifier::notify);
if parts.headers.contains_key(HX_REQUEST) {
Ok(HxRequest(true))
} else {
Ok(HxRequest(false))
}
}
}
#[derive(Debug, Clone)]
pub struct HxTarget(pub Option<String>);
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> {
#[cfg(feature = "auto-vary")]
parts
.extensions
.get_mut::<crate::auto_vary::HxTargetExtracted>()
.map(crate::auto_vary::Notifier::notify);
if let Some(target) = parts.headers.get(HX_TARGET) {
if let Ok(target) = target.to_str() {
return Ok(HxTarget(Some(target.to_string())));
}
}
Ok(HxTarget(None))
}
}
#[derive(Debug, Clone)]
pub struct HxTriggerName(pub Option<String>);
impl<S> FromRequestParts<S> for HxTriggerName
where
S: Send + Sync,
{
type Rejection = std::convert::Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
#[cfg(feature = "auto-vary")]
parts
.extensions
.get_mut::<crate::auto_vary::HxTriggerNameExtracted>()
.map(crate::auto_vary::Notifier::notify);
if let Some(trigger_name) = parts.headers.get(HX_TRIGGER_NAME) {
if let Ok(trigger_name) = trigger_name.to_str() {
return Ok(HxTriggerName(Some(trigger_name.to_string())));
}
}
Ok(HxTriggerName(None))
}
}
#[derive(Debug, Clone)]
pub struct HxTrigger(pub Option<String>);
impl<S> FromRequestParts<S> for HxTrigger
where
S: Send + Sync,
{
type Rejection = std::convert::Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
#[cfg(feature = "auto-vary")]
parts
.extensions
.get_mut::<crate::auto_vary::HxTriggerExtracted>()
.map(crate::auto_vary::Notifier::notify);
if let Some(trigger) = parts.headers.get(HX_TRIGGER) {
if let Ok(trigger) = trigger.to_str() {
return Ok(HxTrigger(Some(trigger.to_string())));
}
}
Ok(HxTrigger(None))
}
}