@@ -362,7 +362,12 @@ impl<'a> ParserBuilder<'a> {
362
362
Ok ( field. set_capture ( sub_keys, & mut self . capture_next ) )
363
363
}
364
364
365
- fn push_regex ( & mut self , s : & LitStr , context : & DisplayContext ) -> Result < ( ) > {
365
+ fn push_regex (
366
+ & mut self ,
367
+ s : & LitStr ,
368
+ context : & DisplayContext ,
369
+ format : & Option < DisplayFormat > ,
370
+ ) -> Result < ( ) > {
366
371
const IDX_ESC : usize = 1 ;
367
372
const IDX_P : usize = 2 ;
368
373
const IDX_KEY : usize = 3 ;
@@ -427,6 +432,9 @@ impl<'a> ParserBuilder<'a> {
427
432
}
428
433
if let DisplayContext :: Field { .. } = context {
429
434
if !has_capture {
435
+ if let Some ( format) = format {
436
+ return self . push_format ( format, context, None , Some ( & text) ) ;
437
+ }
430
438
let name = self . set_capture ( context, & [ ] , s. span ( ) ) ?;
431
439
text = format ! ( "(?<{name}>{text})" ) ;
432
440
}
@@ -438,7 +446,8 @@ impl<'a> ParserBuilder<'a> {
438
446
& mut self ,
439
447
format : & DisplayFormat ,
440
448
context : & DisplayContext ,
441
- with : & Option < Expr > ,
449
+ with : Option < & Expr > ,
450
+ regex : Option < & str > ,
442
451
) -> Result < ( ) > {
443
452
for p in & format. parts {
444
453
match p {
@@ -458,15 +467,18 @@ impl<'a> ParserBuilder<'a> {
458
467
continue ;
459
468
}
460
469
let c = self . set_capture ( context, & keys, format. span ) ?;
461
- let f = format ! ( "(?<{c}>(?s:.*?))" ) ;
462
- self . parse_format . push_hir ( to_hir ( & f) ) ;
470
+ let mut f = format ! ( "(?<{c}>(?s:.*?))" ) ;
463
471
if keys. is_empty ( ) {
472
+ if let Some ( regex) = regex {
473
+ f = format ! ( "(?<{c}>(?s:{regex}))" ) ;
474
+ }
464
475
if let DisplayContext :: Field { field, key, .. } = context {
465
476
if let Some ( with_expr) = with {
466
477
self . with . push ( With :: new ( c, key, with_expr, & field. ty ) ) ;
467
478
}
468
479
}
469
480
}
481
+ self . parse_format . push_hir ( to_hir ( & f) ) ;
470
482
}
471
483
}
472
484
}
@@ -484,7 +496,12 @@ impl<'a> ParserBuilder<'a> {
484
496
}
485
497
fn push_attrs ( & mut self , hattrs : & HelperAttributes , context : & DisplayContext ) -> Result < ( ) > {
486
498
if !self . try_push_attrs ( hattrs, context) ? {
487
- self . push_format ( & context. default_from_str_format ( ) ?, context, & hattrs. with ) ?;
499
+ self . push_format (
500
+ & context. default_from_str_format ( ) ?,
501
+ context,
502
+ hattrs. with . as_ref ( ) ,
503
+ None ,
504
+ ) ?;
488
505
}
489
506
Ok ( ( ) )
490
507
}
@@ -494,10 +511,10 @@ impl<'a> ParserBuilder<'a> {
494
511
context : & DisplayContext ,
495
512
) -> Result < bool > {
496
513
Ok ( if let Some ( regex) = & hattrs. regex {
497
- self . push_regex ( regex, context) ?;
514
+ self . push_regex ( regex, context, & hattrs . format ) ?;
498
515
true
499
516
} else if let Some ( format) = & hattrs. format {
500
- self . push_format ( format, context, & hattrs. with ) ?;
517
+ self . push_format ( format, context, hattrs. with . as_ref ( ) , None ) ?;
501
518
true
502
519
} else {
503
520
false
0 commit comments