24
24
GetLocalizedFunc : Fn ( & str ) -> Option < Cow < ' a , str > > ,
25
25
{
26
26
fn replace_append ( & mut self , caps : & Captures < ' _ > , dst : & mut String ) {
27
+ let default_replace = |dst : & mut String | dst. push_str ( caps. get ( 0 ) . unwrap ( ) . as_str ( ) ) ;
28
+
27
29
let mut handle_value_capture = |cap_match : Match < ' _ > | {
28
30
if let Some ( value) = ( self . get_value ) ( cap_match. as_str ( ) ) {
29
31
if let Value :: Ref ( val) = value {
34
36
dst. push_str ( & value. to_string ( ) )
35
37
}
36
38
} else {
37
- dst . push_str ( caps . get ( 0 ) . unwrap ( ) . as_str ( ) ) ;
39
+ default_replace ( dst ) ;
38
40
}
39
41
} ;
40
42
@@ -49,10 +51,10 @@ where
49
51
if let Some ( value) = ( self . get_localized ) ( cap_match. as_str ( ) ) {
50
52
dst. push_str ( & value) ;
51
53
} else {
52
- dst . push_str ( caps . get ( 0 ) . unwrap ( ) . as_str ( ) ) ;
54
+ default_replace ( dst ) ;
53
55
}
54
56
} else {
55
- dst . push_str ( caps . get ( 0 ) . unwrap ( ) . as_str ( ) ) ;
57
+ default_replace ( dst ) ;
56
58
}
57
59
}
58
60
}
@@ -80,18 +82,19 @@ where
80
82
// Cache the regular expression in memory so it doesn't need to compile on each invocation.
81
83
static REG_EX : OnceLock < Regex > = OnceLock :: new ( ) ;
82
84
83
- let regex = REG_EX . get_or_init ( || {
84
- // Replace $tags, ${tag} or $<pod::key>
85
- Regex :: new ( r"(\$([a-z][a-zA-Z0-9_]+))|(\$\{([a-z][a-zA-Z0-9_]+)\})|(\$<([^>]+)>)" ) . unwrap ( )
86
- } ) ;
87
-
88
- regex. replace_all (
89
- pattern,
90
- DisReplacer {
91
- get_value : & get_value,
92
- get_localized : & get_localized,
93
- } ,
94
- )
85
+ REG_EX
86
+ . get_or_init ( || {
87
+ // Replace $tags, ${tag} or $<pod::key>
88
+ Regex :: new ( r"(\$([a-z][a-zA-Z0-9_]+))|(\$\{([a-z][a-zA-Z0-9_]+)\})|(\$<([^>]+)>)" )
89
+ . unwrap ( )
90
+ } )
91
+ . replace_all (
92
+ pattern,
93
+ DisReplacer {
94
+ get_value : & get_value,
95
+ get_localized : & get_localized,
96
+ } ,
97
+ )
95
98
}
96
99
97
100
#[ cfg( test) ]
@@ -120,10 +123,9 @@ mod test {
120
123
}
121
124
122
125
fn i18n_cb < ' a > ( name : & str ) -> Option < Cow < ' a , str > > {
123
- if name == "pod::hello" {
124
- Some ( Cow :: Borrowed ( "world" ) )
125
- } else {
126
- None
126
+ match name {
127
+ "pod::hello" => Some ( Cow :: Borrowed ( "world" ) ) ,
128
+ _ => None ,
127
129
}
128
130
}
129
131
0 commit comments