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)?;
|
let child_answer = evaluate_node(child_node)?;
|
||||||
match function {
|
match function {
|
||||||
Cbrt => {
|
Cbrt => {
|
||||||
if child_answer.unit.category() == UnitType::NoUnit {
|
if child_answer.unit.category() == UnitType::NoType {
|
||||||
let result = cbrt(child_answer.value);
|
let result = cbrt(child_answer.value);
|
||||||
return Ok(Number::new(result, child_answer.unit))
|
return Ok(Number::new(result, child_answer.unit))
|
||||||
} else {
|
} else {
|
||||||
return Err(format!("log() only accepts UnitType::NoUnit").to_string())
|
return Err(format!("log() only accepts UnitType::NoType").to_string())
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Sqrt => {
|
Sqrt => {
|
||||||
if child_answer.unit.category() == UnitType::NoUnit {
|
if child_answer.unit.category() == UnitType::NoType {
|
||||||
let result = sqrt(child_answer.value);
|
let result = sqrt(child_answer.value);
|
||||||
return Ok(Number::new(result, child_answer.unit))
|
return Ok(Number::new(result, child_answer.unit))
|
||||||
} else {
|
} else {
|
||||||
return Err(format!("log() only accepts UnitType::NoUnit").to_string())
|
return Err(format!("log() only accepts UnitType::NoType").to_string())
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Log => {
|
Log => {
|
||||||
if child_answer.unit.category() == UnitType::NoUnit {
|
if child_answer.unit.category() == UnitType::NoType {
|
||||||
let result = child_answer.value.log10();
|
let result = child_answer.value.log10();
|
||||||
return Ok(Number::new(result, child_answer.unit))
|
return Ok(Number::new(result, child_answer.unit))
|
||||||
} else {
|
} else {
|
||||||
return Err(format!("log() only accepts UnitType::NoUnit").to_string())
|
return Err(format!("log() only accepts UnitType::NoType").to_string())
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Ln => {
|
Ln => {
|
||||||
if child_answer.unit.category() == UnitType::NoUnit {
|
if child_answer.unit.category() == UnitType::NoType {
|
||||||
let result = child_answer.value.ln();
|
let result = child_answer.value.ln();
|
||||||
return Ok(Number::new(result, child_answer.unit))
|
return Ok(Number::new(result, child_answer.unit))
|
||||||
} else {
|
} else {
|
||||||
return Err(format!("ln() only accepts UnitType::NoUnit").to_string())
|
return Err(format!("ln() only accepts UnitType::NoType").to_string())
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Exp => {
|
Exp => {
|
||||||
if child_answer.unit.category() == UnitType::NoUnit {
|
if child_answer.unit.category() == UnitType::NoType {
|
||||||
let result = child_answer.value.exp(child_answer.value);
|
let result = child_answer.value.exp(child_answer.value);
|
||||||
return Ok(Number::new(result, child_answer.unit))
|
return Ok(Number::new(result, child_answer.unit))
|
||||||
} else {
|
} else {
|
||||||
return Err(format!("exp() only accepts UnitType::NoUnit").to_string())
|
return Err(format!("exp() only accepts UnitType::NoType").to_string())
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Round => {
|
Round => {
|
||||||
|
|||||||
20
src/units.rs
20
src/units.rs
@ -2,7 +2,7 @@ use decimal::d128;
|
|||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||||
pub enum UnitType {
|
pub enum UnitType {
|
||||||
NoUnit,
|
NoType,
|
||||||
Time,
|
Time,
|
||||||
Length,
|
Length,
|
||||||
Area,
|
Area,
|
||||||
@ -45,7 +45,7 @@ macro_rules! create_units {
|
|||||||
}
|
}
|
||||||
|
|
||||||
create_units!(
|
create_units!(
|
||||||
NoUnit: (UnitType::NoUnit, d128!(1)),
|
NoUnit: (NoType, d128!(1)),
|
||||||
|
|
||||||
Nanosecond: (Time, d128!(1)),
|
Nanosecond: (Time, d128!(1)),
|
||||||
Microsecond: (Time, d128!(1000)),
|
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> {
|
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
|
// 3 * 2
|
||||||
return Ok(Number::new(left.value * right.value, left.unit))
|
return Ok(Number::new(left.value * right.value, left.unit))
|
||||||
} else if left.unit.category() == Temperature || right.unit.category() == Temperature {
|
} else if left.unit.category() == Temperature || right.unit.category() == Temperature {
|
||||||
// if temperature
|
// if temperature
|
||||||
return Err(format!("Cannot multiply {:?} and {:?}", left.unit, right.unit))
|
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
|
// 3 * 1 km
|
||||||
return Ok(Number::new(left.value * right.value, right.unit))
|
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
|
// 1 km * 3
|
||||||
return Ok(Number::new(left.value * right.value, left.unit))
|
return Ok(Number::new(left.value * right.value, left.unit))
|
||||||
} else {
|
} 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> {
|
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
|
// 3 / 2
|
||||||
Ok(Number::new(left.value / right.value, left.unit))
|
Ok(Number::new(left.value / right.value, left.unit))
|
||||||
} else if left.unit.category() == Temperature || right.unit.category() == Temperature {
|
} else if left.unit.category() == Temperature || right.unit.category() == Temperature {
|
||||||
// if temperature
|
// if temperature
|
||||||
return Err(format!("Cannot divide {:?} by {:?}", left.unit, right.unit))
|
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
|
// 1 km / 2
|
||||||
Ok(Number::new(left.value / right.value, right.unit))
|
Ok(Number::new(left.value / right.value, right.unit))
|
||||||
} else if left.unit.category() == right.unit.category() {
|
} else if left.unit.category() == right.unit.category() {
|
||||||
// 1 km / 1 km
|
// 1 km / 1 km
|
||||||
let (left, right) = convert_to_lowest(left, right)?;
|
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 {
|
} else {
|
||||||
Err(format!("Cannot divide {:?} by {:?}", left.unit, right.unit))
|
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> {
|
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
|
// 3 ^ 2
|
||||||
return Ok(Number::new(left.value.pow(right.value), left.unit))
|
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
|
// 1 km ^ 3
|
||||||
return Ok(Number::new(left.value.pow(right.value), left.unit))
|
return Ok(Number::new(left.value.pow(right.value), left.unit))
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user