Make regex compilation errors more specific

This commit is contained in:
Ryan Geary 2020-03-14 22:20:52 -04:00
parent 59ada49afd
commit 94085bb302

View file

@ -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, Some(s) => s,
None => "[[:space:]]", None => "[[:space:]]",
}) }) {
.unwrap_or_else(|e| { Ok(r) => r,
eprintln!("Failed to compile regular expression: {}", e); Err(e) => {
// Exit code of 1 means failed to compile field_separator regex // Exit code of 1 means failed to compile field_separator regex
process::exit(1); 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 } Config { opt, separator }
} }