From 0bfd57be84b7cfe11e5181222ba6018e668d63c9 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Mon, 3 Apr 2023 02:52:18 +0000 Subject: refactor(handler): move all responses to traits --- src/handler.rs | 19 +++++++++++++++---- src/router.rs | 43 +++++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/handler.rs b/src/handler.rs index b283cdf..88dafc8 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -38,7 +38,18 @@ pub trait ErrorResponse: impl ErrorResponse for T where T: FnMut(returnable::ErrorContext<'_>) -> Response + Send + Sync {} -pub type Callback = Box) + Send + Sync>; -pub type CleanupCallback = - Box, &mut Response) + Send + Sync>; -pub type Partial = Box) -> String + Send + Sync>; +pub trait Callback: FnMut(CallbackContext<'_>) + Send + Sync {} + +impl Callback for T where T: FnMut(CallbackContext<'_>) + Send + Sync {} + +pub trait CleanupCallback: + FnMut(CallbackContext<'_>, &mut Response) + Send + Sync +{ +} + +impl CleanupCallback for T where T: FnMut(CallbackContext<'_>, &mut Response) + Send + Sync +{} + +pub trait Partial: FnMut(RouteContext<'_>) -> String + Send + Sync {} + +impl Partial for T where T: FnMut(RouteContext<'_>) -> String + Send + Sync {} diff --git a/src/router.rs b/src/router.rs index 0f4af89..4d52c01 100644 --- a/src/router.rs +++ b/src/router.rs @@ -60,13 +60,13 @@ pub struct Router { error_handler: Arc>>, private_key_file_name: String, ca_file_name: String, - headers: Arc>>, - footers: Arc>>, + headers: Arc>>>, + footers: Arc>>>, ssl_acceptor: Arc, #[cfg(feature = "logger")] default_logger: bool, - pre_route_callback: Arc>, - post_route_callback: Arc>, + pre_route_callback: Arc>>, + post_route_callback: Arc>>, character_set: String, languages: Vec, port: i32, @@ -173,12 +173,12 @@ impl Router { /// # Examples /// /// ```rust - /// windmark::Router::new().add_header(Box::new(|context| { + /// windmark::Router::new().add_header(|context| { /// format!("This is displayed at the top of {}!", context.url.path()) - /// })); + /// }); /// ``` - pub fn add_header(&mut self, handler: Partial) -> &mut Self { - (*self.headers.lock().unwrap()).push(handler); + pub fn add_header(&mut self, handler: impl Partial + 'static) -> &mut Self { + (*self.headers.lock().unwrap()).push(Box::new(handler)); self } @@ -192,12 +192,12 @@ impl Router { /// # Examples /// /// ```rust - /// windmark::Router::new().add_footer(Box::new(|context| { + /// windmark::Router::new().add_footer(|context| { /// format!("This is displayed at the bottom of {}!", context.url.path()) - /// })); + /// }); /// ``` - pub fn add_footer(&mut self, handler: Partial) -> &mut Self { - (*self.footers.lock().unwrap()).push(handler); + pub fn add_footer(&mut self, handler: impl Partial + 'static) -> &mut Self { + (*self.footers.lock().unwrap()).push(Box::new(handler)); self } @@ -544,15 +544,18 @@ impl Router { /// ```rust /// use log::info; /// - /// windmark::Router::new().set_pre_route_callback(Box::new(|context| { + /// windmark::Router::new().set_pre_route_callback(|context| { /// info!( /// "accepted connection from {}", /// context.stream.peer_addr().unwrap().ip(), /// ) - /// })); + /// }); /// ``` - pub fn set_pre_route_callback(&mut self, callback: Callback) -> &mut Self { - self.pre_route_callback = Arc::new(Mutex::new(callback)); + pub fn set_pre_route_callback( + &mut self, + callback: impl Callback + 'static, + ) -> &mut Self { + self.pre_route_callback = Arc::new(Mutex::new(Box::new(callback))); self } @@ -564,18 +567,18 @@ impl Router { /// ```rust /// use log::info; /// - /// windmark::Router::new().set_post_route_callback(Box::new(|context, _| { + /// windmark::Router::new().set_post_route_callback(|context, _| { /// info!( /// "closed connection from {}", /// context.stream.peer_addr().unwrap().ip(), /// ) - /// })); + /// }); /// ``` pub fn set_post_route_callback( &mut self, - callback: CleanupCallback, + callback: impl CleanupCallback + 'static, ) -> &mut Self { - self.post_route_callback = Arc::new(Mutex::new(callback)); + self.post_route_callback = Arc::new(Mutex::new(Box::new(callback))); self } -- cgit v1.2.3