Reorder conversions.rs such that helpers are after primary functions
This commit is contained in:
parent
c79584d807
commit
7170ac3240
|
@ -1,5 +1,12 @@
|
||||||
use crate::units::{Metric, MetricQuantity, NonMetric, NonMetricQuantity};
|
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 {
|
struct Conversion {
|
||||||
from: f64,
|
from: f64,
|
||||||
to: MetricQuantity,
|
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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
struct Test(NonMetric, f64);
|
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]
|
#[test]
|
||||||
fn length() {
|
fn length() {
|
||||||
let tests = [
|
let tests = [
|
||||||
|
@ -93,4 +79,18 @@ mod test {
|
||||||
];
|
];
|
||||||
run_tests(&tests, Metric::Gram);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue