use std::fmt;
#[derive(Debug, Clone)]
pub struct Icon {
pub glyph: &'static str,
pub color: Option<String>,
}
impl Icon {
pub fn new(glyph: &'static str) -> Self {
Self { glyph, color: None }
}
pub fn colored(mut self, color: impl Into<String>) -> Self {
self.color = Some(color.into());
self
}
pub fn glyph(&self) -> &'static str {
self.glyph
}
pub fn has_color(&self) -> bool {
self.color.is_some()
}
pub fn get_color(&self) -> Option<&str> {
self.color.as_deref()
}
}
impl fmt::Display for Icon {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.glyph)
}
}
impl From<&'static str> for Icon {
fn from(glyph: &'static str) -> Self {
Self::new(glyph)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_icon_creation() {
let icon = Icon::new("");
assert_eq!(icon.glyph(), "");
assert!(!icon.has_color());
}
#[test]
fn test_icon_with_color() {
let icon = Icon::new("").colored("#ff0000");
assert!(icon.has_color());
assert_eq!(icon.get_color(), Some("#ff0000"));
}
#[test]
fn test_icon_display() {
let icon = Icon::new("");
assert_eq!(format!("{}", icon), "");
}
}