11use crate :: http:: Request ;
2- use crate :: router:: request_matcher:: { MethodMatcher , RequestMatcher } ;
2+ use crate :: router:: request_matcher:: { PathAndQueryMatcher , RequestMatcher } ;
33use crate :: router:: route_datetime:: RouteDateTime ;
44use crate :: router:: route_time:: RouteTime ;
55use crate :: router:: route_weekday:: RouteWeekday ;
66use crate :: router:: trace:: { TraceInfo , TraceInfoDateTimeCondition } ;
77use crate :: router:: { Route , RouteData , Trace } ;
8- use chrono:: { DateTime , Utc } ;
98use serde:: { Deserialize , Serialize } ;
109use std:: collections:: BTreeMap ;
1110use std:: collections:: BTreeSet ;
@@ -32,13 +31,14 @@ impl<T: RouteData> RequestMatcher<T> for DateTimeMatcher<T> {
3231 self . count += 1 ;
3332
3433 let mut condition_group = BTreeSet :: new ( ) ;
35- let mut conditions = BTreeSet :: new ( ) ;
34+ let mut route_conditions = BTreeSet :: new ( ) ;
3635
3736 match route. datetime ( ) {
3837 Some ( route_datetime) => {
3938 let condition = DateTimeCondition :: DateTimeRange ( route_datetime. clone ( ) ) ;
4039 condition_group. insert ( condition. clone ( ) ) ;
41- conditions. insert ( condition) ;
40+ route_conditions. insert ( condition. clone ( ) ) ;
41+ self . conditions . insert ( condition) ;
4242 }
4343 None => ( ) ,
4444 }
@@ -47,7 +47,8 @@ impl<T: RouteData> RequestMatcher<T> for DateTimeMatcher<T> {
4747 Some ( route_weekdays) => {
4848 let condition = DateTimeCondition :: Weekdays ( route_weekdays. clone ( ) ) ;
4949 condition_group. insert ( condition. clone ( ) ) ;
50- conditions. insert ( condition) ;
50+ route_conditions. insert ( condition. clone ( ) ) ;
51+ self . conditions . insert ( condition) ;
5152 }
5253 None => ( ) ,
5354 }
@@ -56,19 +57,18 @@ impl<T: RouteData> RequestMatcher<T> for DateTimeMatcher<T> {
5657 Some ( route_time) => {
5758 let condition = DateTimeCondition :: TimeRange ( route_time. clone ( ) ) ;
5859 condition_group. insert ( condition. clone ( ) ) ;
59- conditions. insert ( condition) ;
60+ route_conditions. insert ( condition. clone ( ) ) ;
61+ self . conditions . insert ( condition) ;
6062 }
6163 None => ( ) ,
6264 }
6365
64- if conditions . is_empty ( ) {
66+ if route_conditions . is_empty ( ) {
6567 self . any_datetime . insert ( route) ;
6668
6769 return ;
6870 }
6971
70- self . conditions . extend ( conditions) ;
71-
7272 if !self . condition_groups . contains_key ( & condition_group) {
7373 self . condition_groups . insert ( condition_group. clone ( ) , Self :: create_sub_matcher ( ) ) ;
7474 }
@@ -98,25 +98,23 @@ impl<T: RouteData> RequestMatcher<T> for DateTimeMatcher<T> {
9898 let mut rules = self . any_datetime . match_request ( request) ;
9999 let mut execute_conditions = BTreeMap :: new ( ) ;
100100
101- if let Some ( datetime) = request. created_at . as_ref ( ) {
102- ' group: for ( conditions, matcher) in & self . condition_groups {
103- for condition in conditions {
104- match execute_conditions. get ( condition) {
105- None => {
106- // Execute condition
107- let result = condition. match_value ( datetime) ;
108-
109- // Save result
110- execute_conditions. insert ( condition. clone ( ) , result) ;
111-
112- if !result {
113- continue ' group;
114- }
101+ ' group: for ( conditions, matcher) in & self . condition_groups {
102+ for condition in conditions {
103+ match execute_conditions. get ( condition) {
104+ None => {
105+ // Execute condition
106+ let result = condition. match_value ( request) ;
107+
108+ // Save result
109+ execute_conditions. insert ( condition. clone ( ) , result) ;
110+
111+ if !result {
112+ continue ' group;
115113 }
116- Some ( result ) => {
117- if ! result {
118- continue ' group ;
119- }
114+ }
115+ Some ( result) => {
116+ if !result {
117+ continue ' group ;
120118 }
121119 }
122120 }
@@ -132,58 +130,54 @@ impl<T: RouteData> RequestMatcher<T> for DateTimeMatcher<T> {
132130 let mut traces = self . any_datetime . trace ( request) ;
133131 let mut execute_conditions = BTreeMap :: new ( ) ;
134132
135- if let Some ( datetime) = request. created_at . as_ref ( ) {
136- for ( conditions, matcher) in & self . condition_groups {
137- let mut matched = true ;
138- let mut executed = true ;
139- let mut traces_info_datetime = Vec :: new ( ) ;
140-
141- for condition in conditions {
142- match execute_conditions. get ( condition) {
143- None => {
144- // Execute condition
145- let result = condition. match_value ( datetime) ;
146- matched = matched && result;
147-
148- // Save result (only if executed to mimic cache behavior)
149- if executed {
150- execute_conditions. insert ( condition. clone ( ) , matched) ;
151- }
152-
153- traces_info_datetime. push ( TraceInfoDateTimeCondition {
154- result : if executed { Some ( result) } else { None } ,
155- condition : condition. clone ( ) ,
156- against : datetime. clone ( ) ,
157- cached : false ,
158- } ) ;
159-
160- executed = matched;
133+ for ( conditions, matcher) in & self . condition_groups {
134+ let mut matched = true ;
135+ let mut executed = true ;
136+ let mut traces_info_datetime = Vec :: new ( ) ;
137+
138+ for condition in conditions {
139+ match execute_conditions. get ( condition) {
140+ None => {
141+ // Execute condition
142+ let result = condition. match_value ( request) ;
143+ matched = matched && result;
144+
145+ // Save result (only if executed to mimic cache behavior)
146+ if executed {
147+ execute_conditions. insert ( condition. clone ( ) , matched) ;
161148 }
162- Some ( result) => {
163- matched = matched && * result;
164149
165- traces_info_datetime. push ( TraceInfoDateTimeCondition {
166- result : if executed { Some ( * result) } else { None } ,
167- condition : condition. clone ( ) ,
168- against : datetime. clone ( ) ,
169- cached : true ,
170- } ) ;
150+ traces_info_datetime. push ( TraceInfoDateTimeCondition {
151+ result : if executed { Some ( result) } else { None } ,
152+ condition : condition. clone ( ) ,
153+ cached : false ,
154+ } ) ;
171155
172- executed = matched;
173- }
156+ executed = matched;
174157 }
175- }
158+ Some ( result) => {
159+ matched = matched && * result;
176160
177- traces . push ( Trace :: new (
178- matched ,
179- true ,
180- matcher . len ( ) as u64 ,
181- if matched { matcher . trace ( request ) } else { Vec :: new ( ) } ,
182- TraceInfo :: DateTimeGroup {
183- conditions : traces_info_datetime ,
184- } ,
185- ) ) ;
161+ traces_info_datetime . push ( TraceInfoDateTimeCondition {
162+ result : if executed { Some ( * result ) } else { None } ,
163+ condition : condition . clone ( ) ,
164+ cached : true ,
165+ } ) ;
166+
167+ executed = matched ;
168+ }
169+ }
186170 }
171+
172+ traces. push ( Trace :: new (
173+ matched,
174+ true ,
175+ matcher. len ( ) as u64 ,
176+ if matched { matcher. trace ( request) } else { Vec :: new ( ) } ,
177+ TraceInfo :: DateTimeGroup {
178+ conditions : traces_info_datetime,
179+ } ,
180+ ) ) ;
187181 }
188182
189183 traces
@@ -225,30 +219,34 @@ impl<T: RouteData> Default for DateTimeMatcher<T> {
225219
226220impl < T : RouteData > DateTimeMatcher < T > {
227221 pub fn create_sub_matcher ( ) -> Box < dyn RequestMatcher < T > > {
228- Box :: < MethodMatcher < T > > :: default ( )
222+ Box :: < PathAndQueryMatcher < T > > :: default ( )
229223 }
230224}
231225
232226impl DateTimeCondition {
233- pub fn match_value ( & self , datetime : & DateTime < Utc > ) -> bool {
234- match self {
235- DateTimeCondition :: DateTimeRange ( route_date_time) => {
236- for range in route_date_time {
237- if range. match_datetime ( datetime) {
238- return true ;
227+ pub fn match_value ( & self , request : & Request ) -> bool {
228+ if let Some ( datetime) = request. created_at . as_ref ( ) {
229+ match self {
230+ DateTimeCondition :: DateTimeRange ( route_date_time) => {
231+ for range in route_date_time {
232+ if range. match_datetime ( datetime) {
233+ return true ;
234+ }
239235 }
236+ return false ;
240237 }
241- return false ;
242- }
243- DateTimeCondition :: TimeRange ( route_time) => {
244- for range in route_time {
245- if range. match_datetime ( datetime) {
246- return true ;
238+ DateTimeCondition :: TimeRange ( route_time) => {
239+ for range in route_time {
240+ if range. match_datetime ( datetime) {
241+ return true ;
242+ }
247243 }
244+ return false ;
248245 }
249- return false ;
246+ DateTimeCondition :: Weekdays ( route_weekday ) => route_weekday . match_datetime ( datetime ) ,
250247 }
251- DateTimeCondition :: Weekdays ( route_weekday) => route_weekday. match_datetime ( datetime) ,
248+ } else {
249+ false
252250 }
253251 }
254252}
0 commit comments