1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//! # 🎲 die_parser
//!
//! This crate parses the notation for die rolls as used in tabletop games like D&D.
//!
//! It aims to do so in the most *simple*, *easy* and *lightweight* way possible.
//! ```rust,ignore
//!     Input:
//!     1.) "2d6"         (Roll 2 six-sided dice.)
//!     2.) "4d20 - 5"    (Roll 4 twenty-sided dice and subtract 5 from the result.)
//!
//!     Output:
//!     1.)    Roll {
//!             number_of_sides: 6
//!             number_of_dice: 2
//!             modifier: 0
//!            }
//!     2.)    Roll {
//!             number_of_sides: 20
//!             number_of_dice: 4
//!             modifier: -5
//!            }
//! ```
//! ## ❓ Getting started:
//! **Try [Roll::parse_roll()]!**

use nom::bytes::complete::tag;
use nom::character::complete::{char, digit1};
use nom::combinator::{map, map_res};
use nom::sequence::separated_pair;
use nom::{branch, IResult};
use std::fmt;

use std::str::FromStr;

/// Holds information about a die roll.
#[derive(Debug, PartialEq)]
pub struct Roll {
    /// The type of die.
    pub number_of_sides: u16,
    /// How many dice are to be rolled.
    pub number_of_dice: u16,
    /// A modifier to be added to the result of the die rolls.
    pub modifier: i32,
}
impl Roll {
    /// A convenience function that allows you to manually create a new [Roll].
    pub fn new(number_of_sides: u16, number_of_dice: u16, modifier: i32) -> Self {
        Self {
            number_of_sides,
            number_of_dice,
            modifier,
        }
    }

    /// Parses a given input string with no regard to validity.
    fn parse_modified_roll(input: &str) -> Result<Roll, RollError> {
        // Remove whitespaces.
        let whitespaceless = input.replace(' ', "");

        // Parse type of die and amount of dice.
        let (remainder, (number_of_dice, number_of_sides)) =
            match parse_simple_roll(&whitespaceless) {
                Ok(v) => v,
                Err(_) => return Err(RollError::ParsingError),
            };

        // Parse the modifier
        let (_, modifier) = match parse_modifier(remainder) {
            Ok(v) => v,
            Err(_) => return Err(RollError::ParsingError),
        };

        // Success!
        Ok(Roll {
            number_of_dice,
            number_of_sides,
            modifier,
        })
    }

    /// Checks if a give roll is using a valid type of die and amount of dice.
    fn check_roll_validity(&self, max_dice: u16) -> Result<(), RollError> {
        // Check for die type.
        match self.number_of_sides {
            2 => (),
            4 => (),
            6 => (),
            8 => (),
            10 => (),
            12 => (),
            20 => (),
            100 => (),
            _ => return Err(RollError::DieTypeInvalid),
        }

        // Check for amount of dice. If max_dice == 0 ~> no limit.
        if self.number_of_dice > max_dice && !max_dice != 0 {
            return Err(RollError::DiceExceedLimit);
        } else if self.number_of_dice == 0 {
            return Err(RollError::NoDiceToRoll);
        }

        // Checks passed.
        Ok(())
    }

    /// **Tries to parse input as roll notation (e.g. `4d20 + 5`).**
    ///
    /// * Whitespaces are ignored.
    /// * Checks for validity of roll.[^1]
    ///     * Enforces a limit of 100 dice per roll.[^2]
    ///
    /// # Examples
    /// ```
    /// use die_parser::Roll;
    /// use die_parser::RollError;
    ///
    /// let roll = Roll::parse_roll("3d10 - 5");
    /// assert_eq!(roll, Ok(Roll::new(10, 3, -5)));
    ///
    /// let invalid_roll = Roll::parse_roll("101d20");
    /// assert_eq!(invalid_roll, Err(RollError::DiceExceedLimit));
    /// ```
    /// [^1]: Valid die types are: d2, d4, d6, d8, d10, d12, d20, d100
    ///
    /// [^2]: If you wish to allow more (or only allow less) than 100 dice per roll, use [`Roll::parse_roll_with_limit()`] instead.
    pub fn parse_roll(input: &str) -> Result<Roll, RollError> {
        let result = Roll::parse_modified_roll(input)?;

        match result.check_roll_validity(100) {
            Ok(()) => Ok(result),
            Err(e) => Err(e),
        }
    }

