mtg_data/
legality.rs

1#[derive(serde::Serialize, serde::Deserialize)]
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
3#[cfg_attr(feature = "ts_export", derive(ts_rs::TS))]
4pub enum Legality {
5    Legal,
6    Notlegal,
7    Restricted,
8    Banned,
9}
10
11impl std::str::FromStr for Legality {
12    type Err = String;
13    fn from_str(s: &str) -> Result<Self, Self::Err> {
14        match s {
15            "legal" => Ok(Self::Legal),
16            "not_legal" => Ok(Self::Notlegal),
17            "restricted" => Ok(Self::Restricted),
18            "banned" => Ok(Self::Banned),
19            other => Err(format!("Unknown Legality: {}", other.to_string())),
20        }
21    }
22}
23
24impl Legality {
25    pub fn as_str(&self) -> &'static str {
26        match self {
27            Self::Legal => "legal",
28            Self::Notlegal => "not_legal",
29            Self::Restricted => "restricted",
30            Self::Banned => "banned",
31        }
32    }
33}
34
35impl std::fmt::Display for Legality {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        write!(f, "{}", self.as_str())
38    }
39}
40
41impl Legality {
42    pub fn all() -> impl Iterator<Item = Self> {
43        [Self::Legal, Self::Notlegal, Self::Restricted, Self::Banned].into_iter()
44    }
45}