Disallow several temperatures in input
This commit is contained in:
parent
1a493a1716
commit
1842d480e1
|
@ -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<String, String> {
|
||||
let non_metric = match parse(input) {
|
||||
|
@ -32,7 +32,8 @@ pub fn run(input: &str) -> Result<String, String> {
|
|||
let metric: Vec<MetricQuantity> = 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<String, String> {
|
|||
}
|
||||
}
|
||||
}
|
||||
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]
|
||||
|
|
Loading…
Reference in New Issue