|
| 1 | +use crate::renderer::stylesheet::Stylesheet; |
| 2 | +use crate::snippet::{ERROR_TXT, HELP_TXT, INFO_TXT, NOTE_TXT, WARNING_TXT}; |
| 3 | +use crate::{Element, Group, Message, Title}; |
| 4 | +use anstyle::Style; |
| 5 | + |
| 6 | +pub const ERROR: Level<'_> = Level { |
| 7 | + name: None, |
| 8 | + level: LevelInner::Error, |
| 9 | +}; |
| 10 | + |
| 11 | +pub const WARNING: Level<'_> = Level { |
| 12 | + name: None, |
| 13 | + level: LevelInner::Warning, |
| 14 | +}; |
| 15 | + |
| 16 | +pub const INFO: Level<'_> = Level { |
| 17 | + name: None, |
| 18 | + level: LevelInner::Info, |
| 19 | +}; |
| 20 | + |
| 21 | +pub const NOTE: Level<'_> = Level { |
| 22 | + name: None, |
| 23 | + level: LevelInner::Note, |
| 24 | +}; |
| 25 | + |
| 26 | +pub const HELP: Level<'_> = Level { |
| 27 | + name: None, |
| 28 | + level: LevelInner::Help, |
| 29 | +}; |
| 30 | + |
| 31 | +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 32 | +pub struct Level<'a> { |
| 33 | + pub(crate) name: Option<Option<&'a str>>, |
| 34 | + pub(crate) level: LevelInner, |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 38 | +pub enum Level2<'a> { |
| 39 | + Builtin(LevelInner), |
| 40 | + Custom { |
| 41 | + name: Option<&'a str>, |
| 42 | + level: LevelInner, |
| 43 | + }, |
| 44 | + None, |
| 45 | +} |
| 46 | + |
| 47 | +impl<'a> Level<'a> { |
| 48 | + pub const ERROR: Level<'a> = ERROR; |
| 49 | + pub const WARNING: Level<'a> = WARNING; |
| 50 | + pub const INFO: Level<'a> = INFO; |
| 51 | + pub const NOTE: Level<'a> = NOTE; |
| 52 | + pub const HELP: Level<'a> = HELP; |
| 53 | + |
| 54 | + /// Text passed to this function is considered "untrusted input", as such |
| 55 | + /// all text is passed through a normalization function. Pre-styled text is |
| 56 | + /// not allowed to be passed to this function. |
| 57 | + pub fn text(self, text: Option<&'a str>) -> Level<'a> { |
| 58 | + Level { |
| 59 | + name: Some(text), |
| 60 | + level: self.level, |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<'a> Level<'a> { |
| 66 | + /// Text passed to this function is considered "untrusted input", as such |
| 67 | + /// all text is passed through a normalization function. Pre-styled text is |
| 68 | + /// not allowed to be passed to this function. |
| 69 | + pub fn message(self, title: &'a str) -> Message<'a> { |
| 70 | + Message { |
| 71 | + id: None, |
| 72 | + groups: vec![Group::new().element(Element::Title(Title { |
| 73 | + level: self, |
| 74 | + title, |
| 75 | + primary: true, |
| 76 | + }))], |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /// Text passed to this function is allowed to be pre-styled, as such all |
| 81 | + /// text is considered "trusted input" and has no normalizations applied to |
| 82 | + /// it. [`normalize_untrusted_str`](crate::normalize_untrusted_str) can be |
| 83 | + /// used to normalize untrusted text before it is passed to this function. |
| 84 | + pub fn title(self, title: &'a str) -> Title<'a> { |
| 85 | + Title { |
| 86 | + level: self, |
| 87 | + title, |
| 88 | + primary: false, |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + pub(crate) fn as_str(&self) -> &'a str { |
| 93 | + match (self.name, self.level) { |
| 94 | + (Some(Some(name)), _) => name, |
| 95 | + (Some(None), _) => "", |
| 96 | + (None, LevelInner::Error) => ERROR_TXT, |
| 97 | + (None, LevelInner::Warning) => WARNING_TXT, |
| 98 | + (None, LevelInner::Info) => INFO_TXT, |
| 99 | + (None, LevelInner::Note) => NOTE_TXT, |
| 100 | + (None, LevelInner::Help) => HELP_TXT, |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + pub(crate) fn style(&self, stylesheet: &Stylesheet) -> Style { |
| 105 | + self.level.style(stylesheet) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 110 | +pub enum LevelInner { |
| 111 | + Error, |
| 112 | + Warning, |
| 113 | + Info, |
| 114 | + Note, |
| 115 | + Help, |
| 116 | +} |
| 117 | + |
| 118 | +impl LevelInner { |
| 119 | + pub(crate) fn style(self, stylesheet: &Stylesheet) -> Style { |
| 120 | + match self { |
| 121 | + LevelInner::Error => stylesheet.error, |
| 122 | + LevelInner::Warning => stylesheet.warning, |
| 123 | + LevelInner::Info => stylesheet.info, |
| 124 | + LevelInner::Note => stylesheet.note, |
| 125 | + LevelInner::Help => stylesheet.help, |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments