diff options
| author | Fuwn <[email protected]> | 2023-04-03 03:01:20 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-04-03 03:01:20 +0000 |
| commit | 9dcb9e8651c2d2cde8854277c9dcb96a4f1af7b9 (patch) | |
| tree | e5c41a2b7e4258d3a9b24c800d9b87649f38581d | |
| parent | refactor(handler): move all responses to traits (diff) | |
| download | windmark-9dcb9e8651c2d2cde8854277c9dcb96a4f1af7b9.tar.xz windmark-9dcb9e8651c2d2cde8854277c9dcb96a4f1af7b9.zip | |
refactor(handler): move traits
| -rw-r--r-- | src/handler.rs | 44 | ||||
| -rw-r--r-- | src/router.rs | 16 |
2 files changed, 19 insertions, 41 deletions
diff --git a/src/handler.rs b/src/handler.rs index 88dafc8..63c353c 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -16,40 +16,12 @@ // Copyright (C) 2022-2022 Fuwn <[email protected]> // SPDX-License-Identifier: GPL-3.0-only -use crate::{ - response::Response, - returnable, - returnable::{CallbackContext, RouteContext}, +mod hooks; +mod partial; +mod response; + +pub use self::{ + hooks::{PostRouteCallback, PreRouteCallback}, + partial::Partial, + response::{ErrorResponse, RouteResponse}, }; - -pub trait RouteResponse: - FnMut(RouteContext<'_>) -> Response + Send + Sync -{ -} - -impl<T> RouteResponse for T where T: FnMut(RouteContext<'_>) -> 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 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 4d52c01..5c1fcfa 100644 --- a/src/router.rs +++ b/src/router.rs @@ -29,7 +29,13 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt}; use url::Url; use crate::{ - handler::{Callback, CleanupCallback, ErrorResponse, Partial, RouteResponse}, + handler::{ + ErrorResponse, + Partial, + PostRouteCallback, + PreRouteCallback, + RouteResponse, + }, module::Module, response::Response, returnable::{CallbackContext, ErrorContext, RouteContext}, @@ -65,8 +71,8 @@ pub struct Router { ssl_acceptor: Arc<SslAcceptor>, #[cfg(feature = "logger")] default_logger: bool, - pre_route_callback: Arc<Mutex<Box<dyn Callback>>>, - post_route_callback: Arc<Mutex<Box<dyn CleanupCallback>>>, + pre_route_callback: Arc<Mutex<Box<dyn PreRouteCallback>>>, + post_route_callback: Arc<Mutex<Box<dyn PostRouteCallback>>>, character_set: String, languages: Vec<String>, port: i32, @@ -553,7 +559,7 @@ impl Router { /// ``` pub fn set_pre_route_callback( &mut self, - callback: impl Callback + 'static, + callback: impl PreRouteCallback + 'static, ) -> &mut Self { self.pre_route_callback = Arc::new(Mutex::new(Box::new(callback))); @@ -576,7 +582,7 @@ impl Router { /// ``` pub fn set_post_route_callback( &mut self, - callback: impl CleanupCallback + 'static, + callback: impl PostRouteCallback + 'static, ) -> &mut Self { self.post_route_callback = Arc::new(Mutex::new(Box::new(callback))); |