mirror of https://github.com/rm-dr/daisy
Added detailed conversion error
parent
5ec3d6e380
commit
47cbd29c27
|
@ -10,6 +10,7 @@ pub enum EvalError {
|
|||
TooBig,
|
||||
ZeroDivision,
|
||||
IncompatibleUnit,
|
||||
IncompatibleUnits(String, String),
|
||||
BadDefineName,
|
||||
Undefined(String)
|
||||
}
|
||||
|
@ -28,7 +29,10 @@ impl ToString for EvalError {
|
|||
String::from("Division by zero")
|
||||
},
|
||||
EvalError::IncompatibleUnit => {
|
||||
String::from("Incompatible units")
|
||||
String::from("Incompatible unit")
|
||||
},
|
||||
EvalError::IncompatibleUnits(a, b) => {
|
||||
format!("Incompatible units ({} and {})", a, b)
|
||||
},
|
||||
EvalError::BadDefineName => {
|
||||
String::from("Invalid variable name")
|
||||
|
|
|
@ -157,7 +157,13 @@ pub fn eval_operator(g: &Expression, context: &mut Context) -> Result<Option<Exp
|
|||
if let Expression::Quantity(lb, vb) = b {
|
||||
let n = va.clone().convert_to(vb.clone());
|
||||
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())));
|
||||
} else { return Ok(None); }
|
||||
|
|
Loading…
Reference in New Issue