Skip to content

Commit cce264b

Browse files
committed
Wrap license-related errors in enum
1 parent 243ac1a commit cce264b

File tree

3 files changed

+71
-45
lines changed

3 files changed

+71
-45
lines changed

rustfmt-config/src/config_type.rs

+15-32
Original file line numberDiff line numberDiff line change
@@ -392,39 +392,22 @@ macro_rules! create_config {
392392
}
393393

394394
fn set_license_template(&mut self) {
395-
if self.was_set().license_template_path() {
396-
let lt_path = self.license_template_path();
397-
let mut lt_file = match File::open(&lt_path) {
398-
Ok(file) => file,
399-
Err(e) => {
400-
eprintln!("Warning: unable to open license template file {:?}: {}",
401-
lt_path, e);
402-
return;
403-
}
404-
};
405-
let mut lt_str = String::new();
406-
if let Err(e) = lt_file.read_to_string(&mut lt_str) {
407-
eprintln!("Warning: unable to read from license template file {:?}: {}",
408-
lt_path, e);
409-
return;
410-
};
411-
let lt_parsed = match TemplateParser::parse(&lt_str) {
412-
Ok(string) => string,
413-
Err(e) => {
414-
eprintln!("Warning: unable to parse license template file {:?}: {}",
415-
lt_path, e);
416-
return;
417-
}
418-
};
419-
self.license_template = match Regex::new(&lt_parsed) {
420-
Ok(re) => Some(re),
421-
Err(e) => {
422-
eprintln!("Warning: regex syntax error in placeholder, unable to compile \
423-
license template from file {:?}: {}", lt_path, e);
424-
return;
425-
}
426-
}
395+
if !self.was_set().license_template_path() {
396+
return;
427397
}
398+
let lt_path = self.license_template_path();
399+
let try = || -> Result<Regex, LicenseError> {
400+
let mut lt_file = File::open(&lt_path)?;
401+
let mut lt_str = String::new();
402+
lt_file.read_to_string(&mut lt_str)?;
403+
let lt_parsed = TemplateParser::parse(&lt_str)?;
404+
Ok(Regex::new(&lt_parsed)?)
405+
};
406+
match try() {
407+
Ok(re) => self.license_template = Some(re),
408+
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
409+
lt_path, msg),
410+
};
428411
}
429412
}
430413

rustfmt-config/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub mod license;
3737

3838
use config_type::ConfigType;
3939
use file_lines::FileLines;
40-
use license::TemplateParser;
40+
use license::{LicenseError, TemplateParser};
4141
pub use lists::*;
4242
pub use options::*;
4343
use summary::Summary;

rustfmt-config/src/license.rs

+55-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
1+
use std::io;
2+
use std::fmt;
3+
14
use regex;
25

6+
#[derive(Debug)]
7+
pub enum LicenseError {
8+
IO(io::Error),
9+
Regex(regex::Error),
10+
Parse(String),
11+
}
12+
13+
impl fmt::Display for LicenseError {
14+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15+
match *self {
16+
LicenseError::IO(ref err) => err.fmt(f),
17+
LicenseError::Regex(ref err) => err.fmt(f),
18+
LicenseError::Parse(ref err) => write!(f, "parsing failed, {}", err),
19+
}
20+
}
21+
}
22+
23+
impl From<io::Error> for LicenseError {
24+
fn from(err: io::Error) -> LicenseError {
25+
LicenseError::IO(err)
26+
}
27+
}
28+
29+
impl From<regex::Error> for LicenseError {
30+
fn from(err: regex::Error) -> LicenseError {
31+
LicenseError::Regex(err)
32+
}
33+
}
34+
335
// the template is parsed using a state machine
436
enum ParsingState {
537
Lit,
@@ -76,7 +108,7 @@ impl TemplateParser {
76108
/// "
77109
/// );
78110
/// ```
79-
pub fn parse(template: &str) -> Result<String, String> {
111+
pub fn parse(template: &str) -> Result<String, LicenseError> {
80112
let mut parser = Self::new();
81113
for chr in template.chars() {
82114
if chr == '\n' {
@@ -87,19 +119,24 @@ impl TemplateParser {
87119
LitEsc => parser.trans_from_litesc(chr),
88120
Re(brace_nesting) => parser.trans_from_re(chr, brace_nesting),
89121
ReEsc(brace_nesting) => parser.trans_from_reesc(chr, brace_nesting),
90-
Abort(msg) => return Err(msg),
122+
Abort(msg) => return Err(LicenseError::Parse(msg)),
91123
};
92124
}
93125
// check if we've ended parsing in a valid state
94126
match parser.state {
95-
Abort(msg) => return Err(msg),
127+
Abort(msg) => return Err(LicenseError::Parse(msg)),
96128
Re(_) | ReEsc(_) => {
97-
return Err(format!(
129+
return Err(LicenseError::Parse(format!(
98130
"escape or balance opening brace on l. {}",
99131
parser.open_brace_line
100-
));
132+
)));
133+
}
134+
LitEsc => {
135+
return Err(LicenseError::Parse(format!(
136+
"incomplete escape sequence on l. {}",
137+
parser.linum
138+
)))
101139
}
102-
LitEsc => return Err(format!("incomplete escape sequence on l. {}", parser.linum)),
103140
_ => (),
104141
}
105142
parser.parsed.push_str(&regex::escape(&parser.buffer));
@@ -198,16 +235,22 @@ mod test {
198235
r"^unbalanced nested braces \{{3}"
199236
);
200237
assert_eq!(
201-
TemplateParser::parse("parsing error }").unwrap_err(),
202-
"escape or balance closing brace on l. 1"
238+
&TemplateParser::parse("parsing error }")
239+
.unwrap_err()
240+
.to_string(),
241+
"parsing failed, escape or balance closing brace on l. 1"
203242
);
204243
assert_eq!(
205-
TemplateParser::parse("parsing error {\nsecond line").unwrap_err(),
206-
"escape or balance opening brace on l. 1"
244+
&TemplateParser::parse("parsing error {\nsecond line")
245+
.unwrap_err()
246+
.to_string(),
247+
"parsing failed, escape or balance opening brace on l. 1"
207248
);
208249
assert_eq!(
209-
TemplateParser::parse(r"parsing error \").unwrap_err(),
210-
"incomplete escape sequence on l. 1"
250+
&TemplateParser::parse(r"parsing error \")
251+
.unwrap_err()
252+
.to_string(),
253+
"parsing failed, incomplete escape sequence on l. 1"
211254
);
212255
}
213256
}

0 commit comments

Comments
 (0)