Compare commits
No commits in common. "1842d480e188e2b981cc34153a4e09d724dfb84e" and "055ab32a99840ecb8d21a1e236ea5976107b8984" have entirely different histories.
1842d480e1
...
055ab32a99
4 changed files with 12 additions and 17 deletions
|
@ -62,7 +62,7 @@ fn get_conversion(unit: NonMetric) -> Conversion {
|
||||||
NonMetric::Fahrenheit => Conversion {
|
NonMetric::Fahrenheit => Conversion {
|
||||||
offset: 32.0,
|
offset: 32.0,
|
||||||
from: 9.0,
|
from: 9.0,
|
||||||
to: MetricQuantity { amount: 5.0, unit: Metric::Celsius },
|
to: MetricQuantity { amount: 5.0, unit: Metric::Celcius },
|
||||||
},
|
},
|
||||||
// Area
|
// Area
|
||||||
NonMetric::SquareInch => Conversion {
|
NonMetric::SquareInch => Conversion {
|
||||||
|
@ -122,14 +122,14 @@ mod test {
|
||||||
unit: NonMetric::Fahrenheit,
|
unit: NonMetric::Fahrenheit,
|
||||||
}), MetricQuantity {
|
}), MetricQuantity {
|
||||||
amount: -40.0,
|
amount: -40.0,
|
||||||
unit: Metric::Celsius,
|
unit: Metric::Celcius,
|
||||||
});
|
});
|
||||||
assert_eq!(convert(NonMetricQuantity {
|
assert_eq!(convert(NonMetricQuantity {
|
||||||
amount: 32.0,
|
amount: 32.0,
|
||||||
unit: NonMetric::Fahrenheit,
|
unit: NonMetric::Fahrenheit,
|
||||||
}), MetricQuantity {
|
}), MetricQuantity {
|
||||||
amount: 0.0,
|
amount: 0.0,
|
||||||
unit: Metric::Celsius,
|
unit: Metric::Celcius,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,7 @@ fn prefixed_unit(quantity: MetricQuantity) -> PrefixedUnit {
|
||||||
return PrefixedUnit(1.0, "g");
|
return PrefixedUnit(1.0, "g");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Metric::Celsius => PrefixedUnit(1.0, "°C"),
|
Metric::Celcius => PrefixedUnit(1.0, "°C"),
|
||||||
Metric::SquareMetre => {
|
Metric::SquareMetre => {
|
||||||
if absolute >= 1000.0 * 1000.0 {
|
if absolute >= 1000.0 * 1000.0 {
|
||||||
return PrefixedUnit(1000.0 * 1000.0, "km²");
|
return PrefixedUnit(1000.0 * 1000.0, "km²");
|
||||||
|
@ -132,7 +132,7 @@ mod test {
|
||||||
|
|
||||||
assert_eq!("1 000 °C", &format(MetricQuantity {
|
assert_eq!("1 000 °C", &format(MetricQuantity {
|
||||||
amount: 1_000.0,
|
amount: 1_000.0,
|
||||||
unit: Metric::Celsius,
|
unit: Metric::Celcius,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,18 +259,18 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn celsius() {
|
fn celcius() {
|
||||||
assert_eq!(PrefixedUnit(1.0, "°C"), prefixed_unit(MetricQuantity {
|
assert_eq!(PrefixedUnit(1.0, "°C"), prefixed_unit(MetricQuantity {
|
||||||
amount: 0.0001,
|
amount: 0.0001,
|
||||||
unit: Metric::Celsius,
|
unit: Metric::Celcius,
|
||||||
}));
|
}));
|
||||||
assert_eq!(PrefixedUnit(1.0, "°C"), prefixed_unit(MetricQuantity {
|
assert_eq!(PrefixedUnit(1.0, "°C"), prefixed_unit(MetricQuantity {
|
||||||
amount: -1.0,
|
amount: -1.0,
|
||||||
unit: Metric::Celsius,
|
unit: Metric::Celcius,
|
||||||
}));
|
}));
|
||||||
assert_eq!(PrefixedUnit(1.0, "°C"), prefixed_unit(MetricQuantity {
|
assert_eq!(PrefixedUnit(1.0, "°C"), prefixed_unit(MetricQuantity {
|
||||||
amount: 1_000.0,
|
amount: 1_000.0,
|
||||||
unit: Metric::Celsius,
|
unit: Metric::Celcius,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ mod units;
|
||||||
use conversions::convert;
|
use conversions::convert;
|
||||||
use format::format;
|
use format::format;
|
||||||
use parse::{parse, ParseError};
|
use parse::{parse, ParseError};
|
||||||
use units::{Metric, MetricQuantity, NonMetric};
|
use units::{MetricQuantity, NonMetric};
|
||||||
|
|
||||||
pub fn run(input: &str) -> Result<String, String> {
|
pub fn run(input: &str) -> Result<String, String> {
|
||||||
let non_metric = match parse(input) {
|
let non_metric = match parse(input) {
|
||||||
|
@ -32,8 +32,7 @@ pub fn run(input: &str) -> Result<String, String> {
|
||||||
let metric: Vec<MetricQuantity> = non_metric.clone().into_iter().map(convert).collect();
|
let metric: Vec<MetricQuantity> = non_metric.clone().into_iter().map(convert).collect();
|
||||||
|
|
||||||
// Make sure the results of the conversions can be summed together
|
// Make sure the results of the conversions can be summed together
|
||||||
// This is the case if the units after conversion are the same and
|
// This is the case if the units after conversion are the same
|
||||||
// they are not temperature units (i.e. degrees Celsius)
|
|
||||||
let mut metric_unit = None;
|
let mut metric_unit = None;
|
||||||
for index in 0..metric.len() {
|
for index in 0..metric.len() {
|
||||||
match metric_unit {
|
match metric_unit {
|
||||||
|
@ -49,9 +48,6 @@ 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();
|
let amount = metric.into_iter().map(|quantity| { quantity.amount }).sum();
|
||||||
|
|
||||||
|
@ -96,7 +92,6 @@ mod test {
|
||||||
assert_eq!(run("1"), Err("Expected a unit".to_string()));
|
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(""), 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("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]
|
#[test]
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
pub enum Metric {
|
pub enum Metric {
|
||||||
Metre,
|
Metre,
|
||||||
Gram,
|
Gram,
|
||||||
Celsius,
|
Celcius,
|
||||||
SquareMetre,
|
SquareMetre,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue