@@ -3,7 +3,7 @@ pub mod literal_expr;
3
3
4
4
use hir:: Hir ;
5
5
use regex:: Regex ;
6
- use regex_syntax:: parse;
6
+ use regex_syntax:: { hir :: Properties , parse} ;
7
7
use thiserror:: Error ;
8
8
9
9
/// Chroma custom wrapper for a Regex pattern.
@@ -19,14 +19,15 @@ use thiserror::Error;
19
19
pub struct ChromaRegex {
20
20
hir : Hir ,
21
21
pattern : String ,
22
+ properties : Properties ,
22
23
}
23
24
24
25
#[ derive( Debug , Error ) ]
25
26
pub enum ChromaRegexError {
26
27
#[ error( "Byte pattern is not allowed" ) ]
27
28
BytePattern ,
28
- #[ error( "Pattern that always matches is not allowed " ) ]
29
- EmptyPattern ,
29
+ #[ error( "Pattern is too permissive " ) ]
30
+ PermissivePattern ,
30
31
// NOTE: regex::Error is a large type, so we only store its error message here.
31
32
#[ error( "Unexpected regex error: {0}" ) ]
32
33
Regex ( String ) ,
@@ -39,6 +40,9 @@ impl ChromaRegex {
39
40
pub fn hir ( & self ) -> & Hir {
40
41
& self . hir
41
42
}
43
+ pub fn properties ( & self ) -> & Properties {
44
+ & self . properties
45
+ }
42
46
pub fn regex ( & self ) -> Result < Regex , ChromaRegexError > {
43
47
// NOTE: Although this method return a Result<_, _> type, in practice it should always
44
48
// be Ok(_) becasue we validate the pattern during struct construction. Specifically,
@@ -53,16 +57,18 @@ impl TryFrom<String> for ChromaRegex {
53
57
54
58
fn try_from ( value : String ) -> Result < Self , Self :: Error > {
55
59
let hir = parse ( & value) . map_err ( |e| ChromaRegexError :: RegexSyntax ( e. to_string ( ) ) ) ?;
56
- if let Some ( 0 ) = hir. properties ( ) . minimum_len ( ) {
57
- return Err ( ChromaRegexError :: EmptyPattern ) ;
60
+ let properties = hir. properties ( ) . clone ( ) ;
61
+ if let Some ( 0 ) = properties. minimum_len ( ) {
62
+ return Err ( ChromaRegexError :: PermissivePattern ) ;
58
63
}
59
64
let chroma_hir = Hir :: try_from ( hir) ?;
60
65
if let Hir :: Empty = chroma_hir {
61
- return Err ( ChromaRegexError :: EmptyPattern ) ;
66
+ return Err ( ChromaRegexError :: PermissivePattern ) ;
62
67
}
63
68
Ok ( Self {
64
69
hir : chroma_hir,
65
70
pattern : value,
71
+ properties,
66
72
} )
67
73
}
68
74
}
0 commit comments