mirror of https://github.com/rm-dr/daisy
Added detailed conversion error
parent
5ec3d6e380
commit
47cbd29c27
|
@ -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")
|
||||||
|
|
|
@ -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); }
|
||||||
|
|
Loading…
Reference in New Issue