mirror of
https://github.com/robertwayne/axum-htmx
synced 2024-12-28 19:29:33 +01:00
Minor documentation / code cleanup
This commit is contained in:
parent
6eea312d94
commit
d551c4cd8e
2 changed files with 69 additions and 31 deletions
|
@ -21,9 +21,13 @@ const HX_SWAP_NONE: &str = "none";
|
|||
|
||||
/// The `HX-Location` header.
|
||||
///
|
||||
/// This response header can be used to trigger a client side redirection without reloading the whole page.
|
||||
/// This response header can be used to trigger a client side redirection
|
||||
/// without reloading the whole page.
|
||||
///
|
||||
/// Will fail if the supplied Uri contains characters that are not visible ASCII (32-127).
|
||||
/// Will fail if the supplied Uri contains characters that are not visible ASCII
|
||||
/// (32-127).
|
||||
///
|
||||
/// See <https://htmx.org/headers/hx-location/> for more information.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HxLocation(pub Uri);
|
||||
|
||||
|
@ -35,6 +39,7 @@ impl IntoResponseParts for HxLocation {
|
|||
headers::HX_LOCATION,
|
||||
HeaderValue::from_maybe_shared(self.0.to_string())?,
|
||||
);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
@ -43,7 +48,8 @@ impl IntoResponseParts for HxLocation {
|
|||
///
|
||||
/// Pushes a new url into the history stack.
|
||||
///
|
||||
/// Will fail if the supplied Uri contains characters that are not visible ASCII (32-127).
|
||||
/// Will fail if the supplied Uri contains characters that are not visible ASCII
|
||||
/// (32-127).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HxPushUrl(pub Uri);
|
||||
|
||||
|
@ -55,6 +61,7 @@ impl IntoResponseParts for HxPushUrl {
|
|||
headers::HX_PUSH_URL,
|
||||
HeaderValue::from_maybe_shared(self.0.to_string())?,
|
||||
);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
@ -63,7 +70,8 @@ impl IntoResponseParts for HxPushUrl {
|
|||
///
|
||||
/// Can be used to do a client-side redirect to a new location.
|
||||
///
|
||||
/// Will fail if the supplied Uri contains characters that are not visible ASCII (32-127).
|
||||
/// Will fail if the supplied Uri contains characters that are not visible ASCII
|
||||
/// (32-127).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HxRedirect(pub Uri);
|
||||
|
||||
|
@ -75,6 +83,7 @@ impl IntoResponseParts for HxRedirect {
|
|||
headers::HX_REDIRECT,
|
||||
HeaderValue::from_maybe_shared(self.0.to_string())?,
|
||||
);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
@ -98,6 +107,7 @@ impl IntoResponseParts for HxRefresh {
|
|||
false => HeaderValue::from_static("false"),
|
||||
},
|
||||
);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
@ -106,7 +116,8 @@ impl IntoResponseParts for HxRefresh {
|
|||
///
|
||||
/// Replaces the currelt URL in the location bar.
|
||||
///
|
||||
/// Will fail if the supplied Uri contains characters that are not visible ASCII (32-127).
|
||||
/// Will fail if the supplied Uri contains characters that are not visible ASCII
|
||||
/// (32-127).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HxReplaceUrl(pub Uri);
|
||||
|
||||
|
@ -118,6 +129,7 @@ impl IntoResponseParts for HxReplaceUrl {
|
|||
headers::HX_REPLACE_URL,
|
||||
HeaderValue::from_maybe_shared(self.0.to_string())?,
|
||||
);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
@ -135,15 +147,18 @@ impl IntoResponseParts for HxReswap {
|
|||
|
||||
fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> {
|
||||
res.headers_mut().insert(headers::HX_RESWAP, self.0.into());
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
/// The `HX-Retarget` header.
|
||||
///
|
||||
/// A CSS selector that updates the target of the content update to a different element on the page.
|
||||
/// A CSS selector that updates the target of the content update to a different
|
||||
/// element on the page.
|
||||
///
|
||||
/// Will fail if the supplied String contains characters that are not visible ASCII (32-127).
|
||||
/// Will fail if the supplied String contains characters that are not visible
|
||||
/// ASCII (32-127).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HxRetarget(pub String);
|
||||
|
||||
|
@ -155,15 +170,18 @@ impl IntoResponseParts for HxRetarget {
|
|||
headers::HX_RETARGET,
|
||||
HeaderValue::from_maybe_shared(self.0)?,
|
||||
);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
/// The `HX-Reselect` header.
|
||||
///
|
||||
/// 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.
|
||||
/// 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.
|
||||
///
|
||||
/// Will fail if the supplied String contains characters that are not visible ASCII (32-127).
|
||||
/// Will fail if the supplied String contains characters that are not visible
|
||||
/// ASCII (32-127).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HxReselect(pub String);
|
||||
|
||||
|
@ -175,6 +193,7 @@ impl IntoResponseParts for HxReselect {
|
|||
headers::HX_RESELECT,
|
||||
HeaderValue::from_maybe_shared(self.0)?,
|
||||
);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
@ -183,7 +202,8 @@ impl IntoResponseParts for HxReselect {
|
|||
///
|
||||
/// Allows you to trigger client-side events.
|
||||
///
|
||||
/// Will fail if the supplied events contain or produce characters that are not visible ASCII (32-127) when serializing to json.
|
||||
/// Will fail if the supplied events contain or produce characters that are not
|
||||
/// visible ASCII (32-127) when serializing to json.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HxResponseTrigger(pub Vec<String>);
|
||||
|
||||
|
@ -200,6 +220,7 @@ impl IntoResponseParts for HxResponseTrigger {
|
|||
.unwrap_or_default(),
|
||||
)?,
|
||||
);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
@ -208,7 +229,8 @@ impl IntoResponseParts for HxResponseTrigger {
|
|||
///
|
||||
/// Allows you to trigger client-side events after the settle step.
|
||||
///
|
||||
/// Will fail if the supplied events contain or produce characters that are not visible ASCII (32-127) when serializing to json.
|
||||
/// Will fail if the supplied events contain or produce characters that are not
|
||||
/// visible ASCII (32-127) when serializing to json.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HxResponseTriggerAfterSettle(pub Vec<String>);
|
||||
|
||||
|
@ -225,6 +247,7 @@ impl IntoResponseParts for HxResponseTriggerAfterSettle {
|
|||
.unwrap_or_default(),
|
||||
)?,
|
||||
);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
@ -233,7 +256,8 @@ impl IntoResponseParts for HxResponseTriggerAfterSettle {
|
|||
///
|
||||
/// Allows you to trigger client-side events after the swap step.
|
||||
///
|
||||
/// Will fail if the supplied events contain or produce characters that are not visible ASCII (32-127) when serializing to json.
|
||||
/// Will fail if the supplied events contain or produce characters that are not
|
||||
/// visible ASCII (32-127) when serializing to json.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HxResponseTriggerAfterSwap(pub Vec<String>);
|
||||
|
||||
|
@ -250,6 +274,7 @@ impl IntoResponseParts for HxResponseTriggerAfterSwap {
|
|||
.unwrap_or_default(),
|
||||
)?,
|
||||
);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
@ -272,7 +297,8 @@ pub enum SwapOption {
|
|||
AfterEnd,
|
||||
/// Deletes the target element regardless of the response
|
||||
Delete,
|
||||
/// Does not append content from response (out of band items will still be processed).
|
||||
/// Does not append content from response (out of band items will still be
|
||||
/// processed).
|
||||
None,
|
||||
}
|
||||
|
||||
|
|
|
@ -18,26 +18,30 @@ use crate::{
|
|||
|
||||
/// The `HX-Location` header.
|
||||
///
|
||||
/// This response header can be used to trigger a client side redirection without reloading the whole page. Instead of changing the page’s location it will act like following a hx-boost link, creating a new history entry, issuing an ajax request to the value of the header and pushing the path into history.
|
||||
/// This response header can be used to trigger a client side redirection
|
||||
/// without reloading the whole page.
|
||||
///
|
||||
/// Will fail if the supplied data contains or produces characters that are not visible ASCII (32-127) when serializing to json.
|
||||
/// Will fail if the supplied data contains or produces characters that are not
|
||||
/// visible ASCII (32-127) when serializing to JSON.
|
||||
///
|
||||
/// See <https://htmx.org/headers/hx-location/> for more information.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct HxLocation {
|
||||
/// Url to load the response from
|
||||
/// Url to load the response from.
|
||||
pub path: String,
|
||||
/// The source element of the request
|
||||
/// The source element of the request.
|
||||
pub source: Option<String>,
|
||||
/// An event that "triggered" the request
|
||||
/// An event that "triggered" the request.
|
||||
pub event: Option<String>,
|
||||
/// A callback that will handle the response HTML
|
||||
/// A callback that will handle the response HTML.
|
||||
pub handler: Option<String>,
|
||||
/// The target to swap the response into
|
||||
/// The target to swap the response into.
|
||||
pub target: Option<String>,
|
||||
/// How the response will be swapped in relative to the target
|
||||
/// How the response will be swapped in relative to the target.
|
||||
pub swap: Option<SwapOption>,
|
||||
/// Values to submit with the request
|
||||
/// Values to submit with the request.
|
||||
pub values: Option<Value>,
|
||||
/// headers to submit with the request
|
||||
/// Headers to submit with the request.
|
||||
pub headers: Option<Value>,
|
||||
}
|
||||
|
||||
|
@ -74,6 +78,7 @@ impl IntoResponseParts for HxLocation {
|
|||
};
|
||||
|
||||
res.headers_mut().insert(headers::HX_LOCATION, header_value);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
@ -82,7 +87,8 @@ impl IntoResponseParts for HxLocation {
|
|||
///
|
||||
/// Allows you to trigger client-side events.
|
||||
///
|
||||
/// Will fail if the supplied events contain or produce characters that are not visible ASCII (32-127) when serializing to json.
|
||||
/// Will fail if the supplied events contain or produce characters that are not
|
||||
/// visible ASCII (32-127) when serializing to json.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HxTrigger(pub Vec<HxEvent>);
|
||||
|
||||
|
@ -92,6 +98,7 @@ impl IntoResponseParts for HxTrigger {
|
|||
fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> {
|
||||
res.headers_mut()
|
||||
.insert(headers::HX_TRIGGER, events_to_header_value(self.0)?);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
@ -100,7 +107,8 @@ impl IntoResponseParts for HxTrigger {
|
|||
///
|
||||
/// Allows you to trigger client-side events after the settle step.
|
||||
///
|
||||
/// Will fail if the supplied events contain or produce characters that are not visible ASCII (32-127) when serializing to json.
|
||||
/// Will fail if the supplied events contain or produce characters that are not
|
||||
/// visible ASCII (32-127) when serializing to json.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HxTriggerAfterSettle(Vec<HxEvent>);
|
||||
|
||||
|
@ -112,6 +120,7 @@ impl IntoResponseParts for HxTriggerAfterSettle {
|
|||
headers::HX_TRIGGER_AFTER_SETTLE,
|
||||
events_to_header_value(self.0)?,
|
||||
);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
@ -120,7 +129,8 @@ impl IntoResponseParts for HxTriggerAfterSettle {
|
|||
///
|
||||
/// Allows you to trigger client-side events after the swap step.
|
||||
///
|
||||
/// Will fail if the supplied events contain or produce characters that are not visible ASCII (32-127) when serializing to json.
|
||||
/// Will fail if the supplied events contain or produce characters that are not
|
||||
/// visible ASCII (32-127) when serializing to json.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HxTriggerAfterSwap(Vec<HxEvent>);
|
||||
|
||||
|
@ -132,6 +142,7 @@ impl IntoResponseParts for HxTriggerAfterSwap {
|
|||
headers::HX_TRIGGER_AFTER_SWAP,
|
||||
events_to_header_value(self.0)?,
|
||||
);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
@ -160,15 +171,16 @@ pub(crate) fn events_to_header_value(events: Vec<HxEvent>) -> Result<HeaderValue
|
|||
let with_data = events.iter().any(|e| e.data.is_some());
|
||||
|
||||
let header_value = if with_data {
|
||||
// at least one event contains data so the header_value needs to be json encoded.
|
||||
// at least one event contains data so the header_value needs to be json
|
||||
// encoded.
|
||||
let header_value = events
|
||||
.into_iter()
|
||||
.map(|e| (e.name, e.data.map(|d| d.to_string()).unwrap_or_default()))
|
||||
.collect::<HashMap<_, _>>();
|
||||
serde_json::to_string(&header_value)?
|
||||
} else {
|
||||
// no event contains data, the event names can be put in the header value separated
|
||||
// by a comma.
|
||||
// no event contains data, the event names can be put in the header
|
||||
// value separated by a comma.
|
||||
events
|
||||
.into_iter()
|
||||
.map(|e| e.name)
|
||||
|
@ -179,8 +191,8 @@ pub(crate) fn events_to_header_value(events: Vec<HxEvent>) -> Result<HeaderValue
|
|||
HeaderValue::from_maybe_shared(header_value).map_err(HxError::from)
|
||||
}
|
||||
|
||||
// can be removed and automatically derived when https://github.com/serde-rs/serde/issues/2485
|
||||
// is implemented
|
||||
// can be removed and automatically derived when
|
||||
// https://github.com/serde-rs/serde/issues/2485 is implemented
|
||||
impl serde::Serialize for SwapOption {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
|
|
Loading…
Reference in a new issue