boseiju/ability_tree/terminals/mtg_data_as_token/
mana.rs

1use crate::ability_tree::AbilityTreeNode;
2use crate::ability_tree::MAX_CHILDREN_PER_NODE;
3use crate::ability_tree::MAX_NODE_DATA_SIZE;
4use crate::lexer::IntoToken;
5
6/// Wrapper around the mana kind.
7#[derive(serde::Serialize, serde::Deserialize)]
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum Mana {
10    X {
11        #[cfg(feature = "spanned_tree")]
12        span: crate::ability_tree::span::TreeSpan,
13    },
14    Any(AnyMana),
15    Colored(ColoredMana),
16    Hybrid(HybridMana),
17    MonocoloredHybrid(MonocoloredHybridMana),
18    Phyrexian(PhyrexianMana),
19    HybridPhyrexian(HybridPhyrexianMana),
20    Snow {
21        #[cfg(feature = "spanned_tree")]
22        span: crate::ability_tree::span::TreeSpan,
23    },
24}
25
26impl Mana {
27    pub fn mana_value(&self) -> usize {
28        unimplemented!()
29    }
30
31    #[cfg(feature = "spanned_tree")]
32    pub fn span(&self) -> crate::ability_tree::span::TreeSpan {
33        match self {
34            Self::X { span } => *span,
35            Self::Any(child) => child.span,
36            Self::Colored(child) => child.span,
37            Self::Hybrid(child) => child.span,
38            Self::MonocoloredHybrid(child) => child.span,
39            Self::Phyrexian(child) => child.span,
40            Self::HybridPhyrexian(child) => child.span,
41            Self::Snow { span } => *span,
42        }
43    }
44}
45
46impl AbilityTreeNode for Mana {
47    fn node_id(&self) -> usize {
48        use crate::ability_tree::tree_node::MtgDataNodeKind;
49        use idris::Idris;
50
51        crate::ability_tree::NodeKind::MtgData(MtgDataNodeKind::ManaIdMarker).id()
52    }
53
54    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
55        use crate::ability_tree::dummy_terminal::TreeNodeDummyTerminal;
56        use crate::ability_tree::tree_node::MtgDataNodeKind;
57        use idris::Idris;
58
59        let mut children = arrayvec::ArrayVec::new_const();
60        match self {
61            Self::X { .. } => children.push(TreeNodeDummyTerminal::new(
62                crate::ability_tree::NodeKind::MtgData(MtgDataNodeKind::Mana(mtg_data::Mana::X)).id(),
63            ) as &dyn AbilityTreeNode),
64            Self::Snow { .. } => children.push(TreeNodeDummyTerminal::new(
65                crate::ability_tree::NodeKind::MtgData(MtgDataNodeKind::Mana(mtg_data::Mana::Snow)).id(),
66            ) as &dyn AbilityTreeNode),
67            Self::Any(child) => children.push(child as &dyn AbilityTreeNode),
68            Self::Colored(child) => children.push(child as &dyn AbilityTreeNode),
69            Self::Hybrid(child) => children.push(child as &dyn AbilityTreeNode),
70            Self::MonocoloredHybrid(child) => children.push(child as &dyn AbilityTreeNode),
71            Self::Phyrexian(child) => children.push(child as &dyn AbilityTreeNode),
72            Self::HybridPhyrexian(child) => children.push(child as &dyn AbilityTreeNode),
73        }
74        children
75    }
76
77    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
78        use std::io::Write;
79        match self {
80            Self::X { .. } => write!(out, "x")?,
81            Self::Snow { .. } => write!(out, "snow")?,
82            Self::Any(child) => child.display(out)?,
83            Self::Colored(child) => child.display(out)?,
84            Self::Hybrid(child) => child.display(out)?,
85            Self::MonocoloredHybrid(child) => child.display(out)?,
86            Self::Phyrexian(child) => child.display(out)?,
87            Self::HybridPhyrexian(child) => child.display(out)?,
88        }
89        Ok(())
90    }
91
92    fn node_tag(&self) -> &'static str {
93        "mana symbol"
94    }
95
96    #[cfg(feature = "spanned_tree")]
97    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
98        match self {
99            Self::X { span } => *span,
100            Self::Snow { span } => *span,
101            Self::Any(child) => child.node_span(),
102            Self::Colored(child) => child.node_span(),
103            Self::Hybrid(child) => child.node_span(),
104            Self::MonocoloredHybrid(child) => child.node_span(),
105            Self::Phyrexian(child) => child.node_span(),
106            Self::HybridPhyrexian(child) => child.node_span(),
107        }
108    }
109}
110
111#[cfg(feature = "lexer")]
112impl IntoToken for Mana {
113    fn try_from_span(span: &crate::lexer::Span) -> Option<Self> {
114        use std::str::FromStr;
115        Some(match mtg_data::Mana::from_str(&span.text).ok()? {
116            mtg_data::Mana::X => Self::X {
117                #[cfg(feature = "spanned_tree")]
118                span: span.into(),
119            },
120            mtg_data::Mana::Any(mana) => Self::Any(AnyMana {
121                mana,
122                #[cfg(feature = "spanned_tree")]
123                span: span.into(),
124            }),
125            mtg_data::Mana::Colored(mana) => Self::Colored(ColoredMana {
126                mana,
127                #[cfg(feature = "spanned_tree")]
128                span: span.into(),
129            }),
130            mtg_data::Mana::Hybrid(mana) => Self::Hybrid(HybridMana {
131                mana,
132                #[cfg(feature = "spanned_tree")]
133                span: span.into(),
134            }),
135            mtg_data::Mana::MonocoloredHybrid(mana) => Self::MonocoloredHybrid(MonocoloredHybridMana {
136                mana,
137                #[cfg(feature = "spanned_tree")]
138                span: span.into(),
139            }),
140            mtg_data::Mana::Phyrexian(mana) => Self::Phyrexian(PhyrexianMana {
141                mana,
142                #[cfg(feature = "spanned_tree")]
143                span: span.into(),
144            }),
145            mtg_data::Mana::HybridPhyrexian(mana) => Self::HybridPhyrexian(HybridPhyrexianMana {
146                mana,
147                #[cfg(feature = "spanned_tree")]
148                span: span.into(),
149            }),
150            mtg_data::Mana::Snow => Self::Snow {
151                #[cfg(feature = "spanned_tree")]
152                span: span.into(),
153            },
154        })
155    }
156}
157
158#[cfg(feature = "parser")]
159impl crate::utils::DummyInit for Mana {
160    fn dummy_init() -> Self {
161        Self::X {
162            #[cfg(feature = "spanned_tree")]
163            span: Default::default(),
164        }
165    }
166}
167
168/// A mana symbol with a number on it, representing a fixed amount of any kind of mana.
169#[derive(serde::Serialize, serde::Deserialize)]
170#[derive(Debug, Clone, PartialEq, Eq)]
171pub struct AnyMana {
172    pub mana: mtg_data::AnyMana,
173    #[cfg(feature = "spanned_tree")]
174    pub span: crate::ability_tree::span::TreeSpan,
175}
176
177impl AbilityTreeNode for AnyMana {
178    fn node_id(&self) -> usize {
179        use crate::ability_tree::tree_node::MtgDataNodeKind;
180        use idris::Idris;
181
182        crate::ability_tree::NodeKind::MtgData(MtgDataNodeKind::Mana(mtg_data::Mana::Any(self.mana))).id()
183    }
184
185    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
186        arrayvec::ArrayVec::new_const()
187    }
188
189    fn data(&self) -> arrayvec::ArrayVec<u8, MAX_NODE_DATA_SIZE> {
190        unimplemented!()
191    }
192
193    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
194        use std::io::Write;
195        write!(out, "{}", self.mana)
196    }
197
198    fn node_tag(&self) -> &'static str {
199        "numbered mana"
200    }
201
202    #[cfg(feature = "spanned_tree")]
203    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
204        self.span
205    }
206}
207
208/// A mana symbol with a number on it, representing a fixed amount of any kind of mana.
209#[derive(serde::Serialize, serde::Deserialize)]
210#[derive(Debug, Clone, PartialEq, Eq)]
211pub struct ColoredMana {
212    pub mana: mtg_data::ColoredMana,
213    #[cfg(feature = "spanned_tree")]
214    pub span: crate::ability_tree::span::TreeSpan,
215}
216
217impl AbilityTreeNode for ColoredMana {
218    fn node_id(&self) -> usize {
219        use crate::ability_tree::tree_node::MtgDataNodeKind;
220        use idris::Idris;
221
222        crate::ability_tree::NodeKind::MtgData(MtgDataNodeKind::Mana(mtg_data::Mana::Colored(self.mana))).id()
223    }
224
225    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
226        arrayvec::ArrayVec::new_const()
227    }
228
229    fn data(&self) -> arrayvec::ArrayVec<u8, MAX_NODE_DATA_SIZE> {
230        unimplemented!()
231    }
232
233    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
234        use std::io::Write;
235        write!(out, "{}", self.mana)
236    }
237
238    fn node_tag(&self) -> &'static str {
239        "colored mana"
240    }
241
242    #[cfg(feature = "spanned_tree")]
243    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
244        self.span
245    }
246}
247
248/// A mana symbol with a number on it, representing a fixed amount of any kind of mana.
249#[derive(serde::Serialize, serde::Deserialize)]
250#[derive(Debug, Clone, PartialEq, Eq)]
251pub struct HybridMana {
252    pub mana: mtg_data::HybridMana,
253    #[cfg(feature = "spanned_tree")]
254    pub span: crate::ability_tree::span::TreeSpan,
255}
256
257impl AbilityTreeNode for HybridMana {
258    fn node_id(&self) -> usize {
259        use crate::ability_tree::tree_node::MtgDataNodeKind;
260        use idris::Idris;
261
262        crate::ability_tree::NodeKind::MtgData(MtgDataNodeKind::Mana(mtg_data::Mana::Hybrid(self.mana))).id()
263    }
264
265    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
266        arrayvec::ArrayVec::new_const()
267    }
268
269    fn data(&self) -> arrayvec::ArrayVec<u8, MAX_NODE_DATA_SIZE> {
270        unimplemented!()
271    }
272
273    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
274        use std::io::Write;
275        write!(out, "{}", self.mana)
276    }
277
278    fn node_tag(&self) -> &'static str {
279        "hybrid mana"
280    }
281
282    #[cfg(feature = "spanned_tree")]
283    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
284        self.span
285    }
286}
287
288/// A mana symbol with a number on it, representing a fixed amount of any kind of mana.
289#[derive(serde::Serialize, serde::Deserialize)]
290#[derive(Debug, Clone, PartialEq, Eq)]
291pub struct MonocoloredHybridMana {
292    pub mana: mtg_data::MonocoloredHybridMana,
293    #[cfg(feature = "spanned_tree")]
294    pub span: crate::ability_tree::span::TreeSpan,
295}
296
297impl AbilityTreeNode for MonocoloredHybridMana {
298    fn node_id(&self) -> usize {
299        use crate::ability_tree::tree_node::MtgDataNodeKind;
300        use idris::Idris;
301
302        crate::ability_tree::NodeKind::MtgData(MtgDataNodeKind::Mana(mtg_data::Mana::MonocoloredHybrid(self.mana))).id()
303    }
304
305    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
306        arrayvec::ArrayVec::new_const()
307    }
308
309    fn data(&self) -> arrayvec::ArrayVec<u8, MAX_NODE_DATA_SIZE> {
310        unimplemented!()
311    }
312
313    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
314        use std::io::Write;
315        write!(out, "{}", self.mana)
316    }
317
318    fn node_tag(&self) -> &'static str {
319        "monocolored hybrid mana"
320    }
321
322    #[cfg(feature = "spanned_tree")]
323    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
324        self.span
325    }
326}
327
328/// A mana symbol with a number on it, representing a fixed amount of any kind of mana.
329#[derive(serde::Serialize, serde::Deserialize)]
330#[derive(Debug, Clone, PartialEq, Eq)]
331pub struct PhyrexianMana {
332    pub mana: mtg_data::PhyrexianMana,
333    #[cfg(feature = "spanned_tree")]
334    pub span: crate::ability_tree::span::TreeSpan,
335}
336
337impl AbilityTreeNode for PhyrexianMana {
338    fn node_id(&self) -> usize {
339        use crate::ability_tree::tree_node::MtgDataNodeKind;
340        use idris::Idris;
341
342        crate::ability_tree::NodeKind::MtgData(MtgDataNodeKind::Mana(mtg_data::Mana::Phyrexian(self.mana))).id()
343    }
344
345    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
346        arrayvec::ArrayVec::new_const()
347    }
348
349    fn data(&self) -> arrayvec::ArrayVec<u8, MAX_NODE_DATA_SIZE> {
350        unimplemented!()
351    }
352
353    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
354        use std::io::Write;
355        write!(out, "{}", self.mana)
356    }
357
358    fn node_tag(&self) -> &'static str {
359        "phyrexian mana"
360    }
361
362    #[cfg(feature = "spanned_tree")]
363    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
364        self.span
365    }
366}
367
368/// A mana symbol with a number on it, representing a fixed amount of any kind of mana.
369#[derive(serde::Serialize, serde::Deserialize)]
370#[derive(Debug, Clone, PartialEq, Eq)]
371pub struct HybridPhyrexianMana {
372    pub mana: mtg_data::HybridPhyrexianMana,
373    #[cfg(feature = "spanned_tree")]
374    pub span: crate::ability_tree::span::TreeSpan,
375}
376
377impl AbilityTreeNode for HybridPhyrexianMana {
378    fn node_id(&self) -> usize {
379        use crate::ability_tree::tree_node::MtgDataNodeKind;
380        use idris::Idris;
381
382        crate::ability_tree::NodeKind::MtgData(MtgDataNodeKind::Mana(mtg_data::Mana::HybridPhyrexian(self.mana))).id()
383    }
384
385    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
386        arrayvec::ArrayVec::new_const()
387    }
388
389    fn data(&self) -> arrayvec::ArrayVec<u8, MAX_NODE_DATA_SIZE> {
390        unimplemented!()
391    }
392
393    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
394        use std::io::Write;
395        write!(out, "{}", self.mana)
396    }
397
398    fn node_tag(&self) -> &'static str {
399        "hybrid phyrexian mana"
400    }
401
402    #[cfg(feature = "spanned_tree")]
403    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
404        self.span
405    }
406}