Skip to content

Commit 432b1cb

Browse files
committed
Rationalise error types
cc #2639
1 parent 760af6c commit 432b1cb

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

src/bin/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use getopts::{Matches, Options};
2626

2727
use rustfmt::{
2828
emit_post_matter, emit_pre_matter, format_and_emit_report, load_config, CliOptions, Config,
29-
FileName, FmtResult, Input, Summary, Verbosity, WriteMode,
29+
ErrorKind, FileName, Input, Summary, Verbosity, WriteMode,
3030
};
3131

3232
fn main() {
@@ -170,7 +170,7 @@ fn is_nightly() -> bool {
170170
.unwrap_or(false)
171171
}
172172

173-
fn execute(opts: &Options) -> FmtResult<(WriteMode, Summary)> {
173+
fn execute(opts: &Options) -> Result<(WriteMode, Summary), failure::Error> {
174174
let matches = opts.parse(env::args().skip(1))?;
175175
let options = CliOptions::from_matches(&matches)?;
176176

@@ -239,7 +239,7 @@ fn format(
239239
files: Vec<PathBuf>,
240240
minimal_config_path: Option<String>,
241241
options: CliOptions,
242-
) -> FmtResult<(WriteMode, Summary)> {
242+
) -> Result<(WriteMode, Summary), failure::Error> {
243243
options.verify_file_lines(&files);
244244
let (config, config_path) = load_config(None, Some(&options))?;
245245

@@ -347,7 +347,7 @@ fn print_version() {
347347
println!("rustfmt {}", version_info);
348348
}
349349

350-
fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
350+
fn determine_operation(matches: &Matches) -> Result<Operation, ErrorKind> {
351351
if matches.opt_present("h") {
352352
let topic = matches.opt_str("h");
353353
if topic == None {

src/config/options.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ use config::config_type::ConfigType;
1414
use config::file_lines::FileLines;
1515
use config::lists::*;
1616
use config::Config;
17-
use FmtResult;
1817

19-
use failure::err_msg;
18+
use failure::{self, err_msg};
2019

2120
use getopts::Matches;
2221
use std::collections::HashSet;
@@ -345,7 +344,7 @@ pub struct CliOptions {
345344
}
346345

347346
impl CliOptions {
348-
pub fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
347+
pub fn from_matches(matches: &Matches) -> Result<CliOptions, failure::Error> {
349348
let mut options = CliOptions::default();
350349
options.verbose = matches.opt_present("verbose");
351350
options.quiet = matches.opt_present("quiet");
@@ -448,7 +447,7 @@ impl CliOptions {
448447
}
449448
}
450449

451-
fn write_mode_from_emit_str(emit_str: &str) -> FmtResult<WriteMode> {
450+
fn write_mode_from_emit_str(emit_str: &str) -> Result<WriteMode, failure::Error> {
452451
match emit_str {
453452
"files" => Ok(WriteMode::Overwrite),
454453
"stdout" => Ok(WriteMode::Display),

src/lib.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ pub use config::options::CliOptions;
6464
pub use config::summary::Summary;
6565
pub use config::{file_lines, load_config, Config, Verbosity, WriteMode};
6666

67-
pub type FmtResult<T> = std::result::Result<T, failure::Error>;
68-
6967
#[macro_use]
7068
mod utils;
7169

@@ -107,7 +105,7 @@ pub(crate) type FileMap = Vec<FileRecord>;
107105

108106
pub(crate) type FileRecord = (FileName, String);
109107

110-
#[derive(Fail, Debug, Clone, Copy)]
108+
#[derive(Fail, Debug)]
111109
pub enum ErrorKind {
112110
// Line has exceeded character limit (found, maximum)
113111
#[fail(
@@ -132,6 +130,14 @@ pub enum ErrorKind {
132130
// Used a rustfmt:: attribute other than skip
133131
#[fail(display = "invalid attribute")]
134132
BadAttr,
133+
#[fail(display = "io error: {}", _0)]
134+
IoError(io::Error),
135+
}
136+
137+
impl From<io::Error> for ErrorKind {
138+
fn from(e: io::Error) -> ErrorKind {
139+
ErrorKind::IoError(e)
140+
}
135141
}
136142

137143
struct FormattingError {
@@ -162,7 +168,9 @@ impl FormattingError {
162168
}
163169
fn msg_prefix(&self) -> &str {
164170
match self.kind {
165-
ErrorKind::LineOverflow(..) | ErrorKind::TrailingWhitespace => "internal error:",
171+
ErrorKind::LineOverflow(..) | ErrorKind::TrailingWhitespace | ErrorKind::IoError(_) => {
172+
"internal error:"
173+
}
166174
ErrorKind::LicenseCheck | ErrorKind::BadAttr => "error:",
167175
ErrorKind::BadIssue(_) | ErrorKind::DeprecatedAttr => "warning:",
168176
}
@@ -244,6 +252,7 @@ impl FormatReport {
244252
| ErrorKind::BadAttr => {
245253
errs.has_check_errors = true;
246254
}
255+
_ => {}
247256
}
248257
}
249258
}
@@ -469,7 +478,7 @@ fn should_report_error(
469478
config: &Config,
470479
char_kind: FullCodeCharKind,
471480
is_string: bool,
472-
error_kind: ErrorKind,
481+
error_kind: &ErrorKind,
473482
) -> bool {
474483
let allow_error_report = if char_kind.is_comment() || is_string {
475484
config.error_on_unformatted()
@@ -541,7 +550,8 @@ fn format_lines(
541550
if format_line {
542551
// Check for (and record) trailing whitespace.
543552
if let Some(..) = last_wspace {
544-
if should_report_error(config, kind, is_string, ErrorKind::TrailingWhitespace) {
553+
if should_report_error(config, kind, is_string, &ErrorKind::TrailingWhitespace)
554+
{
545555
trims.push((cur_line, kind, line_buffer.clone()));
546556
}
547557
line_len -= 1;
@@ -551,7 +561,7 @@ fn format_lines(
551561
let error_kind = ErrorKind::LineOverflow(line_len, config.max_width());
552562
if line_len > config.max_width()
553563
&& !is_skipped_line(cur_line, skipped_range)
554-
&& should_report_error(config, kind, is_string, error_kind)
564+
&& should_report_error(config, kind, is_string, &error_kind)
555565
{
556566
errors.push(FormattingError {
557567
line: cur_line,
@@ -967,7 +977,7 @@ pub enum Input {
967977
Text(String),
968978
}
969979

970-
pub fn format_and_emit_report(input: Input, config: &Config) -> FmtResult<Summary> {
980+
pub fn format_and_emit_report(input: Input, config: &Config) -> Result<Summary, failure::Error> {
971981
if !config.version_meets_requirement() {
972982
return Err(format_err!("Version mismatch"));
973983
}
@@ -1000,15 +1010,15 @@ pub fn format_and_emit_report(input: Input, config: &Config) -> FmtResult<Summar
10001010
}
10011011
}
10021012

1003-
pub fn emit_pre_matter(config: &Config) -> FmtResult<()> {
1013+
pub fn emit_pre_matter(config: &Config) -> Result<(), ErrorKind> {
10041014
if config.write_mode() == WriteMode::Checkstyle {
10051015
let mut out = &mut stdout();
10061016
checkstyle::output_header(&mut out)?;
10071017
}
10081018
Ok(())
10091019
}
10101020

1011-
pub fn emit_post_matter(config: &Config) -> FmtResult<()> {
1021+
pub fn emit_post_matter(config: &Config) -> Result<(), ErrorKind> {
10121022
if config.write_mode() == WriteMode::Checkstyle {
10131023
let mut out = &mut stdout();
10141024
checkstyle::output_footer(&mut out)?;

0 commit comments

Comments
 (0)