Skip to content

Commit

Permalink
Wrap license-related errors in enum
Browse files Browse the repository at this point in the history
  • Loading branch information
dlukes committed Feb 26, 2018
1 parent 243ac1a commit cce264b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 45 deletions.
47 changes: 15 additions & 32 deletions rustfmt-config/src/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}
}

Expand Down
2 changes: 1 addition & 1 deletion rustfmt-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub mod license;

use config_type::ConfigType;
use file_lines::FileLines;
use license::TemplateParser;
use license::{LicenseError, TemplateParser};
pub use lists::*;
pub use options::*;
use summary::Summary;
Expand Down
67 changes: 55 additions & 12 deletions rustfmt-config/src/license.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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' {
Expand All @@ -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));
Expand Down Expand Up @@ -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"
);
}
}

0 comments on commit cce264b

Please sign in to comment.