Added detailed conversion error

pull/2/head
Mark 2023-08-01 09:43:47 -07:00
parent 5ec3d6e380
commit 47cbd29c27
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 12 additions and 2 deletions

View File

@ -10,6 +10,7 @@ pub enum EvalError {
TooBig, TooBig,
ZeroDivision, ZeroDivision,
IncompatibleUnit, IncompatibleUnit,
IncompatibleUnits(String, String),
BadDefineName, BadDefineName,
Undefined(String) Undefined(String)
} }
@ -28,7 +29,10 @@ impl ToString for EvalError {
String::from("Division by zero") String::from("Division by zero")
}, },
EvalError::IncompatibleUnit => { EvalError::IncompatibleUnit => {
String::from("Incompatible units") String::from("Incompatible unit")
},
EvalError::IncompatibleUnits(a, b) => {
format!("Incompatible units ({} and {})", a, b)
}, },
EvalError::BadDefineName => { EvalError::BadDefineName => {
String::from("Invalid variable name") String::from("Invalid variable name")

View File

@ -157,7 +157,13 @@ pub fn eval_operator(g: &Expression, context: &mut Context) -> Result<Option<Exp
if let Expression::Quantity(lb, vb) = b { if let Expression::Quantity(lb, vb) = b {
let n = va.clone().convert_to(vb.clone()); let n = va.clone().convert_to(vb.clone());
if n.is_none() { if n.is_none() {
return Err((*la + *lb + *op_loc, EvalError::IncompatibleUnit)); return Err((
*la + *lb + *op_loc,
EvalError::IncompatibleUnits(
va.convert_to_base().unit.to_string(),
vb.convert_to_base().unit.to_string()
)
));
} }
return Ok(Some(Expression::Quantity(*la + *lb, n.unwrap()))); return Ok(Some(Expression::Quantity(*la + *lb, n.unwrap())));
} else { return Ok(None); } } else { return Ok(None); }