extern crate codespan;
pub extern crate termcolor;
use std::cmp::Ordering;
use std::fmt;
use std::str::FromStr;
use termcolor::{Color, ColorChoice};
mod diagnostic;
mod emitter;
pub use self::diagnostic::{Diagnostic, Label, LabelStyle};
pub use self::emitter::emit;
#[derive(Copy, Clone, PartialEq, Hash, Debug)]
pub enum Severity {
Bug,
Error,
Warning,
Note,
Help,
}
impl Severity {
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)
}
}
impl Severity {
pub fn color(self) -> Color {
match self {
Severity::Bug | Severity::Error => Color::Red,
Severity::Warning => Color::Yellow,
Severity::Note => Color::Green,
Severity::Help => Color::Cyan,
}
}
pub fn to_str(self) -> &'static str {
match self {
Severity::Bug => "error: internal compiler error",
Severity::Error => "error",
Severity::Warning => "warning",
Severity::Note => "note",
Severity::Help => "help",
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct ColorArg(pub ColorChoice);
impl ColorArg {
pub const VARIANTS: &'static [&'static str] = &["auto", "always", "ansi", "never"];
}
impl FromStr for ColorArg {
type Err = &'static str;
fn from_str(src: &str) -> Result<ColorArg, &'static str> {
match src {
_ if src.eq_ignore_ascii_case("auto") => Ok(ColorArg(ColorChoice::Auto)),
_ if src.eq_ignore_ascii_case("always") => Ok(ColorArg(ColorChoice::Always)),
_ if src.eq_ignore_ascii_case("ansi") => Ok(ColorArg(ColorChoice::AlwaysAnsi)),
_ if src.eq_ignore_ascii_case("never") => Ok(ColorArg(ColorChoice::Never)),
_ => Err("valid values: auto, always, ansi, never"),
}
}
}
impl Into<ColorChoice> for ColorArg {
fn into(self) -> ColorChoice {
self.0
}
}