Crate dice_expression

Source
Expand description

A library to help you parse and execute some dice expressions like:

  • 1d6 + 1
  • 2d20
  • 3d20k2 - 5
  • 10d20>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: d6 or 1d15 or 2d20
  • Dice modifiers:
    • Keep highest: 2d20kH, 2d20k1 or 5d20k2
    • Keep lowest: 2d20klL, 2d20kl1 or 5d8kl3
    • Drop highest: 2d20dhH, 2d20dh1 or 5d20dh2
    • Drop lowest: 2d20dL, 2d20d1 or 5d8d3
    • Reroll: 2d20r5 will reroll all results lower or equal to 5 until it gets results strictly superior to 5.
    • Reroll Once: 2d20ro5 will reroll all results lower or equal to 5, but only once.
    • Minimum: 2d20mi4 or 2d20min4 will change all results lower than 4 to the minimum value of 4.
    • Maximum: 2d20ma17 or 2d20max17 will change all results greater than 17 to the minimum value of 17.
    • Count number of results greater than: 2d20>10 will count the number of results that are greater or equal to 10.
    • Count number of results lower than: 2d20<8 will count the number of results that are lower or equal to 8.

All dice modifiers can be combined, like: 3d20r3k2max18

Structs§

Dice
A single, simple dice.
DiceExpression
A dice expression parsed from a string.

Enums§

DiceExpressionParsingError
Might be returned if an attempt to parse an invalid string to a DiceExpression.
EvaluateError
Might be returned during the evaluation of an expression.

Traits§

Evaluable
A trait marking something that can be evaluated to a specific value.