Add a partial ordering on severity

This commit is contained in:
Brendan Zabarauskas 2018-02-20 20:32:22 +11:00
parent 2e3ac6188d
commit 9e0b229057

View File

@ -1,6 +1,7 @@
extern crate codespan;
extern crate termcolor;
use std::cmp::Ordering;
use std::fmt;
use termcolor::Color;
@ -11,7 +12,18 @@ pub use self::diagnostic::{Diagnostic, Label, LabelStyle};
pub use self::emitter::emit;
/// A severity level for diagnostic messages
#[derive(Copy, PartialEq, Clone, Hash, Debug)]
///
/// These are ordered in the following way:
///
/// ```rust
/// use codespan_reporting::Severity;
///
/// assert!(Severity::Bug > Severity::Error);
/// assert!(Severity::Error > Severity::Warning);
/// assert!(Severity::Warning > Severity::Note);
/// assert!(Severity::Note > Severity::Help);
/// ```
#[derive(Copy, Clone, PartialEq, Hash, Debug)]
pub enum Severity {
/// An unexpected bug.
Bug,
@ -25,6 +37,25 @@ pub enum Severity {
Help,
}
impl Severity {
/// We want bugs to be the maximum severity, errors next, etc...
fn to_cmp_int(self) -> u8 {
match self {
Severity::Bug => 5,
Severity::Error => 4,
Severity::Warning => 3,
Severity::Note => 2,
Severity::Help => 1,
}
}
}
impl PartialOrd for Severity {
fn partial_cmp(&self, other: &Severity) -> Option<Ordering> {
u8::partial_cmp(&self.to_cmp_int(), &other.to_cmp_int())
}
}
impl fmt::Display for Severity {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_str().fmt(f)