2023-09-26 05:48:05 +00:00
|
|
|
use axum::response::{IntoResponse, Response};
|
2023-09-27 03:36:31 +00:00
|
|
|
use headers::{self, Header};
|
2023-09-26 05:48:05 +00:00
|
|
|
use http::header::{HeaderName, HeaderValue};
|
|
|
|
use http::StatusCode;
|
|
|
|
|
2023-10-06 21:50:08 +00:00
|
|
|
#[allow(clippy::declare_interior_mutable_const)]
|
|
|
|
pub const HX_REDIRECT: HeaderName = HeaderName::from_static("hx-redirect");
|
2023-09-26 05:48:05 +00:00
|
|
|
#[allow(clippy::declare_interior_mutable_const)]
|
2023-09-27 03:36:31 +00:00
|
|
|
pub const HX_LOCATION: HeaderName = HeaderName::from_static("hx-location");
|
|
|
|
#[allow(clippy::declare_interior_mutable_const)]
|
2023-09-27 05:28:53 +00:00
|
|
|
pub const HX_REQUEST: HeaderName = HeaderName::from_static("hx-request");
|
|
|
|
#[allow(clippy::declare_interior_mutable_const)]
|
|
|
|
pub const HX_TARGET: HeaderName = HeaderName::from_static("hx-target");
|
2023-09-26 05:48:05 +00:00
|
|
|
|
|
|
|
/// Sets the HX-Location header so that HTMX redirects to the given URI. Unlike
|
|
|
|
/// axum::response::Redirect this does not return a 300-level status code (instead, a 200 status
|
|
|
|
/// code) so that HTMX can see the HX-Location header before the browser handles the redirect.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct HXRedirect {
|
|
|
|
location: HeaderValue,
|
2023-10-06 21:50:08 +00:00
|
|
|
reload: bool,
|
2023-09-26 05:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HXRedirect {
|
|
|
|
pub fn to(uri: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
location: HeaderValue::try_from(uri).expect("URI isn't a valid header value"),
|
2023-10-06 21:50:08 +00:00
|
|
|
reload: false,
|
2023-09-26 05:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-06 21:50:08 +00:00
|
|
|
|
|
|
|
pub fn reload(mut self, reload: bool) -> Self {
|
|
|
|
self.reload = reload;
|
|
|
|
self
|
|
|
|
}
|
2023-09-26 05:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoResponse for HXRedirect {
|
|
|
|
fn into_response(self) -> Response {
|
2023-10-06 21:50:08 +00:00
|
|
|
if self.reload {
|
|
|
|
(
|
|
|
|
StatusCode::OK,
|
|
|
|
[(HX_REDIRECT, self.location)],
|
|
|
|
)
|
|
|
|
.into_response()
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
StatusCode::OK,
|
|
|
|
[(HX_LOCATION, self.location)],
|
|
|
|
)
|
|
|
|
.into_response()
|
|
|
|
}
|
2023-09-26 05:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-27 03:36:31 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-09-27 05:28:53 +00:00
|
|
|
pub struct HXRequest(bool);
|
2023-09-27 03:36:31 +00:00
|
|
|
|
2023-09-27 05:28:53 +00:00
|
|
|
impl HXRequest {
|
|
|
|
pub fn is_true(&self) -> bool {
|
2023-09-27 03:36:31 +00:00
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-27 05:28:53 +00:00
|
|
|
impl Header for HXRequest {
|
2023-09-27 03:36:31 +00:00
|
|
|
fn name() -> &'static HeaderName {
|
2023-09-27 05:28:53 +00:00
|
|
|
static HX_REQUEST_STATIC: HeaderName = HX_REQUEST;
|
|
|
|
&HX_REQUEST_STATIC
|
2023-09-27 03:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn decode<'i, I>(values: &mut I) -> Result<Self, headers::Error>
|
|
|
|
where
|
|
|
|
I: Iterator<Item = &'i HeaderValue>,
|
|
|
|
{
|
|
|
|
let value = values
|
|
|
|
.next()
|
|
|
|
.ok_or_else(headers::Error::invalid)?;
|
|
|
|
|
|
|
|
if value == "true" {
|
2023-09-27 05:28:53 +00:00
|
|
|
Ok(HXRequest(true))
|
2023-09-27 03:36:31 +00:00
|
|
|
} else if value == "false" {
|
2023-09-27 05:28:53 +00:00
|
|
|
Ok(HXRequest(false))
|
2023-09-27 03:36:31 +00:00
|
|
|
} else {
|
|
|
|
Err(headers::Error::invalid())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encode<E>(&self, values: &mut E)
|
|
|
|
where
|
|
|
|
E: Extend<HeaderValue>,
|
|
|
|
{
|
|
|
|
let s = if self.0 {
|
|
|
|
"true"
|
|
|
|
} else {
|
|
|
|
"false"
|
|
|
|
};
|
|
|
|
|
|
|
|
let value = HeaderValue::from_static(s);
|
|
|
|
|
|
|
|
values.extend(std::iter::once(value));
|
|
|
|
}
|
|
|
|
}
|
2023-09-27 05:28:53 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct HXTarget {
|
|
|
|
pub target: HeaderValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Header for HXTarget {
|
|
|
|
fn name() -> &'static HeaderName {
|
|
|
|
static HX_TARGET_STATIC: HeaderName = HX_TARGET;
|
|
|
|
&HX_TARGET_STATIC
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decode<'i, I>(values: &mut I) -> Result<Self, headers::Error>
|
|
|
|
where
|
|
|
|
I: Iterator<Item = &'i HeaderValue>,
|
|
|
|
{
|
|
|
|
let value = values
|
|
|
|
.next()
|
|
|
|
.ok_or_else(headers::Error::invalid)?;
|
|
|
|
|
|
|
|
Ok(HXTarget{ target: value.clone() })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encode<E>(&self, values: &mut E)
|
|
|
|
where
|
|
|
|
E: Extend<HeaderValue>,
|
|
|
|
{
|
|
|
|
|
|
|
|
values.extend(std::iter::once(self.target.clone()));
|
|
|
|
}
|
|
|
|
}
|