    /// **Tries to parse input as roll notation (e.g. `4d20 + 5`).**
    ///
    /// * Whitespaces are ignored.
    /// * Checks for validity of roll.[^1]
    ///     * Enforces a custom limit of how many dice are allowed per roll `(0 = no limit)`.
    ///
    /// # Examples
    /// ```
    /// use die_parser::Roll;
    /// use die_parser::RollError;
    ///
    /// let roll = Roll::parse_roll_with_limit("3d10 - 5", 1000);
    /// assert_eq!(roll, Ok(Roll::new(10, 3, -5)));
    ///
    /// let invalid_roll = Roll::parse_roll_with_limit("15d20", 10);
    /// assert_eq!(invalid_roll, Err(RollError::DiceExceedLimit));
    /// ```
    ///
    /// [^1]: Valid die types are: d2, d4, d6, d8, d10, d12, d20, d100
    pub fn parse_roll_with_limit(input: &str, max_dice: u16) -> Result<Roll, RollError> {
        let result = Roll::parse_modified_roll(input)?;

        // Check if the roll is valid using the users max_dice value.
        match result.check_roll_validity(max_dice) {
            Ok(()) => Ok(result),
            Err(e) => Err(e),
        }
    }
}

/// The different types of errors that may occur trying to construct a [Roll] from a given input string.
#[derive(Debug, PartialEq)]
pub enum RollError {
    /// Signifies that the inputted die type did not match any of the valid types.
    ///
    /// Valid die types are: `d2`, `d4`, `d6`, `d8`, `d10`, `d12`, `d20`, `d100`
    /// # Example
    /// ```
    /// use die_parser::{Roll, RollError};
    ///
    /// let invalid_roll = Roll::parse_roll("1d50");
    /// assert_eq!(invalid_roll, Err(RollError::DieTypeInvalid));
    /// ```
    DieTypeInvalid,
    /// Signifies that the requested amount of dice exceeded the set limit.
    /// # Example
    /// ```
    /// use die_parser::{Roll, RollError};
    ///
    /// let invalid_roll = Roll::parse_roll("9001d20");
    /// assert_eq!(invalid_roll, Err(RollError::DiceExceedLimit));
    /// ```
    DiceExceedLimit,
    /// Signifies that the requested amount of dice was less than 1.
    /// # Example
    /// ```
    /// use die_parser::{Roll, RollError};
    ///
    /// let invalid_roll = Roll::parse_roll("0d20");
    /// assert_eq!(invalid_roll, Err(RollError::NoDiceToRoll));
    /// ```
    NoDiceToRoll,
    /// Signifies that the input string was malformed.
    /// # Example
    /// ```
    /// use die_parser::{Roll, RollError};
    ///
    /// let invalid_roll = Roll::parse_roll("4invalid_charactersd20+5");
    /// assert_eq!(invalid_roll, Err(RollError::ParsingError));
    ///
    /// ```
    ParsingError,
}
impl fmt::Display for RollError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::DieTypeInvalid => write!(f, "The requested type of die is invalid."),
            Self::DiceExceedLimit => write!(f, "Amount of dice exceeds the specified limit."),
            Self::NoDiceToRoll => write!(f, "Can't roll less than 1 die."),
            Self::ParsingError => write!(f, "Failed to parse the input string."),
        }
    }
}
impl std::error::Error for RollError {}

/// Parse a `u16` from the start of the input string.
fn parse_numbers(input: &str) -> IResult<&str, u16> {
    map_res(digit1, u16::from_str)(input)
}

/// Tries to parse die type and amount of dice from a notated die roll (e.g. `4d20`).
fn parse_simple_roll(s: &str) -> IResult<&str, (u16, u16)> {
    let parser = separated_pair(parse_numbers, char('d'), parse_numbers);
    map(parser, |(number_of_dice, number_of_sides)| {
        (number_of_dice, number_of_sides)
    })(s)
}

