From fda6550d1a51216fb1d48368945850c045e6832f Mon Sep 17 00:00:00 2001 From: Kasper Date: Tue, 14 Jan 2020 17:30:46 +0100 Subject: [PATCH] Fixed ambiguity by renaming UnitType::NoUnit to NoUnitType --- src/evaluator.rs | 20 ++++++++++---------- src/units.rs | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/evaluator.rs b/src/evaluator.rs index 2fa906b..60d4f31 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -98,43 +98,43 @@ fn evaluate_node(ast_node: &AstNode) -> Result { let child_answer = evaluate_node(child_node)?; match function { Cbrt => { - if child_answer.unit.category() == UnitType::NoUnit { + if child_answer.unit.category() == UnitType::NoType { let result = cbrt(child_answer.value); return Ok(Number::new(result, child_answer.unit)) } else { - return Err(format!("log() only accepts UnitType::NoUnit").to_string()) + return Err(format!("log() only accepts UnitType::NoType").to_string()) } }, Sqrt => { - if child_answer.unit.category() == UnitType::NoUnit { + if child_answer.unit.category() == UnitType::NoType { let result = sqrt(child_answer.value); return Ok(Number::new(result, child_answer.unit)) } else { - return Err(format!("log() only accepts UnitType::NoUnit").to_string()) + return Err(format!("log() only accepts UnitType::NoType").to_string()) } }, Log => { - if child_answer.unit.category() == UnitType::NoUnit { + if child_answer.unit.category() == UnitType::NoType { let result = child_answer.value.log10(); return Ok(Number::new(result, child_answer.unit)) } else { - return Err(format!("log() only accepts UnitType::NoUnit").to_string()) + return Err(format!("log() only accepts UnitType::NoType").to_string()) } }, Ln => { - if child_answer.unit.category() == UnitType::NoUnit { + if child_answer.unit.category() == UnitType::NoType { let result = child_answer.value.ln(); return Ok(Number::new(result, child_answer.unit)) } else { - return Err(format!("ln() only accepts UnitType::NoUnit").to_string()) + return Err(format!("ln() only accepts UnitType::NoType").to_string()) } }, Exp => { - if child_answer.unit.category() == UnitType::NoUnit { + if child_answer.unit.category() == UnitType::NoType { let result = child_answer.value.exp(child_answer.value); return Ok(Number::new(result, child_answer.unit)) } else { - return Err(format!("exp() only accepts UnitType::NoUnit").to_string()) + return Err(format!("exp() only accepts UnitType::NoType").to_string()) } }, Round => { diff --git a/src/units.rs b/src/units.rs index 10e8b27..d38cde9 100644 --- a/src/units.rs +++ b/src/units.rs @@ -2,7 +2,7 @@ use decimal::d128; #[derive(Clone, Copy, PartialEq, Debug)] pub enum UnitType { - NoUnit, + NoType, Time, Length, Area, @@ -45,7 +45,7 @@ macro_rules! create_units { } create_units!( - NoUnit: (UnitType::NoUnit, d128!(1)), + NoUnit: (NoType, d128!(1)), Nanosecond: (Time, d128!(1)), Microsecond: (Time, d128!(1000)), @@ -289,16 +289,16 @@ pub fn subtract(left: Number, right: Number) -> Result { } pub fn multiply(left: Number, right: Number) -> Result { - if left.unit == Unit::NoUnit && right.unit == Unit::NoUnit { + if left.unit == NoUnit && right.unit == NoUnit { // 3 * 2 return Ok(Number::new(left.value * right.value, left.unit)) } else if left.unit.category() == Temperature || right.unit.category() == Temperature { // if temperature return Err(format!("Cannot multiply {:?} and {:?}", left.unit, right.unit)) - } else if left.unit == Unit::NoUnit && right.unit != Unit::NoUnit { + } else if left.unit == NoUnit && right.unit != NoUnit { // 3 * 1 km return Ok(Number::new(left.value * right.value, right.unit)) - } else if right.unit == Unit::NoUnit && left.unit != Unit::NoUnit { + } else if right.unit == NoUnit && left.unit != NoUnit { // 1 km * 3 return Ok(Number::new(left.value * right.value, left.unit)) } else { @@ -307,19 +307,19 @@ pub fn multiply(left: Number, right: Number) -> Result { } pub fn divide(left: Number, right: Number) -> Result { - if left.unit == Unit::NoUnit && right.unit == Unit::NoUnit { + if left.unit == NoUnit && right.unit == NoUnit { // 3 / 2 Ok(Number::new(left.value / right.value, left.unit)) } else if left.unit.category() == Temperature || right.unit.category() == Temperature { // if temperature return Err(format!("Cannot divide {:?} by {:?}", left.unit, right.unit)) - } else if left.unit != Unit::NoUnit && right.unit == Unit::NoUnit { + } else if left.unit != NoUnit && right.unit == NoUnit { // 1 km / 2 Ok(Number::new(left.value / right.value, right.unit)) } else if left.unit.category() == right.unit.category() { // 1 km / 1 km let (left, right) = convert_to_lowest(left, right)?; - Ok(Number::new(left.value * right.value, Unit::NoUnit)) + Ok(Number::new(left.value * right.value, NoUnit)) } else { Err(format!("Cannot divide {:?} by {:?}", left.unit, right.unit)) } @@ -339,10 +339,10 @@ pub fn modulo(left: Number, right: Number) -> Result { } pub fn pow(left: Number, right: Number) -> Result { - if left.unit == Unit::NoUnit && right.unit == Unit::NoUnit { + if left.unit == NoUnit && right.unit == NoUnit { // 3 ^ 2 return Ok(Number::new(left.value.pow(right.value), left.unit)) - } else if right.unit == Unit::NoUnit && left.unit != Unit::NoUnit { + } else if right.unit == NoUnit && left.unit != NoUnit { // 1 km ^ 3 return Ok(Number::new(left.value.pow(right.value), left.unit)) } else {