boseiju/ability_tree/terminals/
owner_specifier.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 OwnerSpecifier {
10    YouOwn {
11        #[cfg(feature = "spanned_tree")]
12        span: crate::ability_tree::span::TreeSpan,
13    },
14    YouDontOwn {
15        #[cfg(feature = "spanned_tree")]
16        span: crate::ability_tree::span::TreeSpan,
17    },
18    ObjectOwner {
19        #[cfg(feature = "spanned_tree")]
20        span: crate::ability_tree::span::TreeSpan,
21    },
22}
23
24#[cfg(feature = "spanned_tree")]
25impl OwnerSpecifier {
26    pub fn span(&self) -> crate::ability_tree::span::TreeSpan {
27        match self {
28            Self::YouOwn { span } => *span,
29            Self::YouDontOwn { span } => *span,
30            Self::ObjectOwner { span } => *span,
31        }
32    }
33}
34
35impl AbilityTreeNode for OwnerSpecifier {
36    fn node_id(&self) -> usize {
37        use crate::ability_tree::tree_node::TerminalNodeKind;
38        use idris::Idris;
39        crate::ability_tree::NodeKind::Terminal(TerminalNodeKind::OwnerSpecifierIdMarker).id()
40    }
41
42    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
43        use crate::ability_tree::NodeKind;
44        use crate::ability_tree::tree_node::TerminalNodeKind;
45        use idris::Idris;
46
47        let mut children = arrayvec::ArrayVec::new_const();
48        let child_id = NodeKind::Terminal(TerminalNodeKind::OwnerSpecifier(*self)).id();
49        let child = crate::ability_tree::dummy_terminal::TreeNodeDummyTerminal::new(child_id);
50        children.push(child as &dyn AbilityTreeNode);
51        children
52    }
53
54    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
55        use std::io::Write;
56        write!(out, "{self}")
57    }
58
59    fn node_tag(&self) -> &'static str {
60        "owner specifier"
61    }
62
63    #[cfg(feature = "spanned_tree")]
64    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
65        match self {
66            Self::YouOwn { span } => *span,
67            Self::YouDontOwn { span } => *span,
68            Self::ObjectOwner { span } => *span,
69        }
70    }
71}
72
73impl std::fmt::Display for OwnerSpecifier {
74    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75        match self {
76            OwnerSpecifier::YouOwn { .. } => write!(f, "you own"),
77            OwnerSpecifier::YouDontOwn { .. } => write!(f, "you don't own"),
78            OwnerSpecifier::ObjectOwner { .. } => write!(f, "its owner"),
79        }
80    }
81}
82
83#[cfg(feature = "lexer")]
84impl IntoToken for OwnerSpecifier {
85    fn try_from_span(span: &crate::lexer::Span) -> Option<Self> {
86        match span.text {
87            "you own" | "your" => Some(OwnerSpecifier::YouOwn {
88                #[cfg(feature = "spanned_tree")]
89                span: span.into(),
90            }),
91            "you don't own" => Some(OwnerSpecifier::YouDontOwn {
92                #[cfg(feature = "spanned_tree")]
93                span: span.into(),
94            }),
95            "its owner" => Some(OwnerSpecifier::ObjectOwner {
96                #[cfg(feature = "spanned_tree")]
97                span: span.into(),
98            }),
99            _ => None,
100        }
101    }
102}
103
104#[cfg(feature = "parser")]
105impl crate::utils::DummyInit for OwnerSpecifier {
106    fn dummy_init() -> Self {
107        Self::YouOwn {
108            #[cfg(feature = "spanned_tree")]
109            span: Default::default(),
110        }
111    }
112}