From cd6c700082fe0d6bad3062c7e76361bc24746996 Mon Sep 17 00:00:00 2001 From: Rob Wagner Date: Sat, 22 Jul 2023 18:20:29 -0400 Subject: [PATCH] Restructuring --- src/headers.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ src/request.rs | 66 ++++++++------------------------------------ src/response.rs | 57 -------------------------------------- 4 files changed, 86 insertions(+), 112 deletions(-) create mode 100644 src/headers.rs diff --git a/src/headers.rs b/src/headers.rs new file mode 100644 index 0000000..1b922c8 --- /dev/null +++ b/src/headers.rs @@ -0,0 +1,73 @@ +/// Indicates that the request is via an element using `hx-boost` attribute. +/// +/// See 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 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 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 for more information. +pub const HX_TRIGGER: &str = "HX-Trigger"; + +/// Allows you to trigger client-side events. +/// +/// See for more information. +pub const HX_TRIGGER_AFTER_SETTLE: &str = "HX-Trigger-After-Settle"; + +/// Allows you to trigger client-side events. +/// +/// See for more information. +pub const HX_TRIGGER_AFTER_SWAP: &str = "HX-Trigger-After-Swap"; diff --git a/src/lib.rs b/src/lib.rs index c699443..54acb18 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ #![forbid(unsafe_code)] +pub mod headers; pub mod request; pub mod response; +pub use headers::*; pub use request::*; pub use response::*; diff --git a/src/request.rs b/src/request.rs index 134895b..afb36c5 100644 --- a/src/request.rs +++ b/src/request.rs @@ -1,47 +1,9 @@ use axum::{extract::FromRequestParts, http::request::Parts}; -/// Represents all of the headers that can be sent in a request to the server. -/// -/// See for more information. -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum HtmxRequestHeader { - /// Indicates that the request is via an element using `hx-boost` attribute. - /// - /// See 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 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", - } - } -} +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. @@ -61,10 +23,7 @@ where type Rejection = std::convert::Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { - if parts - .headers - .contains_key(HtmxRequestHeader::Boosted.as_str()) - { + if parts.headers.contains_key(HX_BOOSTED) { return Ok(HxBoosted(true)); } else { return Ok(HxBoosted(false)); @@ -83,7 +42,7 @@ where type Rejection = std::convert::Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { - 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() { return Ok(HxCurrentUrl(url.to_string())); } @@ -103,10 +62,7 @@ where type Rejection = std::convert::Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { - if parts - .headers - .contains_key(HtmxRequestHeader::HistoryRestoreRequest.as_str()) - { + if parts.headers.contains_key(HX_HISTORY_RESTORE_REQUEST) { return Ok(HxHistoryRestoreRequest(true)); } else { return Ok(HxHistoryRestoreRequest(false)); @@ -125,7 +81,7 @@ where 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 Some(prompt) = parts.headers.get(HX_PROMPT) { if let Ok(prompt) = prompt.to_str() { return Ok(HxPrompt(Some(prompt.to_string()))); } @@ -161,7 +117,7 @@ where type Rejection = std::convert::Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { - 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() { return Ok(HxTarget(Some(target.to_string()))); } @@ -182,7 +138,7 @@ where type Rejection = std::convert::Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { - 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() { return Ok(HxTriggerName(Some(trigger_name.to_string()))); } @@ -203,7 +159,7 @@ where type Rejection = std::convert::Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { - 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() { return Ok(HxTrigger(Some(trigger.to_string()))); } diff --git a/src/response.rs b/src/response.rs index 5cf8757..8b13789 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,58 +1 @@ -/// Represents all of the headers that can be sent in a response to the client. -/// -/// See 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 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 for more information. - Trigger, - /// Allows you to trigger client-side events. - /// - /// See for more information. - TriggerAfterSettle, - /// Allows you to trigger client-side events. - /// - /// See 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", - } - } -}