2015-05-22 22:17:57 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2015-07-18 18:31:09 +00:00
|
|
|
#[cfg(all(feature = "color", not(target_os = "windows")))]
|
2015-05-22 22:17:57 +00:00
|
|
|
use ansi_term::Colour::{Red, Green, Yellow};
|
2015-07-18 18:31:09 +00:00
|
|
|
#[cfg(all(feature = "color", not(target_os = "windows")))]
|
2015-05-22 22:17:57 +00:00
|
|
|
use ansi_term::ANSIString;
|
|
|
|
|
|
|
|
|
|
|
|
pub enum Format<T> {
|
|
|
|
Error(T),
|
|
|
|
Warning(T),
|
|
|
|
Good(T),
|
|
|
|
}
|
|
|
|
|
2015-07-18 18:31:09 +00:00
|
|
|
#[cfg(all(feature = "color", not(target_os = "windows")))]
|
2015-05-22 22:17:57 +00:00
|
|
|
impl<T: AsRef<str>> Format<T> {
|
|
|
|
fn format(&self) -> ANSIString {
|
|
|
|
match *self {
|
|
|
|
Format::Error(ref e) => Red.bold().paint(e.as_ref()),
|
|
|
|
Format::Warning(ref e) => Yellow.paint(e.as_ref()),
|
|
|
|
Format::Good(ref e) => Green.paint(e.as_ref()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-18 18:31:09 +00:00
|
|
|
#[cfg(all(feature = "color", not(target_os = "windows")))]
|
2015-05-22 22:17:57 +00:00
|
|
|
impl<T: AsRef<str>> fmt::Display for Format<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", &self.format())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-18 18:31:09 +00:00
|
|
|
#[cfg(any(not(feature = "color"), target_os = "windows"))]
|
2015-05-22 22:17:57 +00:00
|
|
|
impl<T: fmt::Display> Format<T> {
|
|
|
|
fn format(&self) -> &T {
|
|
|
|
match *self {
|
|
|
|
Format::Error(ref e) => e,
|
|
|
|
Format::Warning(ref e) => e,
|
|
|
|
Format::Good(ref e) => e,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-18 18:31:09 +00:00
|
|
|
#[cfg(any(not(feature = "color"), target_os = "windows"))]
|
2015-05-22 22:17:57 +00:00
|
|
|
impl<T: fmt::Display> fmt::Display for Format<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", &self.format())
|
|
|
|
}
|
|
|
|
}
|