diff --git a/src/conversions.rs b/src/conversions.rs index 9f65182..4cc5e1f 100644 --- a/src/conversions.rs +++ b/src/conversions.rs @@ -1,5 +1,12 @@ use crate::units::{Metric, MetricQuantity, NonMetric, NonMetricQuantity}; +pub fn convert(from: NonMetricQuantity) -> MetricQuantity { + let conversion = get_conversion(from.unit); + let amount = from.amount * conversion.to.amount / conversion.from; + let unit = conversion.to.unit; + MetricQuantity { amount, unit } +} + struct Conversion { from: f64, to: MetricQuantity, @@ -46,33 +53,12 @@ fn get_conversion(unit: NonMetric) -> Conversion { } } -pub fn convert(from: NonMetricQuantity) -> MetricQuantity { - let conversion = get_conversion(from.unit); - let amount = from.amount * conversion.to.amount / conversion.from; - let unit = conversion.to.unit; - MetricQuantity { amount, unit } -} - #[cfg(test)] mod test { use super::*; struct Test(NonMetric, f64); - fn run_tests(tests: &[Test], unit: Metric) { - for test in tests { - let from = NonMetricQuantity { - amount: 1.0, - unit: test.0, - }; - let to = MetricQuantity { - amount: test.1, - unit: unit, - }; - assert_eq!(convert(from), to); - } - } - #[test] fn length() { let tests = [ @@ -93,4 +79,18 @@ mod test { ]; run_tests(&tests, Metric::Gram); } + + fn run_tests(tests: &[Test], unit: Metric) { + for test in tests { + let from = NonMetricQuantity { + amount: 1.0, + unit: test.0, + }; + let to = MetricQuantity { + amount: test.1, + unit: unit, + }; + assert_eq!(convert(from), to); + } + } }