dice_command_parser/
dice_roll.rs

1#[derive(Clone, Debug, PartialEq)]
2/// This struct represents the information required to calculate the result of a dice roll given the command string.
3/// Validating the sanity of each of the parameters is left to the user. e.g. The number of dice to roll could be 0.
4pub struct DiceRoll {
5    /// How many dice should be rolled.
6    pub number_of_dice_to_roll: u32,
7    /// How many faces each dice has.
8    pub dice_sides: u32,
9    /// The optional fixed modifier that should be applied to each dice roll. Can be positive or negative.
10    pub modifier: Option<i32>,
11    /// Whether the roll has advantage, disadvantage, or is a regular roll
12    pub roll_type: RollType,
13}
14
15impl DiceRoll {
16    /// A convinience method for creating a `DiceRoll`.
17    ///
18    /// # Examples
19    ///
20    /// This represents a d6 with no modifier
21    /// ```
22    /// use dice_command_parser::dice_roll::{DiceRoll, RollType};
23    ///
24    /// let dice_roll = DiceRoll::new(6, None, 1, RollType::Regular);
25    /// ```
26    ///
27    /// This represents two d20 with a +1 modifier rolling with advantage
28    /// ```
29    /// use dice_command_parser::dice_roll::{DiceRoll, RollType};
30    ///
31    /// let dice_roll = DiceRoll::new(20, Some(1), 2, RollType::KeepHighest);
32    /// ```
33    #[must_use]
34    pub fn new(
35        dice_sides: u32,
36        modifier: Option<i32>,
37        number_of_dice_to_roll: u32,
38        roll_type: RollType,
39    ) -> Self {
40        DiceRoll {
41            dice_sides,
42            modifier,
43            number_of_dice_to_roll,
44            roll_type,
45        }
46    }
47
48    /// A convinience method for creating a `DiceRoll`, without advantage or disadvantage.
49    ///
50    /// # Examples
51    ///
52    /// This represents a d6 with no modifier
53    /// ```
54    /// use dice_command_parser::dice_roll::{DiceRoll, Operation};
55    ///
56    /// let dice_roll = DiceRoll::new_regular_roll(6, None, 1);
57    /// ```
58    ///
59    /// This represents two d20 with a +1 modifier.
60    /// ```
61    /// use dice_command_parser::dice_roll::{DiceRoll, Operation};
62    ///
63    /// let dice_roll = DiceRoll::new_regular_roll(20, Some(1), 2);
64    /// ```
65    #[must_use]
66    pub fn new_regular_roll(
67        dice_sides: u32,
68        modifier: Option<i32>,
69        number_of_dice_to_roll: u32,
70    ) -> Self {
71        DiceRoll {
72            dice_sides,
73            modifier,
74            number_of_dice_to_roll,
75            roll_type: RollType::Regular,
76        }
77    }
78}
79
80/// Represents whether a roll has advantage, disadvantage, independence, or not.
81#[derive(Clone, Debug, PartialEq)]
82pub enum RollType {
83    /// The roll will keep the highest of multiple dice rolled. This is used for Advantage in D&D or Bane in Dragonbane.
84    KeepHighest,
85    /// The roll will keep the lowest of multiple dice rolled. This is used for Disadvantage in D&D or Boon in Dragonbane.
86    KeepLowest,
87    /// A regular roll occurs - only one roll needs to occur.
88    Regular,
89}
90
91impl Default for RollType {
92    fn default() -> Self {
93        Self::Regular
94    }
95}
96
97/// Represents whether a roll should be added or taken away from the total
98#[derive(Clone, Debug, PartialEq)]
99pub enum Operation {
100    /// The roll should be added towards the overall total
101    Addition,
102    /// The roll should be taken away from the overall total
103    Subtraction,
104}