use alloc::borrow::Cow;
use anstyle::Style;
use crate::renderer::stylesheet::Stylesheet;
use crate::snippet::{ERROR_TXT, HELP_TXT, INFO_TXT, NOTE_TXT, WARNING_TXT};
use crate::{Message, OptionCow, Title};
pub const ERROR: Level<'_> = Level {
name: None,
level: LevelInner::Error,
};
pub const WARNING: Level<'_> = Level {
name: None,
level: LevelInner::Warning,
};
pub const INFO: Level<'_> = Level {
name: None,
level: LevelInner::Info,
};
pub const NOTE: Level<'_> = Level {
name: None,
level: LevelInner::Note,
};
pub const HELP: Level<'_> = Level {
name: None,
level: LevelInner::Help,
};
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Level<'a> {
pub(crate) name: Option<Option<Cow<'a, str>>>,
pub(crate) level: LevelInner,
}
impl<'a> Level<'a> {
pub const ERROR: Level<'a> = ERROR;
pub const WARNING: Level<'a> = WARNING;
pub const INFO: Level<'a> = INFO;
pub const NOTE: Level<'a> = NOTE;
pub const HELP: Level<'a> = HELP;
}
impl<'a> Level<'a> {
pub fn primary_title(self, text: impl Into<Cow<'a, str>>) -> Title<'a> {
Title {
level: self,
id: None,
text: text.into(),
allows_styling: false,
}
}
pub fn secondary_title(self, text: impl Into<Cow<'a, str>>) -> Title<'a> {
Title {
level: self,
id: None,
text: text.into(),
allows_styling: true,
}
}
pub fn message(self, text: impl Into<Cow<'a, str>>) -> Message<'a> {
Message {
level: self,
text: text.into(),
}
}
pub(crate) fn as_str(&'a self) -> &'a str {
match (&self.name, self.level) {
(Some(Some(name)), _) => name.as_ref(),
(Some(None), _) => "",
(None, LevelInner::Error) => ERROR_TXT,
(None, LevelInner::Warning) => WARNING_TXT,
(None, LevelInner::Info) => INFO_TXT,
(None, LevelInner::Note) => NOTE_TXT,
(None, LevelInner::Help) => HELP_TXT,
}
}
pub(crate) fn style(&self, stylesheet: &Stylesheet) -> Style {
self.level.style(stylesheet)
}
}
impl<'a> Level<'a> {
#[doc = include_str!("../examples/custom_level.rs")]
#[doc = include_str!("../examples/custom_level.svg")]
pub fn with_name(self, name: impl Into<OptionCow<'a>>) -> Level<'a> {
Level {
name: Some(name.into().0),
level: self.level,
}
}
pub fn no_name(self) -> Level<'a> {
self.with_name(None::<&str>)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum LevelInner {
Error,
Warning,
Info,
Note,
Help,
}
impl LevelInner {
pub(crate) fn style(self, stylesheet: &Stylesheet) -> Style {
match self {
LevelInner::Error => stylesheet.error,
LevelInner::Warning => stylesheet.warning,
LevelInner::Info => stylesheet.info,
LevelInner::Note => stylesheet.note,
LevelInner::Help => stylesheet.help,
}
}
}