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
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(clippy::module_name_repetitions)]
#![deny(missing_docs)]
#[macro_use]
extern crate lazy_static;
pub mod dice;
pub mod error;
use anyhow::Result;
use dice::DiceRoll;
use evalexpr::eval;
use rand::{rngs::SmallRng, SeedableRng};
use regex::{Captures, Regex};
use std::borrow::Cow;
pub fn solve_dice_expression(expression: &str, random_seed: Option<u64>) -> Result<i64> {
lazy_static! {
static ref PATTERN: Regex = Regex::new(r"(\d+)d(\d+)").expect("Problem compiling regex");
}
let mut rng = match random_seed {
Some(inner) => SmallRng::seed_from_u64(inner),
None => SmallRng::from_entropy(),
};
let mut error = None;
let rolled_expression = PATTERN.replace_all(expression, |caps: &Captures| {
let diceroll_str = &caps.get(0).unwrap().as_str().to_string();
match DiceRoll::from_string(diceroll_str) {
Ok(dice) => match dice.roll(&mut rng) {
Ok(roll_result) => Cow::Owned(format!("{}", roll_result)),
Err(e) => {
error = Some(e.context(diceroll_str.clone()));
Cow::Borrowed("")
}
},
Err(e) => {
error = Some(e.context(diceroll_str.clone()));
Cow::Borrowed("")
}
}
});
if let Some(e) = error {
Err(e)
} else {
let result = eval(&rolled_expression)?.as_int()?;
Ok(result)
}
}