11use crate :: schema:: Schema ;
22use cidr:: IpCidr ;
33use regex:: Regex ;
4- use std:: net:: IpAddr ;
4+ use std:: { net:: IpAddr , rc :: Rc } ;
55
66#[ cfg( feature = "serde" ) ]
77use serde:: { Deserialize , Serialize } ;
@@ -53,7 +53,7 @@ pub enum Value {
5353 IpAddr ( IpAddr ) ,
5454 Int ( i64 ) ,
5555 #[ cfg_attr( feature = "serde" , serde( with = "serde_regex" ) ) ]
56- Regex ( Regex ) ,
56+ Regex ( Rc < Regex > ) ,
5757}
5858
5959impl PartialEq for Value {
@@ -137,7 +137,7 @@ pub struct Predicate {
137137mod tests {
138138 use super :: * ;
139139 use crate :: parser:: parse;
140- use std:: fmt;
140+ use std:: { collections :: HashMap , fmt} ;
141141
142142 impl fmt:: Display for Expression {
143143 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -240,6 +240,7 @@ mod tests {
240240
241241 #[ test]
242242 fn expr_op_and_prec ( ) {
243+ let mut regex_cache = HashMap :: new ( ) ;
243244 let tests = vec ! [
244245 ( "a > 0" , "(a > 0)" ) ,
245246 ( "a in \" abc\" " , "(a in \" abc\" )" ) ,
@@ -271,13 +272,14 @@ mod tests {
271272 ) ,
272273 ] ;
273274 for ( input, expected) in tests {
274- let result = parse ( input) . unwrap ( ) ;
275+ let result = parse ( input, & mut regex_cache ) . unwrap ( ) ;
275276 assert_eq ! ( result. to_string( ) , expected) ;
276277 }
277278 }
278279
279280 #[ test]
280281 fn expr_var_name_and_ip ( ) {
282+ let mut regex_cache = HashMap :: new ( ) ;
281283 let tests = vec ! [
282284 // ipv4_literal
283285 ( "kong.foo in 1.1.1.1" , "(kong.foo in 1.1.1.1)" ) ,
@@ -298,13 +300,14 @@ mod tests {
298300 ) ,
299301 ] ;
300302 for ( input, expected) in tests {
301- let result = parse ( input) . unwrap ( ) ;
303+ let result = parse ( input, & mut regex_cache ) . unwrap ( ) ;
302304 assert_eq ! ( result. to_string( ) , expected) ;
303305 }
304306 }
305307
306308 #[ test]
307309 fn expr_regex ( ) {
310+ let mut regex_cache = HashMap :: new ( ) ;
308311 let tests = vec ! [
309312 // regex_literal
310313 (
@@ -318,13 +321,14 @@ mod tests {
318321 ) ,
319322 ] ;
320323 for ( input, expected) in tests {
321- let result = parse ( input) . unwrap ( ) ;
324+ let result = parse ( input, & mut regex_cache ) . unwrap ( ) ;
322325 assert_eq ! ( result. to_string( ) , expected) ;
323326 }
324327 }
325328
326329 #[ test]
327330 fn expr_digits ( ) {
331+ let mut regex_cache = HashMap :: new ( ) ;
328332 let tests = vec ! [
329333 // dec literal
330334 ( "kong.foo.foo7 == 123" , "(kong.foo.foo7 == 123)" ) ,
@@ -340,13 +344,14 @@ mod tests {
340344 ( "kong.foo.foo12 == -0123" , "(kong.foo.foo12 == -83)" ) ,
341345 ] ;
342346 for ( input, expected) in tests {
343- let result = parse ( input) . unwrap ( ) ;
347+ let result = parse ( input, & mut regex_cache ) . unwrap ( ) ;
344348 assert_eq ! ( result. to_string( ) , expected) ;
345349 }
346350 }
347351
348352 #[ test]
349353 fn expr_transformations ( ) {
354+ let mut regex_cache = HashMap :: new ( ) ;
350355 let tests = vec ! [
351356 // lower
352357 (
@@ -360,13 +365,14 @@ mod tests {
360365 ) ,
361366 ] ;
362367 for ( input, expected) in tests {
363- let result = parse ( input) . unwrap ( ) ;
368+ let result = parse ( input, & mut regex_cache ) . unwrap ( ) ;
364369 assert_eq ! ( result. to_string( ) , expected) ;
365370 }
366371 }
367372
368373 #[ test]
369374 fn expr_transformations_nested ( ) {
375+ let mut regex_cache = HashMap :: new ( ) ;
370376 let tests = vec ! [
371377 // lower + lower
372378 (
@@ -390,35 +396,37 @@ mod tests {
390396 ) ,
391397 ] ;
392398 for ( input, expected) in tests {
393- let result = parse ( input) . unwrap ( ) ;
399+ let result = parse ( input, & mut regex_cache ) . unwrap ( ) ;
394400 assert_eq ! ( result. to_string( ) , expected) ;
395401 }
396402 }
397403
398404 #[ test]
399405 fn str_unicode_test ( ) {
406+ let mut regex_cache = HashMap :: new ( ) ;
400407 let tests = vec ! [
401408 // cjk chars
402409 ( "t_msg in \" 你好\" " , "(t_msg in \" 你好\" )" ) ,
403410 // 0xXXX unicode
404411 ( "t_msg in \" \u{4f60} \u{597d} \" " , "(t_msg in \" 你好\" )" ) ,
405412 ] ;
406413 for ( input, expected) in tests {
407- let result = parse ( input) . unwrap ( ) ;
414+ let result = parse ( input, & mut regex_cache ) . unwrap ( ) ;
408415 assert_eq ! ( result. to_string( ) , expected) ;
409416 }
410417 }
411418
412419 #[ test]
413420 fn rawstr_test ( ) {
421+ let mut regex_cache = HashMap :: new ( ) ;
414422 let tests = vec ! [
415423 // invalid escape sequence
416424 ( r##"a == r#"/path/to/\d+"#"## , r#"(a == "/path/to/\d+")"# ) ,
417425 // valid escape sequence
418426 ( r##"a == r#"/path/to/\n+"#"## , r#"(a == "/path/to/\n+")"# ) ,
419427 ] ;
420428 for ( input, expected) in tests {
421- let result = parse ( input) . unwrap ( ) ;
429+ let result = parse ( input, & mut regex_cache ) . unwrap ( ) ;
422430 assert_eq ! ( result. to_string( ) , expected) ;
423431 }
424432 }
0 commit comments