mtg_data/
card_type.rs

1#[derive(idris_derive::Idris)]
2#[idris(repr = usize)]
3#[derive(serde::Serialize, serde::Deserialize)]
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
5#[cfg_attr(feature = "ts_export", derive(ts_rs::TS))]
6pub enum CardType {
7    Artifact,
8    Battle,
9    Conspiracy,
10    Creature,
11    Dungeon,
12    Emblem,
13    Enchantment,
14    Hero,
15    Instant,
16    Kindred,
17    Land,
18    Phenomenon,
19    Plane,
20    Planeswalker,
21    Scheme,
22    Sorcery,
23    Vanguard,
24}
25
26impl std::str::FromStr for CardType {
27    type Err = crate::ParsingError;
28    fn from_str(s: &str) -> Result<Self, Self::Err> {
29        match s {
30            "artifact" => Ok(Self::Artifact),
31            "battle" => Ok(Self::Battle),
32            "conspiracy" => Ok(Self::Conspiracy),
33            "creature" => Ok(Self::Creature),
34            "dungeon" => Ok(Self::Dungeon),
35            "emblem" => Ok(Self::Emblem),
36            "enchantment" => Ok(Self::Enchantment),
37            "hero" => Ok(Self::Hero),
38            "instant" => Ok(Self::Instant),
39            "kindred" => Ok(Self::Kindred),
40            "land" => Ok(Self::Land),
41            "phenomenon" => Ok(Self::Phenomenon),
42            "plane" => Ok(Self::Plane),
43            "planeswalker" => Ok(Self::Planeswalker),
44            "scheme" => Ok(Self::Scheme),
45            "sorcery" => Ok(Self::Sorcery),
46            "vanguard" => Ok(Self::Vanguard),
47            _ => Err(crate::ParsingError {
48                item: "CardType",
49                message: "provided source does not match",
50            }),
51        }
52    }
53}
54
55impl CardType {
56    pub fn as_str(&self) -> &'static str {
57        match self {
58            Self::Artifact => "artifact",
59            Self::Battle => "battle",
60            Self::Conspiracy => "conspiracy",
61            Self::Creature => "creature",
62            Self::Dungeon => "dungeon",
63            Self::Emblem => "emblem",
64            Self::Enchantment => "enchantment",
65            Self::Hero => "hero",
66            Self::Instant => "instant",
67            Self::Kindred => "kindred",
68            Self::Land => "land",
69            Self::Phenomenon => "phenomenon",
70            Self::Plane => "plane",
71            Self::Planeswalker => "planeswalker",
72            Self::Scheme => "scheme",
73            Self::Sorcery => "sorcery",
74            Self::Vanguard => "vanguard",
75        }
76    }
77}
78
79impl std::fmt::Display for CardType {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        write!(f, "{}", self.as_str())
82    }
83}
84
85impl CardType {
86    pub fn all() -> impl Iterator<Item = Self> {
87        [
88            Self::Artifact,
89            Self::Battle,
90            Self::Conspiracy,
91            Self::Creature,
92            Self::Dungeon,
93            Self::Emblem,
94            Self::Enchantment,
95            Self::Hero,
96            Self::Instant,
97            Self::Kindred,
98            Self::Land,
99            Self::Phenomenon,
100            Self::Plane,
101            Self::Planeswalker,
102            Self::Scheme,
103            Self::Sorcery,
104            Self::Vanguard,
105        ].into_iter()
106    }
107}