axfive_matrix_dicebot/
dice.rs1pub mod parser;
2
3use std::fmt;
4use std::ops::{Deref, DerefMut};
5
6#[derive(Debug, PartialEq, Eq, Clone, Copy)]
7pub struct Dice {
8 pub(crate) count: u32,
9 pub(crate) sides: u32,
10}
11
12impl fmt::Display for Dice {
13 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14 write!(f, "{}d{}", self.count, self.sides)
15 }
16}
17
18impl Dice {
19 fn new(count: u32, sides: u32) -> Dice {
20 Dice { count, sides }
21 }
22}
23
24#[derive(Debug, PartialEq, Eq, Clone, Copy)]
25pub enum Element {
26 Dice(Dice),
27 Bonus(u32),
28}
29
30impl fmt::Display for Element {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 Element::Dice(d) => write!(f, "{}", d),
34 Element::Bonus(b) => write!(f, "{}", b),
35 }
36 }
37}
38
39#[derive(Debug, PartialEq, Eq, Clone, Copy)]
40pub enum SignedElement {
41 Positive(Element),
42 Negative(Element),
43}
44
45impl fmt::Display for SignedElement {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 match self {
48 SignedElement::Positive(e) => write!(f, "{}", e),
49 SignedElement::Negative(e) => write!(f, "-{}", e),
50 }
51 }
52}
53
54#[derive(Debug, PartialEq, Eq, Clone)]
55pub struct ElementExpression(Vec<SignedElement>);
56
57impl Deref for ElementExpression {
58 type Target = Vec<SignedElement>;
59
60 fn deref(&self) -> &Self::Target {
61 &self.0
62 }
63}
64
65impl DerefMut for ElementExpression {
66 fn deref_mut(&mut self) -> &mut Self::Target {
67 &mut self.0
68 }
69}
70
71impl fmt::Display for ElementExpression {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 let mut iter = self.0.iter();
74 if let Some(first) = iter.next() {
75 write!(f, "{}", first)?;
76 for roll in iter {
77 match roll {
78 SignedElement::Positive(e) => write!(f, " + {}", e)?,
79 SignedElement::Negative(e) => write!(f, " - {}", e)?,
80 }
81 }
82 }
83 Ok(())
84 }
85}