1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
use std::future::Future;
/// Checks a request synchronously
pub trait Predicate<Request, B> {
/// The type of requests returned by [`check`](Predicate::check).
///
/// This request is forwarded to the inner service if the predicate
/// succeeds.
type Request;
/// The type of response return by [`check`](Predicate::check) if the predicate failed.
type Response;
/// Check whether the given request should be forwarded.
///
/// If the future resolves with [`Ok`], the request is forwarded to the inner service.
fn check(&mut self, request: Request) -> Result<Self::Request, Self::Response>;
}
impl<T, Req, Res, B, F> Predicate<T, B> for F
where
F: FnMut(T) -> Result<Req, Res>,
{
type Request = Req;
type Response = Res;
fn check(&mut self, request: T) -> Result<Self::Request, Self::Response> {
self(request)
}
}
/// Checks a request asynchronously
pub trait AsyncPredicate<Request, B> {
/// The type of requests returned by [`check`](AsyncPredicate::check)
///
/// Thies request is forwarded to the inner service if the predicate
/// succeeds.
type Request;
/// The type of response return by [`check`](AsyncPredicate::check) if the predicate failed.
type Response;
/// The future returned by [`check`](AsyncPredicate::check)
type Future: Future<Output = Result<Self::Request, Self::Response>>;
/// Check whether the given request should be forwarded.
///
/// If the future resolves with [`Ok`], the request is forwarded to the inner service.
fn check(&mut self, request: Request) -> Self::Future;
}
impl<T, Req, Res, B, U, F> AsyncPredicate<T, B> for F
where
F: FnMut(T) -> U,
U: Future<Output = Result<Req, Res>>,
{
type Request = Req;
type Response = Res;
type Future = U;
fn check(&mut self, request: T) -> Self::Future {
self(request)
}
}