Error message edits

pull/2/head
Mark 2023-06-07 14:43:05 -07:00
parent c73bec0c13
commit 4eff57c273
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
3 changed files with 24 additions and 2 deletions

View File

@ -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) => { Err(EvalError::BadMath) => {
write!( write!(
stdout, "\n {}{}Mathematical Error: {}Failed to evaluate expression{}\r\n\n", stdout, "\n {}{}Mathematical Error: {}Failed to evaluate expression{}\r\n\n",

View File

@ -8,5 +8,7 @@ pub use crate::tokens::operator::Operator;
pub enum EvalError { pub enum EvalError {
BadMath, BadMath,
TooBig,
ZeroDivision,
IncompatibleUnit IncompatibleUnit
} }

View File

@ -365,7 +365,7 @@ impl Operator{
let args = args[0].as_number(); let args = args[0].as_number();
if let Token::Number(v) = args { 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( return Ok(Token::Number(
Quantity::new_rational(1f64).unwrap()/v Quantity::new_rational(1f64).unwrap()/v
)); ));
@ -478,7 +478,7 @@ impl Operator{
} }
if !v.fract().is_zero() { return Err(EvalError::BadMath); } 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 prod = Quantity::new_rational(1f64).unwrap();
let mut u = v.clone(); let mut u = v.clone();