1use std::fmt;
4
5#[derive(Debug, Clone)]
7pub struct Icon {
8 pub glyph: &'static str,
10 pub color: Option<String>,
12}
13
14impl Icon {
15 pub fn new(glyph: &'static str) -> Self {
17 Self { glyph, color: None }
18 }
19
20 pub fn colored(mut self, color: impl Into<String>) -> Self {
22 self.color = Some(color.into());
23 self
24 }
25
26 pub fn glyph(&self) -> &'static str {
28 self.glyph
29 }
30
31 pub fn has_color(&self) -> bool {
33 self.color.is_some()
34 }
35
36 pub fn get_color(&self) -> Option<&str> {
38 self.color.as_deref()
39 }
40}
41
42impl fmt::Display for Icon {
43 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44 write!(f, "{}", self.glyph)
45 }
46}
47
48impl From<&'static str> for Icon {
49 fn from(glyph: &'static str) -> Self {
50 Self::new(glyph)
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_icon_creation() {
60 let icon = Icon::new("");
61 assert_eq!(icon.glyph(), "");
62 assert!(!icon.has_color());
63 }
64
65 #[test]
66 fn test_icon_with_color() {
67 let icon = Icon::new("").colored("#ff0000");
68 assert!(icon.has_color());
69 assert_eq!(icon.get_color(), Some("#ff0000"));
70 }
71
72 #[test]
73 fn test_icon_display() {
74 let icon = Icon::new("");
75 assert_eq!(format!("{}", icon), "");
76 }
77}