From 50abf6e3c8951e114ba50d658464c8498160abc0 Mon Sep 17 00:00:00 2001 From: Suhail Malik Date: Sun, 30 Oct 2022 12:42:08 +0530 Subject: [PATCH] update code with renaming some types --- src/routers/middleware_router.rs | 15 ++++++--------- src/routers/types.rs | 10 +++++----- src/server.rs | 8 ++++---- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/routers/middleware_router.rs b/src/routers/middleware_router.rs index 53199869f..c2e06d452 100644 --- a/src/routers/middleware_router.rs +++ b/src/routers/middleware_router.rs @@ -9,7 +9,7 @@ use matchit::Node; use anyhow::{bail, Error, Result}; -use super::types::MiddlewareRouteType; +use crate::routers::types::MiddlewareRoute; /// Contains the thread safe hashmaps of different routes @@ -27,13 +27,10 @@ impl MiddlewareRouter { } #[inline] - fn get_relevant_map( - &self, - route: MiddlewareRouteType, - ) -> Option<&RwLock>> { + fn get_relevant_map(&self, route: MiddlewareRoute) -> Option<&RwLock>> { match route { - MiddlewareRouteType::BeforeRequest => Some(&self.before_request), - MiddlewareRouteType::AfterRequest => Some(&self.after_request), + MiddlewareRoute::BeforeRequest => Some(&self.before_request), + MiddlewareRoute::AfterRequest => Some(&self.after_request), } } @@ -41,7 +38,7 @@ impl MiddlewareRouter { // Inserts them in the router according to their nature(CoRoutine/SyncFunction) pub fn add_route( &self, - route_type: MiddlewareRouteType, // we can just have route type as WS + route_type: MiddlewareRoute, route: &str, handler: Py, is_async: bool, @@ -68,7 +65,7 @@ impl MiddlewareRouter { pub fn get_route( &self, - route_method: MiddlewareRouteType, + route_method: MiddlewareRoute, route: &str, // check for the route method here ) -> Option<((PyFunction, u8), HashMap)> { // need to split this function in multiple smaller functions diff --git a/src/routers/types.rs b/src/routers/types.rs index c49774dd9..6d1da332e 100644 --- a/src/routers/types.rs +++ b/src/routers/types.rs @@ -1,14 +1,14 @@ #[derive(Debug)] -pub enum MiddlewareRouteType { +pub enum MiddlewareRoute { BeforeRequest, AfterRequest, } -impl MiddlewareRouteType { - pub fn from_str(input: &str) -> MiddlewareRouteType { +impl MiddlewareRoute { + pub fn from_str(input: &str) -> MiddlewareRoute { match input { - "BEFORE_REQUEST" => MiddlewareRouteType::BeforeRequest, - "AFTER_REQUEST" => MiddlewareRouteType::AfterRequest, + "BEFORE_REQUEST" => MiddlewareRoute::BeforeRequest, + "AFTER_REQUEST" => MiddlewareRoute::AfterRequest, _ => panic!("Invalid route type enum."), } } diff --git a/src/server.rs b/src/server.rs index 5a2a85fa5..a24415434 100644 --- a/src/server.rs +++ b/src/server.rs @@ -5,7 +5,7 @@ use crate::request_handler::{handle_http_middleware_request, handle_http_request use crate::routers::const_router::ConstRouter; use crate::routers::router::Router; -use crate::routers::types::MiddlewareRouteType; +use crate::routers::types::MiddlewareRoute; use crate::routers::{middleware_router::MiddlewareRouter, web_socket_router::WebSocketRouter}; use crate::shared_socket::SocketHeld; use crate::types::{Headers, PyFunction}; @@ -303,7 +303,7 @@ impl Server { ) { debug!("MiddleWare Route added for {} {} ", route_type, route); - let route_type = MiddlewareRouteType::from_str(route_type); + let route_type = MiddlewareRoute::from_str(route_type); self.middleware_router .add_route(route_type, route, handler, is_async, number_of_params) @@ -399,7 +399,7 @@ async fn index( // need a better name for this let tuple_params = - match middleware_router.get_route(MiddlewareRouteType::BeforeRequest, req.uri().path()) { + match middleware_router.get_route(MiddlewareRoute::BeforeRequest, req.uri().path()) { Some(((handler_function, number_of_params), route_params)) => { let x = handle_http_middleware_request( handler_function, @@ -461,7 +461,7 @@ async fn index( }; if let Some(((handler_function, number_of_params), route_params)) = - middleware_router.get_route(MiddlewareRouteType::AfterRequest, req.uri().path()) + middleware_router.get_route(MiddlewareRoute::AfterRequest, req.uri().path()) { let x = handle_http_middleware_request( handler_function,