diff --git a/src/lib.rs b/src/lib.rs index c428c80..d89ff75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ mod units; use conversions::convert; use format::format; use parse::{parse, ParseError}; -use units::{MetricQuantity, NonMetric}; +use units::{Metric, MetricQuantity, NonMetric}; pub fn run(input: &str) -> Result { let non_metric = match parse(input) { @@ -32,7 +32,8 @@ pub fn run(input: &str) -> Result { let metric: Vec = non_metric.clone().into_iter().map(convert).collect(); // Make sure the results of the conversions can be summed together - // This is the case if the units after conversion are the same + // This is the case if the units after conversion are the same and + // they are not temperature units (i.e. degrees Celsius) let mut metric_unit = None; for index in 0..metric.len() { match metric_unit { @@ -48,6 +49,9 @@ pub fn run(input: &str) -> Result { } } } + if metric.len() > 1 && metric_unit == Some(Metric::Celsius) { + return Err("Cannot sum together temperatures".to_string()); + } let amount = metric.into_iter().map(|quantity| { quantity.amount }).sum(); @@ -92,6 +96,7 @@ mod test { assert_eq!(run("1"), Err("Expected a unit".to_string())); assert_eq!(run(""), Err("Expected quantity or quantities to convert".to_string())); assert_eq!(run("6 ft 1 lbs"), Err("Incompatible units: feet, pounds".to_string())); + assert_eq!(run("0 °F 0 °F"), Err("Cannot sum together temperatures".to_string())); } #[test]