use crate::TermColor::{self, *};
use std::fmt::Display;
use std::str::FromStr;
#[cfg(feature = "config")]
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
#[cfg_attr(feature = "config", derive(Serialize, Deserialize))]
pub enum Level {
Debug = 0,
Info = 1,
Warn = 2,
Error = 3,
None = 4
}
impl Level {
pub fn get_color(&self) -> TermColor {
match self {
Level::Debug => Cyan,
Level::Info => Green,
Level::Warn => Yellow,
Level::Error => Red,
Level::None => Reset
}
}
}
impl Display for Level {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Level::Debug => write!(f, "DEBUG"),
Level::Info => write!(f, "INFO"),
Level::Warn => write!(f, "WARN"),
Level::Error => write!(f, "ERROR"),
Level::None => write!(f, "NONE")
}
}
}
impl FromStr for Level {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_uppercase().as_str() {
"DEBUG" => Ok(Level::Debug),
"INFO" => Ok(Level::Info),
"WARN" => Ok(Level::Warn),
"ERROR" => Ok(Level::Error),
"NONE" => Ok(Level::None),
_ => Err(format!("Invalid level: {}", s))
}
}
}
impl Default for Level {
fn default() -> Self {
Level::Info
}
}