@@ -280,7 +280,7 @@ pub struct RegexRule {
280280 pub pattern : String ,
281281 /// Compiled regex (not serialized)
282282 #[ serde( skip) ]
283- compiled : OnceLock < Regex > ,
283+ compiled : OnceLock < Result < Regex , String > > ,
284284 /// Custom error message
285285 #[ serde( skip_serializing_if = "Option::is_none" ) ]
286286 pub message : Option < String > ,
@@ -309,19 +309,15 @@ impl RegexRule {
309309 }
310310
311311 fn get_regex ( & self ) -> Result < & Regex , RuleError > {
312- self . compiled . get_or_init ( || {
313- Regex :: new ( & self . pattern ) . unwrap_or_else ( |_| Regex :: new ( "^$" ) . unwrap ( ) )
312+ let result = self . compiled . get_or_init ( || {
313+ Regex :: new ( & self . pattern )
314+ . map_err ( |_| format ! ( "Invalid regex pattern: {}" , self . pattern) )
314315 } ) ;
315316
316- // Verify the pattern is valid
317- if Regex :: new ( & self . pattern ) . is_err ( ) {
318- return Err ( RuleError :: new (
319- "regex" ,
320- format ! ( "Invalid regex pattern: {}" , self . pattern) ,
321- ) ) ;
317+ match result {
318+ Ok ( regex) => Ok ( regex) ,
319+ Err ( msg) => Err ( RuleError :: new ( "regex" , msg. clone ( ) ) ) ,
322320 }
323-
324- Ok ( self . compiled . get ( ) . unwrap ( ) )
325321 }
326322}
327323
0 commit comments