Wrap license-related errors in enum

This commit is contained in:
David Lukes 2018-02-26 18:53:46 +01:00
parent 53347bc226
commit 1db84a3ec5
3 changed files with 71 additions and 45 deletions

View File

@ -392,39 +392,22 @@ macro_rules! create_config {
}
fn set_license_template(&mut self) {
if self.was_set().license_template_path() {
let lt_path = self.license_template_path();
let mut lt_file = match File::open(&lt_path) {
Ok(file) => file,
Err(e) => {
eprintln!("Warning: unable to open license template file {:?}: {}",
lt_path, e);
return;
}
};
let mut lt_str = String::new();
if let Err(e) = lt_file.read_to_string(&mut lt_str) {
eprintln!("Warning: unable to read from license template file {:?}: {}",
lt_path, e);
return;
};
let lt_parsed = match TemplateParser::parse(&lt_str) {
Ok(string) => string,
Err(e) => {
eprintln!("Warning: unable to parse license template file {:?}: {}",
lt_path, e);
return;
}
};
self.license_template = match Regex::new(&lt_parsed) {
Ok(re) => Some(re),
Err(e) => {
eprintln!("Warning: regex syntax error in placeholder, unable to compile \
license template from file {:?}: {}", lt_path, e);
return;
}
}
if !self.was_set().license_template_path() {
return;
}
let lt_path = self.license_template_path();
let try = || -> Result<Regex, LicenseError> {
let mut lt_file = File::open(&lt_path)?;
let mut lt_str = String::new();
lt_file.read_to_string(&mut lt_str)?;
let lt_parsed = TemplateParser::parse(&lt_str)?;
Ok(Regex::new(&lt_parsed)?)
};
match try() {
Ok(re) => self.license_template = Some(re),
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
lt_path, msg),
};
}
}

View File

@ -1,5 +1,37 @@
use std::io;
use std::fmt;
use regex;
#[derive(Debug)]
pub enum LicenseError {
IO(io::Error),
Regex(regex::Error),
Parse(String),
}
impl fmt::Display for LicenseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LicenseError::IO(ref err) => err.fmt(f),
LicenseError::Regex(ref err) => err.fmt(f),
LicenseError::Parse(ref err) => write!(f, "parsing failed, {}", err),
}
}
}
impl From<io::Error> for LicenseError {
fn from(err: io::Error) -> LicenseError {
LicenseError::IO(err)
}
}
impl From<regex::Error> for LicenseError {
fn from(err: regex::Error) -> LicenseError {
LicenseError::Regex(err)
}
}
// the template is parsed using a state machine
enum ParsingState {
Lit,
@ -76,7 +108,7 @@ impl TemplateParser {
/// "
/// );
/// ```
pub fn parse(template: &str) -> Result<String, String> {
pub fn parse(template: &str) -> Result<String, LicenseError> {
let mut parser = Self::new();
for chr in template.chars() {
if chr == '\n' {
@ -87,19 +119,24 @@ impl TemplateParser {
LitEsc => parser.trans_from_litesc(chr),
Re(brace_nesting) => parser.trans_from_re(chr, brace_nesting),
ReEsc(brace_nesting) => parser.trans_from_reesc(chr, brace_nesting),
Abort(msg) => return Err(msg),
Abort(msg) => return Err(LicenseError::Parse(msg)),
};
}
// check if we've ended parsing in a valid state
match parser.state {
Abort(msg) => return Err(msg),
Abort(msg) => return Err(LicenseError::Parse(msg)),
Re(_) | ReEsc(_) => {
return Err(format!(
return Err(LicenseError::Parse(format!(
"escape or balance opening brace on l. {}",
parser.open_brace_line
));
)));
}
LitEsc => {
return Err(LicenseError::Parse(format!(
"incomplete escape sequence on l. {}",
parser.linum
)))
}
LitEsc => return Err(format!("incomplete escape sequence on l. {}", parser.linum)),
_ => (),
}
parser.parsed.push_str(&regex::escape(&parser.buffer));
@ -198,16 +235,22 @@ mod test {
r"^unbalanced nested braces \{{3}"
);
assert_eq!(
TemplateParser::parse("parsing error }").unwrap_err(),
"escape or balance closing brace on l. 1"
&TemplateParser::parse("parsing error }")
.unwrap_err()
.to_string(),
"parsing failed, escape or balance closing brace on l. 1"
);
assert_eq!(
TemplateParser::parse("parsing error {\nsecond line").unwrap_err(),
"escape or balance opening brace on l. 1"
&TemplateParser::parse("parsing error {\nsecond line")
.unwrap_err()
.to_string(),
"parsing failed, escape or balance opening brace on l. 1"
);
assert_eq!(
TemplateParser::parse(r"parsing error \").unwrap_err(),
"incomplete escape sequence on l. 1"
&TemplateParser::parse(r"parsing error \")
.unwrap_err()
.to_string(),
"parsing failed, incomplete escape sequence on l. 1"
);
}
}

View File

@ -29,7 +29,7 @@ pub mod license;
use config::config_type::ConfigType;
use config::file_lines::FileLines;
use config::license::TemplateParser;
use config::license::{LicenseError, TemplateParser};
pub use config::lists::*;
pub use config::options::*;
use config::summary::Summary;