mtg_data/
color.rs

1#[derive(idris_derive::Idris)]
2#[derive(serde::Serialize, serde::Deserialize)]
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
4#[cfg_attr(feature = "ts_export", derive(ts_rs::TS))]
5pub enum Color {
6    Black,
7    Blue,
8    Colorless,
9    Green,
10    Red,
11    White,
12}
13
14impl std::str::FromStr for Color {
15    type Err = String;
16    fn from_str(s: &str) -> Result<Self, Self::Err> {
17        match s {
18            "b" | "B" | "black" => Ok(Self::Black),
19            "u" | "U" | "blue" => Ok(Self::Blue),
20            "c" | "C" | "colorless" => Ok(Self::Colorless),
21            "g" | "G" | "green" => Ok(Self::Green),
22            "r" | "R" | "red" => Ok(Self::Red),
23            "w" | "W" | "white" => Ok(Self::White),
24            other => Err(format!("Unknown Color: {}", other.to_string())),
25        }
26    }
27}
28
29impl Color {
30    pub fn as_str(&self) -> &'static str {
31        match self {
32            Self::Black => "black",
33            Self::Blue => "blue",
34            Self::Colorless => "colorless",
35            Self::Green => "green",
36            Self::Red => "red",
37            Self::White => "white",
38        }
39    }
40
41    pub fn as_char(&self) -> char {
42        match self {
43            Self::Black => 'b',
44            Self::Blue => 'u',
45            Self::Colorless => 'c',
46            Self::Green => 'g',
47            Self::Red => 'r',
48            Self::White => 'w',
49        }
50    }
51}
52
53impl std::fmt::Display for Color {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        write!(f, "{}", self.as_str())
56    }
57}
58
59impl Color {
60    pub fn all() -> impl Iterator<Item = Self> {
61        [Self::Black, Self::Blue, Self::Colorless, Self::Green, Self::Red, Self::White].into_iter()
62    }
63}