boseiju/ability_tree/
time.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 Instant {
10    AnyTime {
11        #[cfg(feature = "spanned_tree")]
12        span: crate::ability_tree::span::TreeSpan,
13    },
14    TheBeginningOfTheNextEndStep {
15        #[cfg(feature = "spanned_tree")]
16        span: crate::ability_tree::span::TreeSpan,
17    },
18}
19
20impl AbilityTreeNode for Instant {
21    fn node_id(&self) -> usize {
22        use crate::ability_tree::tree_node::TerminalNodeKind;
23        use idris::Idris;
24
25        crate::ability_tree::NodeKind::Terminal(TerminalNodeKind::InstantIdMarker).id()
26    }
27
28    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
29        use crate::ability_tree::NodeKind;
30        use crate::ability_tree::tree_node::TerminalNodeKind;
31        use idris::Idris;
32
33        let mut children = arrayvec::ArrayVec::new_const();
34        let child_id = NodeKind::Terminal(TerminalNodeKind::Instant(*self)).id();
35        let child = crate::ability_tree::dummy_terminal::TreeNodeDummyTerminal::new(child_id);
36        children.push(child as &dyn AbilityTreeNode);
37        children
38    }
39
40    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
41        use std::io::Write;
42        write!(out, "{self}")
43    }
44
45    fn node_tag(&self) -> &'static str {
46        "instant"
47    }
48
49    #[cfg(feature = "spanned_tree")]
50    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
51        match self {
52            Self::AnyTime { span } => *span,
53            Self::TheBeginningOfTheNextEndStep { span } => *span,
54        }
55    }
56}
57
58impl IntoToken for Instant {
59    #[cfg(feature = "lexer")]
60    fn try_from_span(span: &crate::lexer::Span) -> Option<Self> {
61        match span.text {
62            "any time" => Some(Self::AnyTime {
63                #[cfg(feature = "spanned_tree")]
64                span: Default::default(),
65            }),
66            "the beginning of the next end step" => Some(Self::TheBeginningOfTheNextEndStep {
67                #[cfg(feature = "spanned_tree")]
68                span: Default::default(),
69            }),
70            _ => None,
71        }
72    }
73}
74
75#[cfg(feature = "parser")]
76impl crate::utils::DummyInit for Instant {
77    fn dummy_init() -> Self {
78        Self::TheBeginningOfTheNextEndStep {
79            #[cfg(feature = "spanned_tree")]
80            span: Default::default(),
81        }
82    }
83}
84
85impl std::fmt::Display for Instant {
86    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        match self {
88            Self::AnyTime { .. } => write!(f, "at any time"),
89            Self::TheBeginningOfTheNextEndStep { .. } => write!(f, "at the beginning of the next endstep"),
90        }
91    }
92}
93
94/// Fixme: doc
95#[derive(idris_derive::Idris)]
96#[derive(serde::Serialize, serde::Deserialize)]
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
98pub enum ForwardDuration {
99    ForAsLongAsItsExiled {
100        #[cfg(feature = "spanned_tree")]
101        span: crate::ability_tree::span::TreeSpan,
102    },
103    UntilEndOfTurn {
104        #[cfg(feature = "spanned_tree")]
105        span: crate::ability_tree::span::TreeSpan,
106    },
107    UntilEndOfYourNextTurn {
108        #[cfg(feature = "spanned_tree")]
109        span: crate::ability_tree::span::TreeSpan,
110    },
111}
112
113impl AbilityTreeNode for ForwardDuration {
114    fn node_id(&self) -> usize {
115        use crate::ability_tree::tree_node::TerminalNodeKind;
116        use idris::Idris;
117
118        crate::ability_tree::NodeKind::Terminal(TerminalNodeKind::ForwardDurationIdMarker).id()
119    }
120
121    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
122        use crate::ability_tree::NodeKind;
123        use crate::ability_tree::tree_node::TerminalNodeKind;
124        use idris::Idris;
125
126        let mut children = arrayvec::ArrayVec::new_const();
127        let child_id = NodeKind::Terminal(TerminalNodeKind::ForwardDuration(*self)).id();
128        let child = crate::ability_tree::dummy_terminal::TreeNodeDummyTerminal::new(child_id);
129        children.push(child as &dyn AbilityTreeNode);
130        children
131    }
132
133    fn node_tag(&self) -> &'static str {
134        "forward duration"
135    }
136
137    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
138        use std::io::Write;
139        write!(out, "{self}")
140    }
141
142    #[cfg(feature = "spanned_tree")]
143    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
144        match self {
145            Self::ForAsLongAsItsExiled { span } => *span,
146            Self::UntilEndOfTurn { span } => *span,
147            Self::UntilEndOfYourNextTurn { span } => *span,
148        }
149    }
150}
151
152impl IntoToken for ForwardDuration {
153    #[cfg(feature = "lexer")]
154    fn try_from_span(span: &crate::lexer::Span) -> Option<Self> {
155        match span.text {
156            "until end of turn" => Some(Self::UntilEndOfTurn {
157                #[cfg(feature = "spanned_tree")]
158                span: Default::default(),
159            }),
160            "until the end of your next turn" => Some(Self::UntilEndOfYourNextTurn {
161                #[cfg(feature = "spanned_tree")]
162                span: Default::default(),
163            }),
164            _ => None,
165        }
166    }
167}
168
169#[cfg(feature = "parser")]
170impl crate::utils::DummyInit for ForwardDuration {
171    fn dummy_init() -> Self {
172        Self::UntilEndOfTurn {
173            #[cfg(feature = "spanned_tree")]
174            span: Default::default(),
175        }
176    }
177}
178
179impl std::fmt::Display for ForwardDuration {
180    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
181        match self {
182            Self::ForAsLongAsItsExiled { .. } => write!(f, "for as long as it remains exiled"),
183            Self::UntilEndOfTurn { .. } => write!(f, "until end of turn"),
184            Self::UntilEndOfYourNextTurn { .. } => write!(f, "until the end of your next turn"),
185        }
186    }
187}
188
189/// Fixme: doc
190#[derive(idris_derive::Idris)]
191#[derive(serde::Serialize, serde::Deserialize)]
192#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
193pub enum BackwardDuration {
194    ThisTurn {
195        #[cfg(feature = "spanned_tree")]
196        span: crate::ability_tree::span::TreeSpan,
197    },
198}
199
200impl AbilityTreeNode for BackwardDuration {
201    fn node_id(&self) -> usize {
202        use crate::ability_tree::tree_node::TerminalNodeKind;
203        use idris::Idris;
204
205        crate::ability_tree::NodeKind::Terminal(TerminalNodeKind::BackwardDurationIdMarker).id()
206    }
207
208    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
209        use crate::ability_tree::NodeKind;
210        use crate::ability_tree::tree_node::TerminalNodeKind;
211        use idris::Idris;
212
213        let mut children = arrayvec::ArrayVec::new_const();
214        let child_id = NodeKind::Terminal(TerminalNodeKind::BackwardDuration(*self)).id();
215        let child = crate::ability_tree::dummy_terminal::TreeNodeDummyTerminal::new(child_id);
216        children.push(child as &dyn AbilityTreeNode);
217        children
218    }
219
220    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
221        use std::io::Write;
222        write!(out, "{self}")
223    }
224
225    fn node_tag(&self) -> &'static str {
226        "backward duration"
227    }
228
229    #[cfg(feature = "spanned_tree")]
230    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
231        match self {
232            Self::ThisTurn { span } => *span,
233        }
234    }
235}
236
237impl IntoToken for BackwardDuration {
238    #[cfg(feature = "lexer")]
239    fn try_from_span(span: &crate::lexer::Span) -> Option<Self> {
240        match span.text {
241            "this turn" => Some(Self::ThisTurn {
242                #[cfg(feature = "spanned_tree")]
243                span: Default::default(),
244            }),
245            _ => None,
246        }
247    }
248}
249
250#[cfg(feature = "parser")]
251impl crate::utils::DummyInit for BackwardDuration {
252    fn dummy_init() -> Self {
253        Self::ThisTurn {
254            #[cfg(feature = "spanned_tree")]
255            span: Default::default(),
256        }
257    }
258}
259
260impl std::fmt::Display for BackwardDuration {
261    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
262        match self {
263            Self::ThisTurn { .. } => write!(f, "this turn"),
264        }
265    }
266}