Crates.io | saikoro |
lib.rs | saikoro |
version | 1.2.4 |
source | src |
created_at | 2024-01-13 06:33:53.49079 |
updated_at | 2024-05-31 15:52:44.726005 |
description | Parser and evaluator for doing math with dice notation expressions |
homepage | https://jolkert.dev/saikoro |
repository | https://github.com/Jolkert/saikoro |
max_upload_size | |
id | 1098338 |
size | 95,206 |
Saikoro is a library for evaluating dice rolls with a syntax similar to the Dice Notation
used by many tabletop RPGs with a few (maybe) convenient extras thrown in. Expressions are treated as mathemical expressions
where the D
or d
operator is used for rolling dice, and is a high-priority operator. It also comes with a very (painfully) simple
command-line executable implementation.
A very basic usage of Saikoro's library would look something like
fn main() {
if let Ok(roll) = saikoro::evaluate("8d6") {
println!("Fireball deals {} fire damage", roll.value),
}
}
D
/d
)+
& -
)*
& /
)%
)^
)==
, !=
, <
, >
, <=
, >=
)stats
Elements higher in the list are evaluated before those lower in the list
D
/d
^
*
/
%
+
-
Unary +
and -
are implemented, as well as a unary D
/d
operator
Unary D
/d
works the same as a binary D
/d
with 1
on the left-hand side (i.e. d20
is equivalent to 1d20
)
Comparison operators will remove elements in the left-hand roll item where the value does not meet the filter critera implied by the operator.
For example: 5d8 > 5
will cause the 5d8
result to only count rolls with a value greater than 5
(eg. if 5d8
would produce {1, 3, 4, 5, 8}
), the final total will be 13
, as the 1
3
and 4
will be filtered out)
D
/d
+ comparison operator is treated as a ternary operator with the absolute lowest priority (i.e. the comparison operator will
consider as its right-hand side, the entirety of the rest of the expression unless parentheses are used)
For example:
// this will produce an error variant at parse-time, as the left-hand side is a constant 6
let result = saikoro::evaluate("6 > 3");
assert!(result.is_err());
// this will also produce an error variant at parse-time, as after the addition, the `2d6` is a value, not a roll expression
let maybe_unintuitive = saikoro::evaluate("(2d6 + 5) > 9");
assert!(maybe_unintuitive.is_err());
-H
and -L
syntax for removing the highest/lowest value of a particular rollabs
, sin
, max
, log
)