Reorder conversions.rs such that helpers are after primary functions

This commit is contained in:
Juhani Krekelä 2023-05-14 02:01:50 +03:00
parent c79584d807
commit 7170ac3240
1 changed files with 21 additions and 21 deletions

View File

@ -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);
}
}
}