use once_cell::sync::Lazy;
use owo_colors::OwoColorize;
use std::collections::HashMap;
type ColorFn = Box<dyn Fn(&str) -> String + Send + Sync>;
type ColorMapType = HashMap<&'static str, ColorFn>;
type Color = (&'static str, fn(&str) -> String);
const COLORS: [Color; 5] = [
("red", |text: &str| text.red().to_string()),
("green", |text: &str| text.green().to_string()),
("white", |text: &str| text.white().to_string()),
("cyan", |text: &str| text.cyan().to_string()),
("yellow", |text: &str| text.yellow().to_string()),
];
#[rustfmt::skip]
pub static COLOR_MAP: Lazy<ColorMapType> = Lazy::new(|| {
let mut map: ColorMapType = HashMap::new();
for &color in &COLORS {
map.insert(color.0, Box::new(color.1));
}
map
});
#[cfg(test)]
mod color_map_tests {
use super::*;
#[test]
fn color_map_contains_expected_colors() {
let expected_colors = vec!["red", "green", "white", "cyan", "yellow"];
for color in expected_colors {
assert!(COLOR_MAP.contains_key(color));
}
}
#[test]
fn color_map_applies_correct_color() {
let red_text = (COLOR_MAP.get("red").unwrap())("test");
assert_eq!(red_text, "test".red().to_string());
}
#[test]
fn color_map_returns_none_for_unknown_color() {
assert!(COLOR_MAP.get("unknown_color").is_none());
}
#[test]
fn color_map_handles_empty_string() {
let red_text = (COLOR_MAP.get("red").unwrap())("");
assert_eq!(red_text, "".red().to_string());
}
}