use axum::{extract::FromRequestParts, http::request::Parts};
use crate::{
HX_BOOSTED, HX_CURRENT_URL, HX_HISTORY_RESTORE_REQUEST, HX_PROMPT, HX_TARGET, HX_TRIGGER,
HX_TRIGGER_NAME,
};
/// The `HX-Boosted` header. This header is set when a request is made with the
/// "hx-boost" attribute is set on an element.
///
/// This extractor does not fail if no header is present, instead returning a
/// `false` value.
///
/// See for more information.
#[derive(Debug, Clone, Copy)]
pub struct HxBoosted(pub bool);
#[axum::async_trait]
impl FromRequestParts for HxBoosted
where
S: Send + Sync,
{
type Rejection = std::convert::Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result {
if parts.headers.contains_key(HX_BOOSTED) {
return Ok(HxBoosted(true));
} else {
return Ok(HxBoosted(false));
}
}
}
#[derive(Debug, Clone)]
pub struct HxCurrentUrl(pub String);
#[axum::async_trait]
impl FromRequestParts for HxCurrentUrl
where
S: Send + Sync,
{
type Rejection = std::convert::Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result {
if let Some(url) = parts.headers.get(HX_CURRENT_URL) {
if let Ok(url) = url.to_str() {
return Ok(HxCurrentUrl(url.to_string()));
}
}
return Ok(HxCurrentUrl("".to_string()));
}
}
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(HX_HISTORY_RESTORE_REQUEST) {
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(HX_PROMPT) {
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);
#[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(HX_TARGET) {
if let Ok(target) = target.to_str() {
return Ok(HxTarget(Some(target.to_string())));
}
}
return Ok(HxTarget(None));
}
}
#[derive(Debug, Clone)]
pub struct HxTriggerName(pub Option);
#[axum::async_trait]
impl FromRequestParts for HxTriggerName
where
S: Send + Sync,
{
type Rejection = std::convert::Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result {
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())));
}
}
return Ok(HxTriggerName(None));
}
}
#[derive(Debug, Clone)]
pub struct HxTrigger(pub Option);
#[axum::async_trait]
impl FromRequestParts for HxTrigger
where
S: Send + Sync,
{
type Rejection = std::convert::Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result {
if let Some(trigger) = parts.headers.get(HX_TRIGGER) {
if let Ok(trigger) = trigger.to_str() {
return Ok(HxTrigger(Some(trigger.to_string())));
}
}
return Ok(HxTrigger(None));
}
}