lune_utils/fmt/label.rs
1use std::fmt;
2
3use console::{Color, style};
4
5/**
6 Label enum used for consistent output formatting throughout Lune.
7
8 # Example usage
9
10 ```rs
11 use lune_utils::fmt::Label;
12
13 println!("{} This is an info message", Label::Info);
14 // [INFO] This is an info message
15
16 println!("{} This is a warning message", Label::Warn);
17 // [WARN] This is a warning message
18
19 println!("{} This is an error message", Label::Error);
20 // [ERROR] This is an error message
21 ```
22*/
23#[derive(Debug, Clone, Copy)]
24pub enum Label {
25 Info,
26 Warn,
27 Error,
28}
29
30impl Label {
31 /**
32 Returns the name of the label in all uppercase.
33 */
34 #[must_use]
35 pub fn name(&self) -> &str {
36 match self {
37 Self::Info => "INFO",
38 Self::Warn => "WARN",
39 Self::Error => "ERROR",
40 }
41 }
42
43 /**
44 Returns the color of the label.
45 */
46 #[must_use]
47 pub fn color(&self) -> Color {
48 match self {
49 Self::Info => Color::Blue,
50 Self::Warn => Color::Yellow,
51 Self::Error => Color::Red,
52 }
53 }
54}
55
56impl fmt::Display for Label {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 write!(
59 f,
60 "{}{}{}",
61 style("[").dim(),
62 style(self.name()).fg(self.color()),
63 style("]").dim()
64 )
65 }
66}