Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vidhanio opened this issue Oct 21, 2022 · 0 comments
Open
Labels
feature New feature or request

Comments

@vidhanio
Copy link

vidhanio commented Oct 21, 2022

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 vidhanio added the feature New feature or request label Oct 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant