warp icon indicating copy to clipboard operation
warp copied to clipboard

`warp::filters::reject_all`, a convenience filter which will always reject.

Open vidhanio opened this issue 2 years ago • 0 comments

Is your feature request related to a problem? Please describe. Writing macros to Filter::or together several filters is harder than it needs to be, as you must use recursion or write your own always-rejecting filter to put at the top.

For example:

macro_rules! filters {
    ($($module:ident),* $(,)?) => {
        pub fn filters() -> impl ::warp::Filter<Extract = (impl ::warp::Reply,), Error = warp::Rejection> + ::std::clone::Clone {
            ::warp::reject_all()
                $(
                    .or($crate::routes::$module::filter())
                )*
            )
        }
    };
}

Describe the solution you'd like A simple filter which is the opposite of warp::any(): warp::reject_all(). This filter will reject anything that hits it, passing it down to then next Filter::or'd filter.

The one I came up with to use on my own is:

fn reject_all<R: warp::Reply>() -> impl Filter<Extract = (R,), Error = warp::Rejection> + Clone {
    warp::any().and_then(|| async { Err(warp::reject::not_found()) })
}

Describe alternatives you've considered Using macro recursion or writing my own filter.

vidhanio avatar Oct 21 '22 21:10 vidhanio