rocket_etag_if_none_match/
lib.rspub extern crate entity_tag;
use entity_tag::EntityTag;
use rocket::{
outcome::Outcome,
request::{self, FromRequest, Request},
};
#[derive(Debug, Clone, Default)]
pub struct EtagIfNoneMatch<'r> {
pub etag: Option<EntityTag<'r>>,
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for EtagIfNoneMatch<'r> {
type Error = ();
async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
let raw_etag: Option<&str> = request.headers().get("if-none-match").next(); let etag = raw_etag.map(|raw_etag| EntityTag::from_str(raw_etag).ok()).unwrap_or(None);
Outcome::Success(EtagIfNoneMatch {
etag,
})
}
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for &'r EtagIfNoneMatch<'r> {
type Error = ();
async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
Outcome::Success(request.local_cache(|| {
let raw_etag: Option<&str> = request.headers().get("if-none-match").next(); let etag =
raw_etag.map(|raw_etag| EntityTag::from_string(raw_etag).ok()).unwrap_or(None);
EtagIfNoneMatch {
etag,
}
}))
}
}
impl<'r> EtagIfNoneMatch<'r> {
pub fn weak_eq(&self, other_etag: &EntityTag) -> bool {
match &self.etag {
Some(etag) => etag.weak_eq(other_etag),
None => false,
}
}
pub fn strong_eq(&self, other_etag: &EntityTag) -> bool {
match &self.etag {
Some(etag) => etag.strong_eq(other_etag),
None => false,
}
}
}