based/src/request/context.rs

28 lines
744 B
Rust
Raw Normal View History

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
}