From 6e3ec5d44bc1294a673704b654faea1c2f7b81e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Wed, 15 Jul 2020 00:06:52 +0100 Subject: [PATCH] Add wrap_fn, closes #16 --- examples/wrapping.rs | 31 +++++++++++++++++++++++++++++++ src/filter/mod.rs | 1 + src/filter/wrap.rs | 28 ++++++++++++++++++++++++++++ src/lib.rs | 1 + 4 files changed, 61 insertions(+) create mode 100644 examples/wrapping.rs diff --git a/examples/wrapping.rs b/examples/wrapping.rs new file mode 100644 index 000000000..bfc84f980 --- /dev/null +++ b/examples/wrapping.rs @@ -0,0 +1,31 @@ +#![deny(warnings)] +use warp::Filter; + +fn hello_wrapper( + filter: F, +) -> impl Filter + Clone + Send + Sync + 'static +where + F: Filter + Clone + Send + Sync + 'static, + F::Extract: warp::Reply, +{ + warp::any() + .map(|| { + println!("before filter"); + }) + .untuple_one() + .and(filter) + .map(|_arg| "wrapped hello world") +} + +#[tokio::main] +async fn main() { + // Match any request and return hello world! + let routes = warp::any() + .map(|| "hello world") + .boxed() + .recover(|_err| async { Ok("recovered") }) + // wrap the filter with hello_wrapper + .with(warp::wrap_fn(hello_wrapper)); + + warp::serve(routes).run(([127, 0, 0, 1], 3030)).await; +} diff --git a/src/filter/mod.rs b/src/filter/mod.rs index 55a458d95..851806bb7 100644 --- a/src/filter/mod.rs +++ b/src/filter/mod.rs @@ -30,6 +30,7 @@ use self::or_else::OrElse; use self::recover::Recover; use self::unify::Unify; use self::untuple_one::UntupleOne; +pub use self::wrap::wrap_fn; pub(crate) use self::wrap::{Wrap, WrapSealed}; // A crate-private base trait, allowing the actual `filter` method to change diff --git a/src/filter/wrap.rs b/src/filter/wrap.rs index 2226286e2..377b0ef0e 100644 --- a/src/filter/wrap.rs +++ b/src/filter/wrap.rs @@ -25,3 +25,31 @@ where F: Filter, { } + +/// Function that receives a filter to be combined with pre and after filters +pub fn wrap_fn(func: F) -> WrapFn +where + F: Fn(T) -> U, + T: Filter, + U: Filter, +{ + WrapFn { func } +} + +#[derive(Debug)] +pub struct WrapFn { + func: F, +} + +impl WrapSealed for WrapFn +where + F: Fn(T) -> U, + T: Filter, + U: Filter, +{ + type Wrapped = U; + + fn wrap(&self, filter: T) -> Self::Wrapped { + (self.func)(filter) + } +} diff --git a/src/lib.rs b/src/lib.rs index 65469dc5d..c4c6a016f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,6 +149,7 @@ pub use self::filters::{ trace::trace, }; // ws() function +pub use self::filter::wrap_fn; #[cfg(feature = "websocket")] #[doc(hidden)] pub use self::filters::ws::ws;