From 4eff57c273c9169fa4050de00de89313d7a8a8d0 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 7 Jun 2023 14:43:05 -0700 Subject: [PATCH] Error message edits --- src/entry/unix/unix.rs | 20 ++++++++++++++++++++ src/tokens/mod.rs | 2 ++ src/tokens/operator.rs | 4 ++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/entry/unix/unix.rs b/src/entry/unix/unix.rs index b26d69e..b8987ce 100644 --- a/src/entry/unix/unix.rs +++ b/src/entry/unix/unix.rs @@ -57,6 +57,26 @@ fn do_expression( )?; }, + Err(EvalError::TooBig) => { + write!( + stdout, "\n {}{}Mathematical Error: {}Number too big{}\r\n\n", + style::Bold, + color::Fg(color::Red), + style::Reset, + color::Fg(color::Reset), + )?; + }, + + Err(EvalError::ZeroDivision) => { + write!( + stdout, "\n {}{}Mathematical Error: {}Division by zero{}\r\n\n", + style::Bold, + color::Fg(color::Red), + style::Reset, + color::Fg(color::Reset), + )?; + }, + Err(EvalError::BadMath) => { write!( stdout, "\n {}{}Mathematical Error: {}Failed to evaluate expression{}\r\n\n", diff --git a/src/tokens/mod.rs b/src/tokens/mod.rs index 65c21c3..d9dc9f6 100644 --- a/src/tokens/mod.rs +++ b/src/tokens/mod.rs @@ -8,5 +8,7 @@ pub use crate::tokens::operator::Operator; pub enum EvalError { BadMath, + TooBig, + ZeroDivision, IncompatibleUnit } \ No newline at end of file diff --git a/src/tokens/operator.rs b/src/tokens/operator.rs index e6cf835..07b2c39 100644 --- a/src/tokens/operator.rs +++ b/src/tokens/operator.rs @@ -365,7 +365,7 @@ impl Operator{ let args = args[0].as_number(); if let Token::Number(v) = args { - if v.is_zero() { return Err(EvalError::BadMath); } + if v.is_zero() { return Err(EvalError::ZeroDivision); } return Ok(Token::Number( Quantity::new_rational(1f64).unwrap()/v )); @@ -478,7 +478,7 @@ impl Operator{ } if !v.fract().is_zero() { return Err(EvalError::BadMath); } - if v > Quantity::new_rational(50_000f64).unwrap() { return Err(EvalError::IncompatibleUnit); } + if v > Quantity::new_rational(50_000f64).unwrap() { return Err(EvalError::TooBig); } let mut prod = Quantity::new_rational(1f64).unwrap(); let mut u = v.clone();