/// Looks for modifiers operator.
fn parse_operator(s: &str) -> IResult<&str, &str> {
    branch::alt((tag("+"), tag("-"), tag("")))(s)
}

/// Tries to parse the modifier part of a notated die roll (e.g. `+5`).
fn parse_modifier(s: &str) -> IResult<&str, i32> {
    // Split operator and modifier.
    let (remainder, operator) = parse_operator(s).unwrap();

    // Generate i32.
    match operator {
        "+" => map(parse_numbers, |modifier| modifier as i32)(remainder),
        "-" => map(parse_numbers, |modifier| -(modifier as i32))(remainder),
        // Return 0 as modifier if no operator signalling a modifier was found.
        _ => Ok((remainder, 0)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_parse_simple_roll() {
        let tests = [
            ("4d20", (4, 20), ""),
            ("4d20remainder_text", (4, 20), "remainder_text"),
        ];

        for (input, expected_output, expected_remaining_input) in tests {
            let (remaining_input, output) = parse_simple_roll(input).unwrap();
            assert_eq!(remaining_input, expected_remaining_input);
            assert_eq!(output, expected_output);
        }
    }

    #[test]
    fn test_parse_modifier() {
        let tests_positive = [
            ("+5", 5, ""),
            ("+5remainder_text", 5, "remainder_text"),
            ("", 0, ""),
            ("random_unparsable", 0, "random_unparsable"),
        ];
        let tests_negative = [
            ("-5", -5, ""),
            ("-5remainder_text", -5, "remainder_text"),
            ("", 0, ""),
            ("random_unparsable", 0, "random_unparsable"),
        ];
        for (input, expected_output, expected_remaining_input) in tests_positive {
            let (remaining_input, output) = parse_modifier(input).unwrap();
            assert_eq!(remaining_input, expected_remaining_input);
            assert_eq!(output, expected_output);
        }
        for (input, expected_output, expected_remaining_input) in tests_negative {
            let (remaining_input, output) = parse_modifier(input).unwrap();
            assert_eq!(remaining_input, expected_remaining_input);
            assert_eq!(output, expected_output);
        }
    }

    #[test]
    fn test_parse_modified_roll() {
        let tests = [
            ("4d10+3", Roll::new(10, 4, 3)),
            ("4d10-3", Roll::new(10, 4, -3)),
            ("4 d 10  + 3", Roll::new(10, 4, 3)),
            ("4 d 10  - 3", Roll::new(10, 4, -3)),
            ("4d10+3 random_stuff", Roll::new(10, 4, 3)),
            ("4d10-3 random_stuff", Roll::new(10, 4, -3)),
        ];

        for (input, expected_output) in tests {
            let output = Roll::parse_modified_roll(input).unwrap();
            assert_eq!(output, expected_output);
        }
    }

    #[test]
    fn test_err_modified_roll() {
        let tests = [
            ("4d10+unparsable_modifier", RollError::ParsingError),
            ("4d10-unparsable_modifier", RollError::ParsingError),
            ("4d10  + unparsable_modifier", RollError::ParsingError),
            ("4d10  - unparsable_modifier", RollError::ParsingError),
            ("4dinvalid_die_type", RollError::ParsingError),
            ("invalid_die_amountd20", RollError::ParsingError),
        ];

        for (input, expected_output) in tests {
            let output = Roll::parse_modified_roll(input).unwrap_err();
            assert_eq!(output, expected_output);
        }
    }

    #[test]
    fn test_err_parse_roll() {
        let tests = [
            ("4d5", RollError::DieTypeInvalid),
            ("0d20", RollError::NoDiceToRoll),
            ("0d5", RollError::DieTypeInvalid),
            ("9001d20", RollError::DiceExceedLimit),
        ];

        for (input, expected_output) in tests {
            let output = Roll::parse_roll(input).unwrap_err();
            assert_eq!(output, expected_output);
        }
    }
}