boseiju/ability_tree/terminals/
power_toughness.rs

1use crate::lexer::IntoToken;
2
3#[derive(serde::Serialize, serde::Deserialize)]
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
5pub struct PowerToughness {
6    pub power: u32,
7    pub toughness: u32,
8    #[cfg(feature = "spanned_tree")]
9    pub span: crate::ability_tree::span::TreeSpan,
10}
11
12impl PowerToughness {
13    pub const COUNT: usize = 1;
14    pub const fn id(&self) -> usize {
15        0
16    }
17}
18
19impl std::fmt::Display for PowerToughness {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        write!(f, "{}/{}", self.power, self.toughness)
22    }
23}
24
25#[cfg(feature = "lexer")]
26impl IntoToken for PowerToughness {
27    fn try_from_span(span: &crate::lexer::Span) -> Option<Self> {
28        let split: Vec<_> = span.text.split('/').collect();
29        let (raw_pow, raw_tough) = match split.as_slice() {
30            [pow, tough] => (pow, tough),
31            _ => return None,
32        };
33        if !crate::utils::is_digits(raw_pow) {
34            return None;
35        }
36        if !crate::utils::is_digits(raw_tough) {
37            return None;
38        }
39        Some(PowerToughness {
40            power: raw_pow.parse().ok()?,
41            toughness: raw_tough.parse().ok()?,
42            #[cfg(feature = "spanned_tree")]
43            span: span.into(),
44        })
45    }
46}