boseiju/ability_tree/terminals/mtg_data_as_token/
keywords.rs

1use crate::ability_tree::AbilityTreeNode;
2use crate::ability_tree::MAX_CHILDREN_PER_NODE;
3use crate::lexer::IntoToken;
4
5use crate::ability_tree::NodeKind;
6use crate::ability_tree::tree_node::MtgDataNodeKind;
7use idris::Idris;
8
9/// Wrapper around the keyword actions.
10#[derive(serde::Serialize, serde::Deserialize)]
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
12pub struct KeywordAction {
13    pub keyword_action: mtg_data::KeywordAction,
14    #[cfg(feature = "spanned_tree")]
15    pub span: crate::ability_tree::span::TreeSpan,
16}
17
18impl IntoToken for KeywordAction {
19    #[cfg(feature = "lexer")]
20    fn try_from_span(span: &crate::lexer::Span) -> Option<Self> {
21        Some(Self {
22            keyword_action: crate::utils::from_str_singular_or_plural(&span.text)?,
23            #[cfg(feature = "spanned_tree")]
24            span: span.into(),
25        })
26    }
27}
28
29impl idris::Idris for KeywordAction {
30    const COUNT: usize = mtg_data::KeywordAction::COUNT;
31    fn id(&self) -> usize {
32        self.keyword_action.id()
33    }
34    fn name_from_id(id: usize) -> &'static str {
35        mtg_data::KeywordAction::name_from_id(id)
36    }
37}
38
39/// Wrapper around the ability word.
40#[derive(serde::Serialize, serde::Deserialize)]
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
42pub struct AbilityWord {
43    pub ability_word: mtg_data::AbilityWord,
44    #[cfg(feature = "spanned_tree")]
45    pub span: crate::ability_tree::span::TreeSpan,
46}
47
48impl AbilityTreeNode for AbilityWord {
49    fn node_id(&self) -> usize {
50        crate::ability_tree::NodeKind::MtgData(MtgDataNodeKind::AbilityWordIdMarker).id()
51    }
52
53    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
54        let mut children = arrayvec::ArrayVec::new_const();
55        let child_id = NodeKind::MtgData(MtgDataNodeKind::AbilityWord(self.ability_word)).id();
56        let child = crate::ability_tree::dummy_terminal::TreeNodeDummyTerminal::new(child_id);
57        children.push(child as &dyn AbilityTreeNode);
58        children
59    }
60
61    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
62        use std::io::Write;
63        write!(out, "{}", self.ability_word)
64    }
65
66    fn node_tag(&self) -> &'static str {
67        "ability word"
68    }
69
70    #[cfg(feature = "spanned_tree")]
71    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
72        self.span
73    }
74}
75
76impl IntoToken for AbilityWord {
77    #[cfg(feature = "lexer")]
78    fn try_from_span(span: &crate::lexer::Span) -> Option<Self> {
79        use std::str::FromStr;
80        Some(Self {
81            ability_word: mtg_data::AbilityWord::from_str(&span.text).ok()?,
82            #[cfg(feature = "spanned_tree")]
83            span: span.into(),
84        })
85    }
86}
87
88impl idris::Idris for AbilityWord {
89    const COUNT: usize = mtg_data::AbilityWord::COUNT;
90    fn id(&self) -> usize {
91        self.ability_word.id()
92    }
93    fn name_from_id(id: usize) -> &'static str {
94        mtg_data::AbilityWord::name_from_id(id)
95    }
96}
97
98#[cfg(feature = "parser")]
99impl crate::utils::DummyInit for AbilityWord {
100    fn dummy_init() -> Self {
101        Self {
102            ability_word: mtg_data::AbilityWord::Adamant,
103            #[cfg(feature = "spanned_tree")]
104            span: Default::default(),
105        }
106    }
107}