1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::fmt;

use console::{style, Color};

/**
    Label enum used for consistent output formatting throughout Lune.

    # Example usage

    ```rs
    use lune_utils::fmt::Label;

    println!("{} This is an info message", Label::Info);
    // [INFO] This is an info message

    println!("{} This is a warning message", Label::Warn);
    // [WARN] This is a warning message

    println!("{} This is an error message", Label::Error);
    // [ERROR] This is an error message
    ```
*/
#[derive(Debug, Clone, Copy)]
pub enum Label {
    Info,
    Warn,
    Error,
}

impl Label {
    /**
        Returns the name of the label in all uppercase.
    */
    #[must_use]
    pub fn name(&self) -> &str {
        match self {
            Self::Info => "INFO",
            Self::Warn => "WARN",
            Self::Error => "ERROR",
        }
    }

    /**
        Returns the color of the label.
    */
    #[must_use]
    pub fn color(&self) -> Color {
        match self {
            Self::Info => Color::Blue,
            Self::Warn => Color::Yellow,
            Self::Error => Color::Red,
        }
    }
}

impl fmt::Display for Label {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}{}{}",
            style("[").dim(),
            style(self.name()).fg(self.color()),
            style("]").dim()
        )
    }
}