Fixed ambiguity by renaming UnitType::NoUnit to NoUnitType
This commit is contained in:
parent
801fbcf974
commit
fda6550d1a
@ -98,43 +98,43 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
|
||||
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 => {
|
||||
|
||||
20
src/units.rs
20
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<Number, String> {
|
||||
}
|
||||
|
||||
pub fn multiply(left: Number, right: Number) -> Result<Number, String> {
|
||||
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<Number, String> {
|
||||
}
|
||||
|
||||
pub fn divide(left: Number, right: Number) -> Result<Number, String> {
|
||||
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<Number, String> {
|
||||
}
|
||||
|
||||
pub fn pow(left: Number, right: Number) -> Result<Number, String> {
|
||||
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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user