Skip to content

Commit df27c54

Browse files
author
sicheng
committed
Add regex props
1 parent 5f6e417 commit df27c54

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

rust/types/src/regex/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub mod literal_expr;
33

44
use hir::Hir;
55
use regex::Regex;
6-
use regex_syntax::parse;
6+
use regex_syntax::{hir::Properties, parse};
77
use thiserror::Error;
88

99
/// Chroma custom wrapper for a Regex pattern.
@@ -19,14 +19,15 @@ use thiserror::Error;
1919
pub struct ChromaRegex {
2020
hir: Hir,
2121
pattern: String,
22+
properties: Properties,
2223
}
2324

2425
#[derive(Debug, Error)]
2526
pub enum ChromaRegexError {
2627
#[error("Byte pattern is not allowed")]
2728
BytePattern,
28-
#[error("Pattern that always matches is not allowed")]
29-
EmptyPattern,
29+
#[error("Pattern is too permissive")]
30+
PermissivePattern,
3031
// NOTE: regex::Error is a large type, so we only store its error message here.
3132
#[error("Unexpected regex error: {0}")]
3233
Regex(String),
@@ -39,6 +40,9 @@ impl ChromaRegex {
3940
pub fn hir(&self) -> &Hir {
4041
&self.hir
4142
}
43+
pub fn properties(&self) -> &Properties {
44+
&self.properties
45+
}
4246
pub fn regex(&self) -> Result<Regex, ChromaRegexError> {
4347
// NOTE: Although this method return a Result<_, _> type, in practice it should always
4448
// be Ok(_) becasue we validate the pattern during struct construction. Specifically,
@@ -53,16 +57,18 @@ impl TryFrom<String> for ChromaRegex {
5357

5458
fn try_from(value: String) -> Result<Self, Self::Error> {
5559
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);
5863
}
5964
let chroma_hir = Hir::try_from(hir)?;
6065
if let Hir::Empty = chroma_hir {
61-
return Err(ChromaRegexError::EmptyPattern);
66+
return Err(ChromaRegexError::PermissivePattern);
6267
}
6368
Ok(Self {
6469
hir: chroma_hir,
6570
pattern: value,
71+
properties,
6672
})
6773
}
6874
}

0 commit comments

Comments
 (0)