Skip to main content

rnk_icons/
icon.rs

1//! Icon type with optional styling
2
3use std::fmt;
4
5/// An icon with optional color
6#[derive(Debug, Clone)]
7pub struct Icon {
8    /// The icon character(s)
9    pub glyph: &'static str,
10    /// Optional hex color (e.g., "#ff0000")
11    pub color: Option<String>,
12}
13
14impl Icon {
15    /// Create a new icon from a glyph
16    pub fn new(glyph: &'static str) -> Self {
17        Self { glyph, color: None }
18    }
19
20    /// Set the icon color (hex format)
21    pub fn colored(mut self, color: impl Into<String>) -> Self {
22        self.color = Some(color.into());
23        self
24    }
25
26    /// Get the glyph string
27    pub fn glyph(&self) -> &'static str {
28        self.glyph
29    }
30
31    /// Check if icon has a color
32    pub fn has_color(&self) -> bool {
33        self.color.is_some()
34    }
35
36    /// Get the color if set
37    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}