boseiju/ability_tree/zone/
zone.rs

1use crate::ability_tree::AbilityTreeNode;
2use crate::ability_tree::MAX_CHILDREN_PER_NODE;
3use crate::lexer::IntoToken;
4
5/// Fixme: doc
6#[derive(idris_derive::Idris)]
7#[derive(serde::Serialize, serde::Deserialize)]
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
9pub enum OwnableZone {
10    Graveyard {
11        #[cfg(feature = "spanned_tree")]
12        span: crate::ability_tree::span::TreeSpan,
13    },
14    Hand {
15        #[cfg(feature = "spanned_tree")]
16        span: crate::ability_tree::span::TreeSpan,
17    },
18    Library {
19        #[cfg(feature = "spanned_tree")]
20        span: crate::ability_tree::span::TreeSpan,
21    },
22}
23
24#[cfg(feature = "spanned_tree")]
25impl OwnableZone {
26    pub fn span(&self) -> crate::ability_tree::span::TreeSpan {
27        match self {
28            Self::Graveyard { span } => *span,
29            Self::Hand { span } => *span,
30            Self::Library { span } => *span,
31        }
32    }
33}
34
35impl AbilityTreeNode for OwnableZone {
36    fn node_id(&self) -> usize {
37        use crate::ability_tree::tree_node::TerminalNodeKind;
38        use idris::Idris;
39
40        crate::ability_tree::NodeKind::Terminal(TerminalNodeKind::OwnableZoneIdMarker).id()
41    }
42
43    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
44        use crate::ability_tree::NodeKind;
45        use crate::ability_tree::tree_node::TerminalNodeKind;
46        use idris::Idris;
47
48        let mut children = arrayvec::ArrayVec::new_const();
49        let child_id = NodeKind::Terminal(TerminalNodeKind::OwnableZone(*self)).id();
50        let child = crate::ability_tree::dummy_terminal::TreeNodeDummyTerminal::new(child_id);
51        children.push(child as &dyn AbilityTreeNode);
52        children
53    }
54
55    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
56        use std::io::Write;
57        write!(out, "{self}")
58    }
59
60    fn node_tag(&self) -> &'static str {
61        "zone"
62    }
63
64    #[cfg(feature = "spanned_tree")]
65    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
66        match self {
67            Self::Graveyard { span } => *span,
68            Self::Hand { span } => *span,
69            Self::Library { span } => *span,
70        }
71    }
72}
73
74impl std::fmt::Display for OwnableZone {
75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        match self {
77            OwnableZone::Graveyard { .. } => write!(f, "graveyard"),
78            OwnableZone::Hand { .. } => write!(f, "hand"),
79            OwnableZone::Library { .. } => write!(f, "library"),
80        }
81    }
82}
83
84impl IntoToken for OwnableZone {
85    #[cfg(feature = "lexer")]
86    fn try_from_span(span: &crate::lexer::Span) -> Option<Self> {
87        match span.text {
88            "graveyard" => Some(OwnableZone::Graveyard {
89                #[cfg(feature = "spanned_tree")]
90                span: span.into(),
91            }),
92            "hand" => Some(OwnableZone::Hand {
93                #[cfg(feature = "spanned_tree")]
94                span: span.into(),
95            }),
96            "library" => Some(OwnableZone::Library {
97                #[cfg(feature = "spanned_tree")]
98                span: span.into(),
99            }),
100            _ => None,
101        }
102    }
103}
104
105#[cfg(feature = "parser")]
106impl crate::utils::DummyInit for OwnableZone {
107    fn dummy_init() -> Self {
108        Self::Library {
109            #[cfg(feature = "spanned_tree")]
110            span: Default::default(),
111        }
112    }
113}