aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/commands/roll.rs26
1 files changed, 12 insertions, 14 deletions
diff --git a/src/commands/roll.rs b/src/commands/roll.rs
index 6cd084c..abd7466 100644
--- a/src/commands/roll.rs
+++ b/src/commands/roll.rs
@@ -1,11 +1,11 @@
-use std::result::Result as StdResult;
-
use lazy_static::lazy_static;
use log::{
debug,
error,
};
+use pest::iterators::Pair;
use rand::prelude::*;
+use std::result::Result as StdResult;
use thiserror::Error;
use crate::{
@@ -32,10 +32,7 @@ pub(crate) enum CalcError {
impl Calc {
fn eval<S: AsRef<str>>(s: S) -> StdResult<f64, CalcError> {
use pest::{
- iterators::{
- Pair,
- Pairs,
- },
+ iterators::Pairs,
pratt_parser::PrattParser,
Parser,
};
@@ -46,16 +43,15 @@ impl Calc {
static ref CLIMBER: PrattParser<Rule> = {
use pest::pratt_parser::{
Assoc::*,
- Op as Operator,
+ Op,
};
PrattParser::new()
- .op(Operator::infix(add, Left)
- | Operator::infix(sub, Left)
- | Operator::infix(modulo, Left))
- .op(Operator::infix(mul, Left) | Operator::infix(div, Left))
- .op(Operator::infix(dice, Left))
- .op(Operator::infix(pow, Right))
+ .op(Op::infix(add, Left) | Op::infix(sub, Left) | Op::infix(modulo, Left))
+ .op(Op::infix(mul, Left) | Op::infix(div, Left))
+ .op(Op::infix(dice, Left))
+ .op(Op::infix(pow, Right))
+ .op(Op::postfix(EOI)) // discarded below
};
}
@@ -172,6 +168,7 @@ impl Calc {
Ok(result)
})
+ .map_postfix(|arg, _post| arg) // discard EOI
.parse(p)
}
@@ -202,7 +199,8 @@ mod test {
}
}
-#[poise::command(slash_command, prefix_command, guild_only, aliases("calc", "calculate"))]
+/// Roll some number of dice or perform a calculation.
+#[poise::command(prefix_command, guild_only, aliases("calc", "calculate"))]
pub async fn roll(ctx: PoiseContext<'_>, #[rest] rest: String) -> anyhow::Result<()> {
match Calc::eval(&rest) {
Ok(result) => {