use http::{header, Response};
pub trait Predicate<T>: Copy {
fn check(&mut self, thing: &T) -> bool;
}
#[derive(Copy, Clone, Debug)]
pub struct ContentTypeStartsWith<Patt>(Patt);
impl<Patt: AsRef<str> + Copy> ContentTypeStartsWith<Patt> {
pub fn new(pattern: Patt) -> Self {
ContentTypeStartsWith(pattern)
}
}
impl<T, Patt: AsRef<str> + Copy> Predicate<Response<T>> for ContentTypeStartsWith<Patt> {
fn check(&mut self, response: &Response<T>) -> bool {
response
.headers()
.get(header::CONTENT_TYPE)
.and_then(|val| val.to_str().ok().map(|s| s.starts_with(self.0.as_ref())))
.unwrap_or(false)
}
}
#[derive(Copy, Clone, Debug)]
pub struct Always;
impl<T> Predicate<T> for Always {
fn check(&mut self, _thing: &T) -> bool {
true
}
}
impl<T, F> Predicate<T> for F
where
F: Fn(&T) -> bool + Copy,
{
fn check(&mut self, thing: &T) -> bool {
(self)(thing)
}
}