Skip to content

Commit

Permalink
Use failure for FormattingError
Browse files Browse the repository at this point in the history
  • Loading branch information
t-botz committed Apr 26, 2018
1 parent efb8069 commit 5581be2
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/lib.rs
Expand Up @@ -19,6 +19,7 @@
#[macro_use]
extern crate derive_new;
extern crate diff;
extern crate failure;
extern crate getopts;
extern crate itertools;
#[cfg(test)]
Expand Down Expand Up @@ -53,6 +54,7 @@ use syntax::errors::{DiagnosticBuilder, Handler};
use syntax::parse::{self, ParseSess};

use comment::{CharClasses, FullCodeCharKind, LineClasses};
use failure::Fail;
use issues::{BadIssueSeeker, Issue};
use shape::Indent;
use utils::use_colored_tty;
Expand Down Expand Up @@ -109,33 +111,26 @@ pub(crate) type FileMap = Vec<FileRecord>;

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

#[derive(Clone, Copy)]
#[derive(Fail, Debug, Clone, Copy)]
pub enum ErrorKind {
// Line has exceeded character limit (found, maximum)
#[fail(
display = "line formatted, but exceeded maximum width (maximum: {} (see `max_width` option), found: {})",
_0,
_1
)]
LineOverflow(usize, usize),
// Line ends in whitespace
#[fail(display = "left behind trailing whitespace")]
TrailingWhitespace,
// TODO or FIXME item without an issue number
#[fail(display = "found {}", _0)]
BadIssue(Issue),
// License check has failed
#[fail(display = "license check failed")]
LicenseCheck,
}

impl fmt::Display for ErrorKind {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
ErrorKind::LineOverflow(found, maximum) => write!(
fmt,
"line formatted, but exceeded maximum width (maximum: {} (see `max_width` option), found: {})",
maximum, found,
),
ErrorKind::TrailingWhitespace => write!(fmt, "left behind trailing whitespace"),
ErrorKind::BadIssue(issue) => write!(fmt, "found {}", issue),
ErrorKind::LicenseCheck => write!(fmt, "license check failed"),
}
}
}

// Formatting errors that are identified *after* rustfmt has run.
struct FormattingError {
line: usize,
Expand Down

0 comments on commit 5581be2

Please sign in to comment.