1
- use std :: collections :: HashMap ;
1
+ use dashmap :: DashMap ;
2
2
// pyo3 modules
3
3
use pyo3:: prelude:: * ;
4
4
@@ -62,12 +62,12 @@ impl Route {
62
62
// this should ideally be a hashmap of hashmaps but not really
63
63
64
64
pub struct Router {
65
- get_routes : HashMap < Route , Py < PyAny > > ,
66
- post_routes : HashMap < Route , Py < PyAny > > ,
67
- put_routes : HashMap < Route , Py < PyAny > > ,
68
- update_routes : HashMap < Route , Py < PyAny > > ,
69
- delete_routes : HashMap < Route , Py < PyAny > > ,
70
- patch_routes : HashMap < Route , Py < PyAny > > ,
65
+ get_routes : DashMap < Route , Py < PyAny > > ,
66
+ post_routes : DashMap < Route , Py < PyAny > > ,
67
+ put_routes : DashMap < Route , Py < PyAny > > ,
68
+ update_routes : DashMap < Route , Py < PyAny > > ,
69
+ delete_routes : DashMap < Route , Py < PyAny > > ,
70
+ patch_routes : DashMap < Route , Py < PyAny > > ,
71
71
}
72
72
// these should be of the type struct and not the type router
73
73
// request_stream: &TcpStream,
@@ -78,47 +78,46 @@ pub struct Router {
78
78
impl Router {
79
79
pub fn new ( ) -> Self {
80
80
Self {
81
- get_routes : HashMap :: new ( ) ,
82
- post_routes : HashMap :: new ( ) ,
83
- put_routes : HashMap :: new ( ) ,
84
- update_routes : HashMap :: new ( ) ,
85
- delete_routes : HashMap :: new ( ) ,
86
- patch_routes : HashMap :: new ( ) ,
81
+ get_routes : DashMap :: new ( ) ,
82
+ post_routes : DashMap :: new ( ) ,
83
+ put_routes : DashMap :: new ( ) ,
84
+ update_routes : DashMap :: new ( ) ,
85
+ delete_routes : DashMap :: new ( ) ,
86
+ patch_routes : DashMap :: new ( ) ,
87
87
}
88
88
}
89
89
90
- pub fn add_route ( & mut self , route_type : & str , route : Route , handler : Py < PyAny > ) {
91
- if route_type == "GET" {
92
- self . get_routes . insert ( route, handler) ;
93
- } else if route_type == "POST" {
94
- self . post_routes . insert ( route, handler) ;
95
- } else if route_type == "PUT" {
96
- self . put_routes . insert ( route, handler) ;
97
- } else if route_type == "UPDATE" {
98
- self . update_routes . insert ( route, handler) ;
99
- } else if route_type == "DELETE" {
100
- self . delete_routes . insert ( route, handler) ;
101
- } else if route_type == "PATCH" {
102
- self . patch_routes . insert ( route, handler) ;
90
+ #[ inline]
91
+ fn get_relevant_map ( & self , route : & str ) -> Option < & DashMap < Route , Py < PyAny > > > {
92
+ match route {
93
+ "GET" => Some ( & self . get_routes ) ,
94
+ "POST" => Some ( & self . post_routes ) ,
95
+ "PUT" => Some ( & self . put_routes ) ,
96
+ "UPDATE" => Some ( & self . update_routes ) ,
97
+ "DELETE" => Some ( & self . delete_routes ) ,
98
+ "PATCH" => Some ( & self . patch_routes ) ,
99
+ _ => None ,
103
100
}
104
101
}
105
102
106
- pub fn get_route ( & self , route : Route ) -> Option < & Py < PyAny > > {
107
- let route_type = route. get_route_type ( ) ;
108
- if route_type == "GET" {
109
- self . get_routes . get ( & route)
110
- } else if route_type == "POST" {
111
- self . post_routes . get ( & route)
112
- } else if route_type == "PUT" {
113
- self . put_routes . get ( & route)
114
- } else if route_type == "UPDATE" {
115
- self . update_routes . get ( & route)
116
- } else if route_type == "DELETE" {
117
- self . delete_routes . get ( & route)
118
- } else if route_type == "PATCH" {
119
- self . patch_routes . get ( & route)
120
- } else {
121
- None
103
+ pub fn add_route ( & self , route_type : & str , route : Route , handler : Py < PyAny > ) {
104
+ let table = match self . get_relevant_map ( route_type) {
105
+ Some ( table) => table,
106
+ None => return ,
107
+ } ;
108
+
109
+ table. insert ( route, handler) ;
110
+ }
111
+
112
+ pub fn get_route ( & self , route : Route ) -> Option < Py < PyAny > > {
113
+ let table = match self . get_relevant_map ( route. get_route_type ( ) . as_str ( ) ) {
114
+ Some ( table) => table,
115
+ None => return None ,
116
+ } ;
117
+
118
+ match table. get ( & route) {
119
+ Some ( res) => Some ( res. clone ( ) ) ,
120
+ None => None ,
122
121
}
123
122
}
124
123
}
0 commit comments