boseiju/ability_tree/terminals/
card_property.rs

1use crate::lexer::IntoToken;
2
3#[derive(idris_derive::Idris)]
4#[derive(serde::Serialize, serde::Deserialize)]
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
6pub enum CardProperty {
7    BasePower {
8        #[cfg(feature = "spanned_tree")]
9        span: crate::ability_tree::span::TreeSpan,
10    },
11    BasePowerAndToughness {
12        #[cfg(feature = "spanned_tree")]
13        span: crate::ability_tree::span::TreeSpan,
14    },
15    BaseToughness {
16        #[cfg(feature = "spanned_tree")]
17        span: crate::ability_tree::span::TreeSpan,
18    },
19    Color {
20        #[cfg(feature = "spanned_tree")]
21        span: crate::ability_tree::span::TreeSpan,
22    },
23    ManaValue {
24        #[cfg(feature = "spanned_tree")]
25        span: crate::ability_tree::span::TreeSpan,
26    },
27    Name {
28        #[cfg(feature = "spanned_tree")]
29        span: crate::ability_tree::span::TreeSpan,
30    },
31    Power {
32        #[cfg(feature = "spanned_tree")]
33        span: crate::ability_tree::span::TreeSpan,
34    },
35    Tougness {
36        #[cfg(feature = "spanned_tree")]
37        span: crate::ability_tree::span::TreeSpan,
38    },
39}
40
41#[cfg(feature = "spanned_tree")]
42impl CardProperty {
43    pub fn span(&self) -> crate::ability_tree::span::TreeSpan {
44        match self {
45            Self::BasePower { span } => *span,
46            Self::BasePowerAndToughness { span } => *span,
47            Self::BaseToughness { span } => *span,
48            Self::Color { span } => *span,
49            Self::ManaValue { span } => *span,
50            Self::Name { span } => *span,
51            Self::Power { span } => *span,
52            Self::Tougness { span } => *span,
53        }
54    }
55}
56
57impl std::fmt::Display for CardProperty {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        match self {
60            CardProperty::BasePower { .. } => write!(f, "converted mana cost"),
61            CardProperty::BasePowerAndToughness { .. } => write!(f, "converted mana cost"),
62            CardProperty::BaseToughness { .. } => write!(f, "converted mana cost"),
63            CardProperty::Color { .. } => write!(f, "converted mana cost"),
64            CardProperty::ManaValue { .. } => write!(f, "converted mana cost"),
65            CardProperty::Name { .. } => write!(f, "converted mana cost"),
66            CardProperty::Power { .. } => write!(f, "power"),
67            CardProperty::Tougness { .. } => write!(f, "touhness"),
68        }
69    }
70}
71
72#[cfg(feature = "lexer")]
73impl IntoToken for CardProperty {
74    fn try_from_span(span: &crate::lexer::Span) -> Option<Self> {
75        match span.text {
76            "base power" => Some(CardProperty::BasePower {
77                #[cfg(feature = "spanned_tree")]
78                span: span.into(),
79            }),
80            "base power and toughness" => Some(CardProperty::BasePowerAndToughness {
81                #[cfg(feature = "spanned_tree")]
82                span: span.into(),
83            }),
84            "base toughness" => Some(CardProperty::BaseToughness {
85                #[cfg(feature = "spanned_tree")]
86                span: span.into(),
87            }),
88            "color" | "colors" => Some(CardProperty::Color {
89                #[cfg(feature = "spanned_tree")]
90                span: span.into(),
91            }),
92            "mana cost" | "mana value" => Some(CardProperty::ManaValue {
93                #[cfg(feature = "spanned_tree")]
94                span: span.into(),
95            }),
96            "name" => Some(CardProperty::Name {
97                #[cfg(feature = "spanned_tree")]
98                span: span.into(),
99            }),
100            "power" => Some(CardProperty::Power {
101                #[cfg(feature = "spanned_tree")]
102                span: span.into(),
103            }),
104            "toughness" => Some(CardProperty::Tougness {
105                #[cfg(feature = "spanned_tree")]
106                span: span.into(),
107            }),
108            _ => None,
109        }
110    }
111}