24 lines
677 B
Rust
24 lines
677 B
Rust
use rocket::{
|
|
Request,
|
|
request::{self, FromRequest},
|
|
};
|
|
|
|
/// Represents contextual information about an HTTP request.
|
|
#[derive(Default)]
|
|
pub struct RequestContext {
|
|
/// 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,
|
|
}
|
|
|
|
#[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").next().is_some(),
|
|
})
|
|
}
|
|
}
|