mirror of
				https://github.com/rm-dr/daisy
				synced 2025-10-30 22:14:55 -07:00 
			
		
		
		
	Added detailed conversion error
This commit is contained in:
		| @ -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); } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user