use std::cmp::Ordering;
use yansi::Color;
const TRACE: LogLevel = LogLevel::new(10, Color::Fixed(102), "trace", '•');
const DEBUG: LogLevel = LogLevel::new(20, Color::Green, "debug", '•');
const INFO: LogLevel = LogLevel::new(30, Color::Blue, "info", '•');
const WARN: LogLevel = LogLevel::new(40, Color::Yellow, "warn", '⚠');
const ERROR: LogLevel = LogLevel::new(50, Color::Red, "error", '×');
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct LogLevel {
level: u8,
color: Color,
tag: &'static str,
symbol: char,
}
impl LogLevel {
pub const fn new(level: u8, color: Color, tag: &'static str, symbol: char) -> LogLevel {
LogLevel {
level,
tag,
color,
symbol,
}
}
pub const fn level(&self) -> u8 {
self.level
}
pub const fn tag(&self) -> &'static str {
self.tag
}
pub const fn color(&self) -> Color {
self.color
}
pub const fn symbol(&self) -> char {
self.symbol
}
pub const fn trace() -> LogLevel {
TRACE
}
pub const fn debug() -> LogLevel {
DEBUG
}
pub const fn info() -> LogLevel {
INFO
}
pub const fn warn() -> LogLevel {
WARN
}
pub const fn error() -> LogLevel {
ERROR
}
}
impl PartialOrd for LogLevel {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for LogLevel {
fn cmp(&self, other: &Self) -> Ordering {
self.level.cmp(&other.level)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_order() {
assert!(TRACE < DEBUG, "TRACE is not less than DEBUG");
assert!(DEBUG < INFO, "DEBUG is not less than INFO");
assert!(INFO < WARN, "INFO is not less than WARN");
assert!(WARN < ERROR, "WARN is not less than ERROR");
}
}