boseiju/ability_tree/
cost.rs

1use crate::ability_tree::AbilityTreeNode;
2use crate::ability_tree::MAX_CHILDREN_PER_NODE;
3
4/// A cost is something that need to be paid.
5///
6/// It may be a mana cost (paying mana), or any imperative that requires
7/// the player to do something (discard a card, sacrifice a creature...)
8#[derive(serde::Serialize, serde::Deserialize)]
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum Cost {
11    ManaCost(crate::ability_tree::terminals::ManaCost),
12    Imperative(crate::ability_tree::imperative::Imperative),
13}
14
15#[cfg(feature = "spanned_tree")]
16impl Cost {
17    pub fn span(&self) -> crate::ability_tree::span::TreeSpan {
18        match self {
19            Self::ManaCost(child) => child.span,
20            Self::Imperative(child) => child.node_span(),
21        }
22    }
23}
24
25impl crate::ability_tree::AbilityTreeNode for Cost {
26    fn node_id(&self) -> usize {
27        use idris::Idris;
28        crate::ability_tree::NodeKind::Cost.id()
29    }
30
31    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
32        let mut children = arrayvec::ArrayVec::new_const();
33        match self {
34            Self::ManaCost(child) => children.push(child as &dyn AbilityTreeNode),
35            Self::Imperative(child) => children.push(child as &dyn AbilityTreeNode),
36        }
37        children
38    }
39
40    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
41        match self {
42            Cost::ManaCost(mana_cost) => mana_cost.display(out)?,
43            Cost::Imperative(cost) => cost.display(out)?,
44        }
45        Ok(())
46    }
47
48    fn node_tag(&self) -> &'static str {
49        "cost"
50    }
51
52    #[cfg(feature = "spanned_tree")]
53    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
54        match self {
55            Self::ManaCost(child) => child.node_span(),
56            Self::Imperative(child) => child.node_span(),
57        }
58    }
59}
60
61#[cfg(feature = "parser")]
62impl crate::utils::DummyInit for Cost {
63    fn dummy_init() -> Self {
64        Self::ManaCost(crate::utils::dummy())
65    }
66}