Expand description
A library to help you parse and execute some dice expressions like:
1d6 + 12d203d20k2 - 510d20>10
§Examples
A simple dice roll might look like:
use dice_expression::{Dice, Evaluable};
let d20 = Dice::from(20);
let roll_result = d20.evaluate()
.unwrap();A more complete expression that requires parsing would look like:
use std::str::FromStr;
use dice_expression::{DiceExpression, Evaluable};
let expr = DiceExpression::from_str("2d20k1min3 - 1d4 + 3").unwrap();
let roll_result = expr.evaluate().unwrap();§Supported operations and modifiers
The following operations can be parsed:
- Basic operations:
+,-,* - Negation:
- - Parenthesis:
(and) - Dices:
d6or1d15or2d20 - Dice modifiers:
- Keep highest:
2d20kH,2d20k1or5d20k2 - Keep lowest:
2d20klL,2d20kl1or5d8kl3 - Drop highest:
2d20dhH,2d20dh1or5d20dh2 - Drop lowest:
2d20dL,2d20d1or5d8d3 - Reroll:
2d20r5will reroll all results lower or equal to 5 until it gets results strictly superior to 5. - Reroll Once:
2d20ro5will reroll all results lower or equal to 5, but only once. - Minimum:
2d20mi4or2d20min4will change all results lower than 4 to the minimum value of 4. - Maximum:
2d20ma17or2d20max17will change all results greater than 17 to the minimum value of 17. - Count number of results greater than:
2d20>10will count the number of results that are greater or equal to 10. - Count number of results lower than:
2d20<8will count the number of results that are lower or equal to 8.
- Keep highest:
All dice modifiers can be combined, like: 3d20r3k2max18
Structs§
- Dice
- A single, simple dice.
- Dice
Expression - A dice expression parsed from a string.
Enums§
- Dice
Expression Parsing Error - Might be returned if an attempt to parse an invalid string to a DiceExpression.
- Evaluate
Error - Might be returned during the evaluation of an expression.
Traits§
- Evaluable
- A trait marking something that can be evaluated to a specific value.