mirror of
https://github.com/robertwayne/axum-htmx
synced 2024-11-24 04:12:50 +01:00
Update doc comments
This commit is contained in:
parent
43a16eb708
commit
b07401cdff
1 changed files with 66 additions and 11 deletions
|
@ -1,15 +1,17 @@
|
||||||
use axum::{extract::FromRequestParts, http::request::Parts};
|
use axum::{extract::FromRequestParts, http::request::Parts};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
HX_BOOSTED, HX_CURRENT_URL, HX_HISTORY_RESTORE_REQUEST, HX_PROMPT, HX_TARGET, HX_TRIGGER,
|
HX_BOOSTED, HX_CURRENT_URL, HX_HISTORY_RESTORE_REQUEST, HX_PROMPT, HX_REQUEST, HX_TARGET,
|
||||||
HX_TRIGGER_NAME,
|
HX_TRIGGER, HX_TRIGGER_NAME,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The `HX-Boosted` header. This header is set when a request is made with the
|
/// The `HX-Boosted` header.
|
||||||
/// "hx-boost" attribute is set on an element.
|
|
||||||
///
|
///
|
||||||
/// This extractor does not fail if no header is present, instead returning a
|
/// This is set when a request is made from an element where its parent has the
|
||||||
/// `false` value.
|
/// `hx-boost` attribute set to `true`.
|
||||||
|
///
|
||||||
|
/// This extractor will always return a value. If the header is not present, it
|
||||||
|
/// will return `false`.
|
||||||
///
|
///
|
||||||
/// See <https://htmx.org/attributes/hx-boost/> for more information.
|
/// See <https://htmx.org/attributes/hx-boost/> for more information.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
@ -31,8 +33,15 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The `HX-Current-Url` header.
|
||||||
|
///
|
||||||
|
/// This is set on every request made by htmx itself. As its name implies, it
|
||||||
|
/// just contains the current url.
|
||||||
|
///
|
||||||
|
/// This extractor will always return a value. If the header is not present, it
|
||||||
|
/// will return `None`.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct HxCurrentUrl(pub String);
|
pub struct HxCurrentUrl(pub Option<String>);
|
||||||
|
|
||||||
#[axum::async_trait]
|
#[axum::async_trait]
|
||||||
impl<S> FromRequestParts<S> for HxCurrentUrl
|
impl<S> FromRequestParts<S> for HxCurrentUrl
|
||||||
|
@ -44,14 +53,18 @@ where
|
||||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||||
if let Some(url) = parts.headers.get(HX_CURRENT_URL) {
|
if let Some(url) = parts.headers.get(HX_CURRENT_URL) {
|
||||||
if let Ok(url) = url.to_str() {
|
if let Ok(url) = url.to_str() {
|
||||||
return Ok(HxCurrentUrl(url.to_string()));
|
return Ok(HxCurrentUrl(Some(url.to_string())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(HxCurrentUrl("".to_string()));
|
return Ok(HxCurrentUrl(None));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The `HX-History-Restore-Request` header.
|
||||||
|
///
|
||||||
|
/// This extractor will always return a value. If the header is not present,
|
||||||
|
/// it will return `false`.
|
||||||
pub struct HxHistoryRestoreRequest(pub bool);
|
pub struct HxHistoryRestoreRequest(pub bool);
|
||||||
|
|
||||||
#[axum::async_trait]
|
#[axum::async_trait]
|
||||||
|
@ -70,6 +83,13 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The `HX-Prompt` header.
|
||||||
|
///
|
||||||
|
/// This is set when a request is made from an element that has the `hx-prompt`
|
||||||
|
/// attribute set. The value will contain the string input by the user.
|
||||||
|
///
|
||||||
|
/// This extractor will always return a value. If the header is not present, it
|
||||||
|
/// will return `None`.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct HxPrompt(pub Option<String>);
|
pub struct HxPrompt(pub Option<String>);
|
||||||
|
|
||||||
|
@ -91,6 +111,13 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The `HX-Request` header.
|
||||||
|
///
|
||||||
|
/// This is set on every request made by htmx itself. It won't be present on
|
||||||
|
/// requests made manually, or by other libraries.
|
||||||
|
///
|
||||||
|
/// This extractor will always return a value. If the header is not present, it
|
||||||
|
/// will return `false`.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct HxRequest(pub bool);
|
pub struct HxRequest(pub bool);
|
||||||
|
|
||||||
|
@ -101,11 +128,23 @@ where
|
||||||
{
|
{
|
||||||
type Rejection = std::convert::Infallible;
|
type Rejection = std::convert::Infallible;
|
||||||
|
|
||||||
async fn from_request_parts(_: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||||
|
if parts.headers.contains_key(HX_REQUEST) {
|
||||||
return Ok(HxRequest(true));
|
return Ok(HxRequest(true));
|
||||||
|
} else {
|
||||||
|
return Ok(HxRequest(false));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The `HX-Target` header.
|
||||||
|
///
|
||||||
|
/// This is set when a request is made from an element that has the `hx-target`
|
||||||
|
/// attribute set. The value will contain the target element's id. If the
|
||||||
|
/// id does not exist on the page, the value will be None.
|
||||||
|
///
|
||||||
|
/// This extractor will always return a value. If the header is not present, it
|
||||||
|
/// will return `None`.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct HxTarget(pub Option<String>);
|
pub struct HxTarget(pub Option<String>);
|
||||||
|
|
||||||
|
@ -127,6 +166,14 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The `HX-Trigger-Name` header.
|
||||||
|
///
|
||||||
|
/// This is set when a request is made from an element that has the `hx-trigger`
|
||||||
|
/// attribute set. The value will contain the trigger element's name. If the
|
||||||
|
/// name does not exist on the page, the value will be None.
|
||||||
|
///
|
||||||
|
/// This extractor will always return a value. If the header is not present, it
|
||||||
|
/// will return `None`.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct HxTriggerName(pub Option<String>);
|
pub struct HxTriggerName(pub Option<String>);
|
||||||
|
|
||||||
|
@ -148,6 +195,14 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The `HX-Trigger` header.
|
||||||
|
///
|
||||||
|
/// This is set when a request is made from an element that has the `hx-trigger`
|
||||||
|
/// attribute set. The value will contain the trigger element's id. If the
|
||||||
|
/// id does not exist on the page, the value will be None.
|
||||||
|
///
|
||||||
|
/// This extractor will always return a value. If the header is not present, it
|
||||||
|
/// will return `None`.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct HxTrigger(pub Option<String>);
|
pub struct HxTrigger(pub Option<String>);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue