Compare commits

...

2 Commits

Author SHA1 Message Date
Juhani Krekelä 1842d480e1 Disallow several temperatures in input 2023-05-29 21:39:48 +03:00
Juhani Krekelä 1a493a1716 Fix spelling Celcius → Celsius 2023-05-29 21:37:56 +03:00
4 changed files with 17 additions and 12 deletions

View File

@ -62,7 +62,7 @@ fn get_conversion(unit: NonMetric) -> Conversion {
NonMetric::Fahrenheit => Conversion {
offset: 32.0,
from: 9.0,
to: MetricQuantity { amount: 5.0, unit: Metric::Celcius },
to: MetricQuantity { amount: 5.0, unit: Metric::Celsius },
},
// Area
NonMetric::SquareInch => Conversion {
@ -122,14 +122,14 @@ mod test {
unit: NonMetric::Fahrenheit,
}), MetricQuantity {
amount: -40.0,
unit: Metric::Celcius,
unit: Metric::Celsius,
});
assert_eq!(convert(NonMetricQuantity {
amount: 32.0,
unit: NonMetric::Fahrenheit,
}), MetricQuantity {
amount: 0.0,
unit: Metric::Celcius,
unit: Metric::Celsius,
});
}

View File

@ -83,7 +83,7 @@ fn prefixed_unit(quantity: MetricQuantity) -> PrefixedUnit {
return PrefixedUnit(1.0, "g");
}
}
Metric::Celcius => PrefixedUnit(1.0, "°C"),
Metric::Celsius => PrefixedUnit(1.0, "°C"),
Metric::SquareMetre => {
if absolute >= 1000.0 * 1000.0 {
return PrefixedUnit(1000.0 * 1000.0, "km²");
@ -132,7 +132,7 @@ mod test {
assert_eq!("1 000 °C", &format(MetricQuantity {
amount: 1_000.0,
unit: Metric::Celcius,
unit: Metric::Celsius,
}));
}
@ -259,18 +259,18 @@ mod test {
}
#[test]
fn celcius() {
fn celsius() {
assert_eq!(PrefixedUnit(1.0, "°C"), prefixed_unit(MetricQuantity {
amount: 0.0001,
unit: Metric::Celcius,
unit: Metric::Celsius,
}));
assert_eq!(PrefixedUnit(1.0, "°C"), prefixed_unit(MetricQuantity {
amount: -1.0,
unit: Metric::Celcius,
unit: Metric::Celsius,
}));
assert_eq!(PrefixedUnit(1.0, "°C"), prefixed_unit(MetricQuantity {
amount: 1_000.0,
unit: Metric::Celcius,
unit: Metric::Celsius,
}));
}

View File

@ -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]

View File

@ -2,7 +2,7 @@
pub enum Metric {
Metre,
Gram,
Celcius,
Celsius,
SquareMetre,
}