use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorName {
White,
Black,
Red,
Green,
Blue,
Yellow,
Cyan,
Magenta,
}
impl ColorName {
pub fn as_str(self) -> &'static str {
match self {
ColorName::White => "white",
ColorName::Black => "black",
ColorName::Red => "red",
ColorName::Green => "green",
ColorName::Blue => "blue",
ColorName::Yellow => "yellow",
ColorName::Cyan => "cyan",
ColorName::Magenta => "magenta",
}
}
#[allow(clippy::should_implement_trait)]
pub fn from_str<S: AsRef<str>>(s: S) -> Option<ColorName> {
let s = s.as_ref().to_lowercase();
match s.as_str() {
"white" => Some(ColorName::White),
"black" => Some(ColorName::Black),
"red" => Some(ColorName::Red),
"green" => Some(ColorName::Green),
"blue" => Some(ColorName::Blue),
"yellow" => Some(ColorName::Yellow),
"cyan" => Some(ColorName::Cyan),
"magenta" => Some(ColorName::Magenta),
_ => None,
}
}
}
impl FromStr for ColorName {
type Err = ();
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
ColorName::from_str(s).ok_or(())
}
}