erebus 0.1.7

A CLI message generation library
Documentation
use yansi::Paint;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Severity {
    Error,
    Warning,
    Info,
    Debug,
    Trace,
    Help,
}

impl Severity {
    #[must_use]
    pub fn colorize(&self, text: &str) -> String {
        format!(
            "{}",
            match self {
                Self::Error => text.red(),
                Self::Warning => text.yellow(),
                Self::Info => text.blue(),
                Self::Debug => text.magenta(),
                Self::Trace => text.cyan(),
                Self::Help => text.green(),
            }
        )
    }

    #[must_use]
    pub const fn as_str(&self) -> &str {
        match self {
            Self::Error => "error",
            Self::Warning => "warning",
            Self::Info => "info",
            Self::Debug => "debug",
            Self::Trace => "trace",
            Self::Help => "help",
        }
    }
}

impl PartialOrd for Severity {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        let order = |severity: &Self| match severity {
            Self::Error => 5,
            Self::Warning => 4,
            Self::Help => 3,
            Self::Info => 2,
            Self::Debug => 1,
            Self::Trace => 0,
        };
        Some(order(self).cmp(&order(other)))
    }
}