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

Add none filter #736

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod log;
pub mod method;
#[cfg(feature = "multipart")]
pub mod multipart;
pub mod none;
pub mod path;
pub mod query;
pub mod reply;
Expand Down
41 changes: 41 additions & 0 deletions src/filters/none.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//! A filter that matches no route.
use futures::future;

use crate::filter::{filter_fn, Filter};
use crate::reject::{self, Rejection};

/// A filter that matches no route.
///
/// This can be useful to help with styling.
///
/// # Example
///
/// ```
/// use warp::Filter;
///
/// let route_1 = warp::path!("something").map(|| "Hello, world!".to_string());
/// let route_2 = warp::path!("something2").map(|| "Hello, world again!".to_string());
/// let route_3 = warp::path!("something3").map(|| "Hello, world again again!".to_string());
///
/// let routes = warp::none()
/// .or(route_1)
/// .or(route_2)
/// .or(route_3);
/// ```
///
/// looks nicer than the following because the routes are lined up
/// ```
/// use warp::Filter;
///
/// let route_1 = warp::path!("something").map(|| "Hello, world!".to_string());
/// let route_2 = warp::path!("something2").map(|| "Hello, world again!".to_string());
/// let route_3 = warp::path!("something3").map(|| "Hello, world again again!".to_string());
///
/// let routes = route_1
/// .or(route_2)
/// .or(route_3);
/// ```
pub fn none() -> impl Filter<Extract = (), Error = Rejection> + Copy {
// always reject with not found
filter_fn(|_route| future::ready(Err(reject::not_found())))
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ pub use self::filters::{
// log() function
log::log,
method::{delete, get, head, method, options, patch, post, put},
// none() function
none::none,
path,
// path() function and macro
path::path,
Expand Down
26 changes: 26 additions & 0 deletions tests/none.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![deny(warnings)]
use warp::Filter;

#[tokio::test]
async fn none_is_not_found() {
let filter = warp::none();

// none should return not found for request
let req = warp::test::request();
assert!(req.filter(&filter).await.unwrap_err().is_not_found());
}

#[tokio::test]
async fn none_can_be_chained() {
let req = warp::test::request();
let filter = warp::none();

// should not match anything
assert!(!req.matches(&filter).await);

let req = warp::test::request();
let filter = filter.or(warp::get().map(warp::reply));

// this should now match because we chained the get with 'or'
assert!(req.matches(&filter).await);
}