diff options
| -rw-r--r-- | examples/windmark.rs | 16 | ||||
| -rw-r--r-- | src/handler.rs | 19 | ||||
| -rw-r--r-- | src/router.rs | 43 |
3 files changed, 46 insertions, 32 deletions
diff --git a/examples/windmark.rs b/examples/windmark.rs index afa6614..e10c343 100644 --- a/examples/windmark.rs +++ b/examples/windmark.rs @@ -77,14 +77,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { r.mount("/module", success!("This is a module!")); }); router.attach(Clicker::default()); - router.set_pre_route_callback(Box::new(|context| { + router.set_pre_route_callback(|context| { info!( "accepted connection from {} to {}", context.tcp.peer_addr().unwrap().ip(), context.url.to_string() ) - })); - router.set_post_route_callback(Box::new(|context, content| { + }); + router.set_post_route_callback(|context, content| { content.content = content.content.replace("Welcome!", "Welcome to Windmark!"); @@ -92,12 +92,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { "closed connection from {}", context.tcp.peer_addr().unwrap().ip() ) - })); - router.add_header(Box::new(|_| "```\nART IS COOL\n```\nhi".to_string())); - router.add_footer(Box::new(|_| "Copyright 2022".to_string())); - router.add_footer(Box::new(|context| { + }); + router.add_header(|_| "```\nART IS COOL\n```\nhi".to_string()); + router.add_footer(|_| "Copyright 2022".to_string()); + router.add_footer(|context| { format!("Another footer, but lower! (from {})", context.url.path()) - })); + }); router.mount( "/", success!("# INDEX\n\nWelcome!\n\n=> /test Test Page\n=> /time Unix Epoch"), 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<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>; -pub type Partial = Box<dyn FnMut(RouteContext<'_>) -> String + Send + Sync>; +pub trait Callback: FnMut(CallbackContext<'_>) + Send + Sync {} + +impl<T> Callback for T where T: FnMut(CallbackContext<'_>) + Send + Sync {} + +pub trait CleanupCallback: + FnMut(CallbackContext<'_>, &mut Response) + Send + Sync +{ +} + +impl<T> CleanupCallback for T where T: FnMut(CallbackContext<'_>, &mut Response) + Send + Sync +{} + +pub trait Partial: FnMut(RouteContext<'_>) -> String + Send + Sync {} + +impl<T> 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<Mutex<Box<dyn ErrorResponse>>>, private_key_file_name: String, ca_file_name: String, - headers: Arc<Mutex<Vec<Partial>>>, - footers: Arc<Mutex<Vec<Partial>>>, + headers: Arc<Mutex<Vec<Box<dyn Partial>>>>, + footers: Arc<Mutex<Vec<Box<dyn Partial>>>>, ssl_acceptor: Arc<SslAcceptor>, #[cfg(feature = "logger")] default_logger: bool, - pre_route_callback: Arc<Mutex<Callback>>, - post_route_callback: Arc<Mutex<CleanupCallback>>, + pre_route_callback: Arc<Mutex<Box<dyn Callback>>>, + post_route_callback: Arc<Mutex<Box<dyn CleanupCallback>>>, character_set: String, languages: Vec<String>, 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 } |