summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Geary <rtgnj42@gmail.com>2020-03-14 22:20:52 -0400
committerRyan Geary <rtgnj42@gmail.com>2020-03-14 22:20:52 -0400
commit94085bb302474128acbdbbe975ec415db0ee4003 (patch)
treed2433177ce9c737430ab4cdb4a37a15c17d6296d
parent59ada49afdc918343c1c8eced5694760cd9fac11 (diff)
Make regex compilation errors more specific
-rw-r--r--src/config.rs29
1 files changed, 22 insertions, 7 deletions
diff --git a/src/config.rs b/src/config.rs
index b435d02..cacc87e 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -49,15 +49,30 @@ impl Config {
}
}
- let separator = Regex::new(match &opt.field_separator {
+ let separator = match Regex::new(match &opt.field_separator {
Some(s) => s,
None => "[[:space:]]",
- })
- .unwrap_or_else(|e| {
- eprintln!("Failed to compile regular expression: {}", e);
- // Exit code of 1 means failed to compile field_separator regex
- process::exit(1);
- });
+ }) {
+ Ok(r) => r,
+ Err(e) => {
+ // Exit code of 1 means failed to compile field_separator regex
+ match e {
+ regex::Error::Syntax(e) => {
+ eprintln!("Syntax error compiling regular expression: {}", e);
+ process::exit(1);
+ }
+ regex::Error::CompiledTooBig(e) => {
+ eprintln!("Compiled regular expression too big: compiled size cannot exceed {} bytes", e);
+ process::exit(1);
+ }
+ _ => {
+ eprintln!("Error compiling regular expression: {}", e);
+ process::exit(1);
+ }
+ }
+ }
+ };
+
Config { opt, separator }
}