use anstyle::AnsiColor;
use strum::IntoStaticStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, IntoStaticStr)]
pub enum CheckResult {
Ok,
Info,
Warning,
Error,
Fatal,
}
impl CheckResult {
const fn style(&self) -> anstyle::Style {
match self {
Self::Ok => AnsiColor::Green.on_default(),
Self::Info => AnsiColor::Green.on_default(),
Self::Warning => AnsiColor::Yellow.on_default(),
Self::Error => AnsiColor::Red.on_default(),
Self::Fatal => AnsiColor::Red.on_default(),
}
}
}
impl std::fmt::Display for CheckResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let style = self.style();
let rendered = style.render();
let reset = style.render_reset();
let stringified: &'static str = self.into();
write!(f, "{rendered}")?;
stringified.fmt(f)?;
write!(f, "{reset}")?;
Ok(())
}
}
pub type CheckFn = fn() -> Result<(CheckResult, String), Box<dyn std::error::Error + Send + Sync>>;
#[derive(Debug)]
pub struct Check {
pub(crate) name: &'static str,
pub(crate) func: CheckFn,
}
impl Check {
pub const fn new(name: &'static str, func: CheckFn) -> Self {
Self { name, func }
}
}