mirror of
https://github.com/robertwayne/axum-htmx
synced 2024-11-29 22:54:32 +01:00
Restructuring
This commit is contained in:
parent
e08a2874a7
commit
cd6c700082
4 changed files with 86 additions and 112 deletions
73
src/headers.rs
Normal file
73
src/headers.rs
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
/// Indicates that the request is via an element using `hx-boost` attribute.
|
||||||
|
///
|
||||||
|
/// See <https://htmx.org/attributes/hx-boost/> for more information.
|
||||||
|
pub const HX_BOOSTED: &str = "HX-Boosted";
|
||||||
|
|
||||||
|
/// The current URL of the browser.
|
||||||
|
pub const HX_CURRENT_URL: &str = "HX-Current-URL";
|
||||||
|
|
||||||
|
/// `true` if the request is for history restoration after a miss in the local
|
||||||
|
/// history cache.
|
||||||
|
pub const HX_HISTORY_RESTORE_REQUEST: &str = "HX-History-Restore-Request";
|
||||||
|
|
||||||
|
/// The user response to an `hx-prompt`
|
||||||
|
///
|
||||||
|
/// See <https://htmx.org/attributes/hx-prompt/> for more information.
|
||||||
|
pub const HX_PROMPT: &str = "HX-Prompt";
|
||||||
|
|
||||||
|
/// Always `true`.
|
||||||
|
pub const HX_REQUEST: &str = "HX-Request";
|
||||||
|
|
||||||
|
/// The `id` of the target element, if it exists.
|
||||||
|
pub const HX_TARGET: &str = "HX-Target";
|
||||||
|
|
||||||
|
/// The `name` of the triggered element, if it exists.
|
||||||
|
pub const HX_TRIGGER_NAME: &str = "HX-Trigger-Name";
|
||||||
|
|
||||||
|
/// Allows you to do a client-side redirect that does not do a full page reload.
|
||||||
|
pub const HX_LOCATION: &str = "HX-Location";
|
||||||
|
|
||||||
|
/// Pushes a new URL onto the history stack.
|
||||||
|
pub const HX_PUSH_URL: &str = "HX-Push-Url";
|
||||||
|
|
||||||
|
/// Can be used to do a client-side redirect to a new location.
|
||||||
|
pub const HX_REDIRECT: &str = "HX-Redirect";
|
||||||
|
|
||||||
|
/// If set to `true`, the client will do a full refresh on the page.
|
||||||
|
pub const HX_REFRESH: &str = "HX-Refresh";
|
||||||
|
|
||||||
|
/// Replaces the currelt URL in the location bar.
|
||||||
|
pub const HX_REPLACE_URL: &str = "HX-Replace-Url";
|
||||||
|
|
||||||
|
/// Allows you to specify how the response value will be swapped.
|
||||||
|
///
|
||||||
|
/// See <https://htmx.org/attributes/hx-swap/> for more information.
|
||||||
|
pub const HX_RESWAP: &str = "HX-Reswap";
|
||||||
|
|
||||||
|
/// A CSS selector that update the target of the content update to a different
|
||||||
|
/// element on the page.
|
||||||
|
pub const HX_RETARGET: &str = "HX-Retarget";
|
||||||
|
|
||||||
|
/// A CSS selector that allows you to choose which part of the response is used
|
||||||
|
/// to be swapped in. Overrides an existing `hx-select` on the triggering
|
||||||
|
/// element
|
||||||
|
pub const HX_RESELECT: &str = "HX-Reselect";
|
||||||
|
|
||||||
|
/// Can be set as a request or response header.
|
||||||
|
///
|
||||||
|
/// In a request, it contains the `id of the element that triggered the request.
|
||||||
|
///
|
||||||
|
/// In a response, it can be used to trigger client-side events.
|
||||||
|
///
|
||||||
|
/// See <https://htmx.org/headers/hx-trigger/> for more information.
|
||||||
|
pub const HX_TRIGGER: &str = "HX-Trigger";
|
||||||
|
|
||||||
|
/// Allows you to trigger client-side events.
|
||||||
|
///
|
||||||
|
/// See <https://htmx.org/headers/hx-trigger/> for more information.
|
||||||
|
pub const HX_TRIGGER_AFTER_SETTLE: &str = "HX-Trigger-After-Settle";
|
||||||
|
|
||||||
|
/// Allows you to trigger client-side events.
|
||||||
|
///
|
||||||
|
/// See <https://htmx.org/headers/hx-trigger/> for more information.
|
||||||
|
pub const HX_TRIGGER_AFTER_SWAP: &str = "HX-Trigger-After-Swap";
|
|
@ -1,6 +1,8 @@
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
|
pub mod headers;
|
||||||
pub mod request;
|
pub mod request;
|
||||||
pub mod response;
|
pub mod response;
|
||||||
|
|
||||||
|
pub use headers::*;
|
||||||
pub use request::*;
|
pub use request::*;
|
||||||
pub use response::*;
|
pub use response::*;
|
||||||
|
|
|
@ -1,47 +1,9 @@
|
||||||
use axum::{extract::FromRequestParts, http::request::Parts};
|
use axum::{extract::FromRequestParts, http::request::Parts};
|
||||||
|
|
||||||
/// Represents all of the headers that can be sent in a request to the server.
|
use crate::{
|
||||||
///
|
HX_BOOSTED, HX_CURRENT_URL, HX_HISTORY_RESTORE_REQUEST, HX_PROMPT, HX_TARGET, HX_TRIGGER,
|
||||||
/// See <https://htmx.org/reference/#request_headers> for more information.
|
HX_TRIGGER_NAME,
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
};
|
||||||
pub enum HtmxRequestHeader {
|
|
||||||
/// Indicates that the request is via an element using `hx-boost` attribute.
|
|
||||||
///
|
|
||||||
/// See <https://htmx.org/attributes/hx-boost/> for more information.
|
|
||||||
Boosted,
|
|
||||||
/// The current URL of the browser.
|
|
||||||
CurrentUrl,
|
|
||||||
/// `true` if the request is for history restoration after a miss in the
|
|
||||||
/// local history cache.
|
|
||||||
HistoryRestoreRequest,
|
|
||||||
/// The user response to an `hx-prompt`
|
|
||||||
///
|
|
||||||
/// See <https://htmx.org/attributes/hx-prompt/> for more information.
|
|
||||||
Prompt,
|
|
||||||
/// Always `true`.
|
|
||||||
Request,
|
|
||||||
/// The `id` of the target element, if it exists.
|
|
||||||
Target,
|
|
||||||
/// The `name` of the triggered element, if it exists.
|
|
||||||
TriggerName,
|
|
||||||
/// The `id` of the triggered element, if it exists.
|
|
||||||
Trigger,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HtmxRequestHeader {
|
|
||||||
pub fn as_str(&self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
HtmxRequestHeader::Boosted => "HX-Boosted",
|
|
||||||
HtmxRequestHeader::CurrentUrl => "HX-Current-Url",
|
|
||||||
HtmxRequestHeader::HistoryRestoreRequest => "HX-History-Restore-Request",
|
|
||||||
HtmxRequestHeader::Prompt => "HX-Prompt",
|
|
||||||
HtmxRequestHeader::Request => "HX-Request",
|
|
||||||
HtmxRequestHeader::Target => "HX-Target",
|
|
||||||
HtmxRequestHeader::TriggerName => "HX-Trigger-Name",
|
|
||||||
HtmxRequestHeader::Trigger => "HX-Trigger",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The `HX-Boosted` header. This header is set when a request is made with the
|
/// The `HX-Boosted` header. This header is set when a request is made with the
|
||||||
/// "hx-boost" attribute is set on an element.
|
/// "hx-boost" attribute is set on an element.
|
||||||
|
@ -61,10 +23,7 @@ where
|
||||||
type Rejection = std::convert::Infallible;
|
type Rejection = std::convert::Infallible;
|
||||||
|
|
||||||
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 parts
|
if parts.headers.contains_key(HX_BOOSTED) {
|
||||||
.headers
|
|
||||||
.contains_key(HtmxRequestHeader::Boosted.as_str())
|
|
||||||
{
|
|
||||||
return Ok(HxBoosted(true));
|
return Ok(HxBoosted(true));
|
||||||
} else {
|
} else {
|
||||||
return Ok(HxBoosted(false));
|
return Ok(HxBoosted(false));
|
||||||
|
@ -83,7 +42,7 @@ where
|
||||||
type Rejection = std::convert::Infallible;
|
type Rejection = std::convert::Infallible;
|
||||||
|
|
||||||
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(HtmxRequestHeader::CurrentUrl.as_str()) {
|
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(url.to_string()));
|
||||||
}
|
}
|
||||||
|
@ -103,10 +62,7 @@ where
|
||||||
type Rejection = std::convert::Infallible;
|
type Rejection = std::convert::Infallible;
|
||||||
|
|
||||||
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 parts
|
if parts.headers.contains_key(HX_HISTORY_RESTORE_REQUEST) {
|
||||||
.headers
|
|
||||||
.contains_key(HtmxRequestHeader::HistoryRestoreRequest.as_str())
|
|
||||||
{
|
|
||||||
return Ok(HxHistoryRestoreRequest(true));
|
return Ok(HxHistoryRestoreRequest(true));
|
||||||
} else {
|
} else {
|
||||||
return Ok(HxHistoryRestoreRequest(false));
|
return Ok(HxHistoryRestoreRequest(false));
|
||||||
|
@ -125,7 +81,7 @@ where
|
||||||
type Rejection = std::convert::Infallible;
|
type Rejection = std::convert::Infallible;
|
||||||
|
|
||||||
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(prompt) = parts.headers.get(HtmxRequestHeader::Prompt.as_str()) {
|
if let Some(prompt) = parts.headers.get(HX_PROMPT) {
|
||||||
if let Ok(prompt) = prompt.to_str() {
|
if let Ok(prompt) = prompt.to_str() {
|
||||||
return Ok(HxPrompt(Some(prompt.to_string())));
|
return Ok(HxPrompt(Some(prompt.to_string())));
|
||||||
}
|
}
|
||||||
|
@ -161,7 +117,7 @@ where
|
||||||
type Rejection = std::convert::Infallible;
|
type Rejection = std::convert::Infallible;
|
||||||
|
|
||||||
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(target) = parts.headers.get(HtmxRequestHeader::Target.as_str()) {
|
if let Some(target) = parts.headers.get(HX_TARGET) {
|
||||||
if let Ok(target) = target.to_str() {
|
if let Ok(target) = target.to_str() {
|
||||||
return Ok(HxTarget(Some(target.to_string())));
|
return Ok(HxTarget(Some(target.to_string())));
|
||||||
}
|
}
|
||||||
|
@ -182,7 +138,7 @@ where
|
||||||
type Rejection = std::convert::Infallible;
|
type Rejection = std::convert::Infallible;
|
||||||
|
|
||||||
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(trigger_name) = parts.headers.get(HtmxRequestHeader::TriggerName.as_str()) {
|
if let Some(trigger_name) = parts.headers.get(HX_TRIGGER_NAME) {
|
||||||
if let Ok(trigger_name) = trigger_name.to_str() {
|
if let Ok(trigger_name) = trigger_name.to_str() {
|
||||||
return Ok(HxTriggerName(Some(trigger_name.to_string())));
|
return Ok(HxTriggerName(Some(trigger_name.to_string())));
|
||||||
}
|
}
|
||||||
|
@ -203,7 +159,7 @@ where
|
||||||
type Rejection = std::convert::Infallible;
|
type Rejection = std::convert::Infallible;
|
||||||
|
|
||||||
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(trigger) = parts.headers.get(HtmxRequestHeader::Trigger.as_str()) {
|
if let Some(trigger) = parts.headers.get(HX_TRIGGER) {
|
||||||
if let Ok(trigger) = trigger.to_str() {
|
if let Ok(trigger) = trigger.to_str() {
|
||||||
return Ok(HxTrigger(Some(trigger.to_string())));
|
return Ok(HxTrigger(Some(trigger.to_string())));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,58 +1 @@
|
||||||
/// Represents all of the headers that can be sent in a response to the client.
|
|
||||||
///
|
|
||||||
/// See <https://htmx.org/reference/#response_headers> for more information.
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
||||||
pub enum HtmxResponseHeader {
|
|
||||||
/// Allows you to do a client-side redirect that does not do a full page
|
|
||||||
/// reload.
|
|
||||||
Location,
|
|
||||||
/// Pushes a new URL onto the history stack.
|
|
||||||
PushUrl,
|
|
||||||
/// Can be used to do a client-side redirect to a new location.
|
|
||||||
Redirect,
|
|
||||||
/// If set to `true`, the client will do a full refresh on the page.
|
|
||||||
Refresh,
|
|
||||||
/// Replaces the currelt URL in the location bar.
|
|
||||||
ReplaceUrl,
|
|
||||||
/// Allows you to specify how the response value will be swapped.
|
|
||||||
///
|
|
||||||
/// See <https://htmx.org/attributes/hx-swap/> for more information.
|
|
||||||
Reswap,
|
|
||||||
/// A CSS selector that update the target of the content update to a
|
|
||||||
/// different element on the page.
|
|
||||||
Retarget,
|
|
||||||
/// A CSS selector that allows you to choose which part of the response is
|
|
||||||
/// used to be swapped in. Overrides an existing `hx-select` on the
|
|
||||||
/// triggering element
|
|
||||||
Reselect,
|
|
||||||
/// Allows you to trigger client-side events.
|
|
||||||
///
|
|
||||||
/// See <https://htmx.org/headers/hx-trigger/> for more information.
|
|
||||||
Trigger,
|
|
||||||
/// Allows you to trigger client-side events.
|
|
||||||
///
|
|
||||||
/// See <https://htmx.org/headers/hx-trigger/> for more information.
|
|
||||||
TriggerAfterSettle,
|
|
||||||
/// Allows you to trigger client-side events.
|
|
||||||
///
|
|
||||||
/// See <https://htmx.org/headers/hx-trigger/> for more information.
|
|
||||||
TriggerAfterSwap,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HtmxResponseHeader {
|
|
||||||
pub fn as_str(&self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
HtmxResponseHeader::Location => "HX-Location",
|
|
||||||
HtmxResponseHeader::PushUrl => "HX-Push-Url",
|
|
||||||
HtmxResponseHeader::Redirect => "HX-Redirect",
|
|
||||||
HtmxResponseHeader::Refresh => "HX-Refresh",
|
|
||||||
HtmxResponseHeader::ReplaceUrl => "HX-Replace-Url",
|
|
||||||
HtmxResponseHeader::Reswap => "HX-Reswap",
|
|
||||||
HtmxResponseHeader::Retarget => "HX-Retarget",
|
|
||||||
HtmxResponseHeader::Reselect => "HX-Reselect",
|
|
||||||
HtmxResponseHeader::Trigger => "HX-Trigger",
|
|
||||||
HtmxResponseHeader::TriggerAfterSettle => "HX-Trigger-After-Settle",
|
|
||||||
HtmxResponseHeader::TriggerAfterSwap => "HX-Trigger-After-Swap",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue