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 65
use crate::BoxError;
use std::future::Future;
/// Checks a request asynchronously.
pub trait AsyncPredicate<Request> {
/// The future returned by [`check`].
///
/// [`check`]: crate::filter::AsyncPredicate::check
type Future: Future<Output = Result<Self::Request, BoxError>>;
/// The type of requests returned by [`check`].
///
/// This request is forwarded to the inner service if the predicate
/// succeeds.
///
/// [`check`]: crate::filter::AsyncPredicate::check
type Request;
/// 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;
}
/// Checks a request synchronously.
pub trait Predicate<Request> {
/// The type of requests returned by [`check`].
///
/// This request is forwarded to the inner service if the predicate
/// succeeds.
///
/// [`check`]: crate::filter::Predicate::check
type Request;
/// 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, BoxError>;
}
impl<F, T, U, R, E> AsyncPredicate<T> for F
where
F: FnMut(T) -> U,
U: Future<Output = Result<R, E>>,
E: Into<BoxError>,
{
type Future = futures_util::future::ErrInto<U, BoxError>;
type Request = R;
fn check(&mut self, request: T) -> Self::Future {
use futures_util::TryFutureExt;
self(request).err_into()
}
}
impl<F, T, R, E> Predicate<T> for F
where
F: FnMut(T) -> Result<R, E>,
E: Into<BoxError>,
{
type Request = R;
fn check(&mut self, request: T) -> Result<Self::Request, BoxError> {
self(request).map_err(Into::into)
}
}