diff options
| author | Fuwn <[email protected]> | 2023-04-03 02:47:11 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-04-03 02:47:11 +0000 |
| commit | 79ad55e1e0fe746a635e6814fbf048437c2d50ee (patch) | |
| tree | 12c395e03accf4a4a115b24946760bcae5d0d148 | |
| parent | feat(router): GET RID OF DIRTY BOXES (diff) | |
| download | windmark-79ad55e1e0fe746a635e6814fbf048437c2d50ee.tar.xz windmark-79ad55e1e0fe746a635e6814fbf048437c2d50ee.zip | |
refactor(handler): trait-based response
| -rw-r--r-- | examples/windmark.rs | 4 | ||||
| -rw-r--r-- | src/handler.rs | 10 | ||||
| -rw-r--r-- | src/router.rs | 15 |
3 files changed, 19 insertions, 10 deletions
diff --git a/examples/windmark.rs b/examples/windmark.rs index c139b55..afa6614 100644 --- a/examples/windmark.rs +++ b/examples/windmark.rs @@ -65,13 +65,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { router.set_certificate_file("windmark_public.pem"); #[cfg(feature = "logger")] router.enable_default_logger(true); - router.set_error_handler(Box::new(move |_| { + router.set_error_handler(move |_| { error_count += 1; println!("{} errors so far", error_count); Response::permanent_failure("e") - })); + }); router.set_fix_path(true); router.attach_stateless(|r| { r.mount("/module", success!("This is a module!")); diff --git a/src/handler.rs b/src/handler.rs index f22e3b3..b283cdf 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -30,8 +30,14 @@ pub trait RouteResponse: impl<T> RouteResponse for T where T: FnMut(RouteContext<'_>) -> Response + Send + Sync {} -pub type ErrorResponse = - Box<dyn FnMut(returnable::ErrorContext<'_>) -> Response + Send + Sync>; +pub trait ErrorResponse: + FnMut(returnable::ErrorContext<'_>) -> Response + Send + Sync +{ +} + +impl<T> ErrorResponse for T where T: FnMut(returnable::ErrorContext<'_>) -> Response + Send + Sync +{} + pub type Callback = Box<dyn FnMut(CallbackContext<'_>) + Send + Sync>; pub type CleanupCallback = Box<dyn FnMut(CallbackContext<'_>, &mut Response) + Send + Sync>; diff --git a/src/router.rs b/src/router.rs index 9917923..0f4af89 100644 --- a/src/router.rs +++ b/src/router.rs @@ -57,7 +57,7 @@ macro_rules! or_error { #[derive(Clone)] pub struct Router { routes: matchit::Router<Arc<Mutex<Box<dyn RouteResponse>>>>, - error_handler: Arc<Mutex<ErrorResponse>>, + error_handler: Arc<Mutex<Box<dyn ErrorResponse>>>, private_key_file_name: String, ca_file_name: String, headers: Arc<Mutex<Vec<Partial>>>, @@ -151,12 +151,15 @@ impl Router { /// # Examples /// /// ```rust - /// windmark::Router::new().set_error_handler(Box::new(|_| { - /// windmark::Response::success("You have encountered an error!") - /// })); + /// windmark::Router::new().set_error_handler(|_| { + /// windmark::success!("You have encountered an error!") + /// }); /// ``` - pub fn set_error_handler(&mut self, handler: ErrorResponse) -> &mut Self { - self.error_handler = Arc::new(Mutex::new(handler)); + pub fn set_error_handler( + &mut self, + handler: impl ErrorResponse + 'static, + ) -> &mut Self { + self.error_handler = Arc::new(Mutex::new(Box::new(handler))); self } |