2024-12-18 14:33:53 +01:00
|
|
|
use rocket::{
|
|
|
|
Request,
|
|
|
|
request::{self, FromRequest},
|
|
|
|
};
|
2024-12-17 23:28:43 +01:00
|
|
|
|
2024-12-18 14:33:53 +01:00
|
|
|
/// Represents contextual information about an HTTP request.
|
2024-12-17 23:28:43 +01:00
|
|
|
pub struct RequestContext {
|
2024-12-18 14:33:53 +01:00
|
|
|
/// A flag indicating if the request is an HTMX request.
|
|
|
|
///
|
|
|
|
/// This is determined by checking the presence of the `HX-Request` header.
|
|
|
|
pub is_htmx: bool,
|
2024-12-17 23:28:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[rocket::async_trait]
|
|
|
|
impl<'r> FromRequest<'r> for RequestContext {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
|
|
|
|
rocket::outcome::Outcome::Success(RequestContext {
|
|
|
|
is_htmx: !req
|
|
|
|
.headers()
|
|
|
|
.get("HX-Request")
|
|
|
|
.collect::<Vec<&str>>()
|
2024-12-18 14:33:53 +01:00
|
|
|
.is_empty(),
|
2024-12-17 23:28:43 +01:00
|
|
|
})
|
|
|
|
}
|
2024-12-18 14:33:53 +01:00
|
|
|
}
|
2024-12-29 20:03:04 +01:00
|
|
|
|
|
|
|
impl Default for RequestContext {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { is_htmx: false }
|
|
|
|
}
|
|
|
|
}
|