hex_patch/app/log/
notification.rs

1use std::fmt::{Display, Formatter};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord)]
4pub enum NotificationLevel {
5    #[default]
6    None = 0,
7    Debug = 1,
8    Info = 2,
9    Warning = 3,
10    Error = 4,
11}
12
13impl NotificationLevel {
14    pub fn bump_notification_level(&mut self, new_notification_level: NotificationLevel) {
15        if new_notification_level.notification_level_as_u8() > self.notification_level_as_u8() {
16            *self = new_notification_level;
17        }
18    }
19
20    pub fn reset(&mut self) {
21        *self = NotificationLevel::None;
22    }
23
24    pub fn notification_level_as_u8(&self) -> u8 {
25        *self as u8
26    }
27}
28
29impl From<u8> for NotificationLevel {
30    fn from(value: u8) -> Self {
31        match value {
32            0 => NotificationLevel::None,
33            1 => NotificationLevel::Debug,
34            2 => NotificationLevel::Info,
35            3 => NotificationLevel::Warning,
36            4 => NotificationLevel::Error,
37            _ => NotificationLevel::None,
38        }
39    }
40}
41
42impl Display for NotificationLevel {
43    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
44        let max_len = t!("app.log_levels.none")
45            .chars()
46            .count()
47            .max(t!("app.log_levels.debug").chars().count())
48            .max(t!("app.log_levels.info").chars().count())
49            .max(t!("app.log_levels.warn").chars().count())
50            .max(t!("app.log_levels.error").chars().count());
51
52        match self {
53            NotificationLevel::None => write!(f, "{:<max_len$}", t!("app.log_levels.none")),
54            NotificationLevel::Debug => write!(f, "{:<max_len$}", t!("app.log_levels.debug")),
55            NotificationLevel::Info => write!(f, "{:<max_len$}", t!("app.log_levels.info")),
56            NotificationLevel::Warning => write!(f, "{:<max_len$}", t!("app.log_levels.warn")),
57            NotificationLevel::Error => write!(f, "{:<max_len$}", t!("app.log_levels.error")),
58        }
59    }
60}