boseiju/ability_tree/object/
object_count.rs

1use crate::ability_tree::AbilityTreeNode;
2use crate::ability_tree::MAX_CHILDREN_PER_NODE;
3
4/// Fixme: doc
5#[derive(idris_derive::Idris)]
6#[derive(serde::Serialize, serde::Deserialize)]
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum CountSpecifier {
9    A {
10        #[cfg(feature = "spanned_tree")]
11        span: crate::ability_tree::span::TreeSpan,
12    },
13    All {
14        #[cfg(feature = "spanned_tree")]
15        span: crate::ability_tree::span::TreeSpan,
16    },
17    Target(crate::ability_tree::number::Number),
18    AllOthers {
19        #[cfg(feature = "spanned_tree")]
20        span: crate::ability_tree::span::TreeSpan,
21    },
22}
23
24#[cfg(feature = "spanned_tree")]
25impl CountSpecifier {
26    pub fn span(&self) -> crate::ability_tree::span::TreeSpan {
27        match self {
28            Self::A { span } => *span,
29            Self::All { span } => *span,
30            Self::Target(child) => child.node_span(),
31            Self::AllOthers { span } => *span,
32        }
33    }
34}
35
36impl AbilityTreeNode for CountSpecifier {
37    fn node_id(&self) -> usize {
38        use idris::Idris;
39        crate::ability_tree::NodeKind::CountSpecifierIdMarker.id()
40    }
41
42    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
43        use idris::Idris;
44
45        let mut children = arrayvec::ArrayVec::new_const();
46        match self {
47            Self::Target(child) => children.push(child as &dyn AbilityTreeNode),
48            _ => children.push(crate::ability_tree::dummy_terminal::TreeNodeDummyTerminal::new(
49                crate::ability_tree::NodeKind::CountSpecifier(self.clone()).id(),
50            ) as &dyn AbilityTreeNode),
51        }
52        children
53    }
54
55    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
56        use std::io::Write;
57        write!(out, "count specifier:")?;
58        out.push_final_branch()?;
59        match self {
60            Self::A { .. } => write!(out, "a")?,
61            Self::All { .. } => write!(out, "all")?,
62            Self::Target(num) => {
63                write!(out, "target")?;
64                out.push_final_branch()?;
65                num.display(out)?;
66                out.pop_branch();
67            }
68            Self::AllOthers { .. } => write!(out, "all others")?,
69        }
70        out.pop_branch();
71        Ok(())
72    }
73
74    fn node_tag(&self) -> &'static str {
75        "object count"
76    }
77
78    #[cfg(feature = "spanned_tree")]
79    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
80        match self {
81            Self::A { span } => *span,
82            Self::All { span } => *span,
83            Self::Target(child) => child.node_span(),
84            Self::AllOthers { span } => *span,
85        }
86    }
87}
88
89#[cfg(feature = "parser")]
90impl crate::utils::DummyInit for CountSpecifier {
91    fn dummy_init() -> Self {
92        Self::All {
93            #[cfg(feature = "spanned_tree")]
94            span: Default::default(),
95        }
96    }
97}