From 94085bb302474128acbdbbe975ec415db0ee4003 Mon Sep 17 00:00:00 2001 From: Ryan Geary Date: Sat, 14 Mar 2020 22:20:52 -0400 Subject: Make regex compilation errors more specific --- src/config.rs | 29 ++++++++++++++++++++++------- 1 file 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 } } -- cgit v1.2.3