Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowRZ committed Nov 16, 2023
1 parent 1e3d785 commit c278ca7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ async fn send_error_message(
Some(&FuukaBotError::ShouldAvaliable) => RoomMessageEventContent::text_plain(format!(
"⁉️ The bot fired an internal error: {err:#}"
)),
Some(&FuukaBotError::MathOverflow) | Some(&FuukaBotError::DivByZero) => {
RoomMessageEventContent::text_plain(format!("⁉️ Math error happened: {err:#}"))
}
None => {
RoomMessageEventContent::text_plain(format!("⁉️ An unexpected error occoured: {err:#}"))
}
Expand Down
26 changes: 17 additions & 9 deletions src/dicer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use nom::sequence::{delimited, pair, preceded, separated_pair, terminated};
use nom::{bytes::complete::tag_no_case, IResult};
use std::str::FromStr;

use crate::FuukaBotError;

/// A dice candicate.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct DiceCandidate {
Expand Down Expand Up @@ -87,21 +89,27 @@ pub enum Expr {

impl Expr {
/// Evaluate the expression.
pub fn eval(self) -> i32 {
pub fn eval(self) -> anyhow::Result<i32> {
match self {
Self::DiceOrInt(result) => match result {
DiceOrInt::Dice(dice) => {
let Dice { count, sides } = dice;
(fastrand::u32(1..=sides) * count) as i32
Ok((fastrand::u32(1..=sides) * count) as i32)
}
DiceOrInt::Int(num) => num,
},
Self::BinOp { lhs, op, rhs } => match op {
Op::Add => lhs.eval() + rhs.eval(),
Op::Sub => lhs.eval() - rhs.eval(),
Op::Mul => lhs.eval() * rhs.eval(),
Op::Div => lhs.eval() / rhs.eval(),
DiceOrInt::Int(num) => Ok(num),
},
Self::BinOp { lhs, op, rhs } => {
match op {
Op::Add => Ok(i32::checked_add(lhs.eval()?, rhs.eval()?)
.ok_or(FuukaBotError::MathOverflow)?),
Op::Sub => Ok(i32::checked_sub(lhs.eval()?, rhs.eval()?)
.ok_or(FuukaBotError::MathOverflow)?),
Op::Mul => Ok(i32::checked_mul(lhs.eval()?, rhs.eval()?)
.ok_or(FuukaBotError::MathOverflow)?),
Op::Div => Ok(i32::checked_div(lhs.eval()?, rhs.eval()?)
.ok_or(FuukaBotError::DivByZero)?),
}
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ pub enum FuukaBotError {
/// The specified user does not exist.
#[error("The specified user does not exist.")]
UserNotFound,
/// Math overflow happened.
#[error("Math overflow happened.")]
MathOverflow,
/// Divide by zero happened.
#[error("Divisioned by zero.")]
DivByZero,
// Internal errors.
/// The bot encountered an internal error that the user it checked should be avaliable but didn't.
#[error("This user should be avaliable.")]
Expand Down
2 changes: 1 addition & 1 deletion src/message_responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ async fn _dispatch_dicer(body: &str) -> anyhow::Result<Option<RoomMessageEventCo
return Ok(Some(nom_error_message(expr, e)));
}
};
let result = cand.expr.eval();
let result = cand.expr.eval()?;
let string = match cand.target {
Some(target) => {
if result < (target as i32) {
Expand Down

0 comments on commit c278ca7

Please sign in to comment.