2023-05-13 14:50:18 +00:00
|
|
|
use crate::units::{Metric, MetricQuantity, NonMetric, NonMetricQuantity};
|
|
|
|
|
2023-05-13 23:01:50 +00:00
|
|
|
pub fn convert(from: NonMetricQuantity) -> MetricQuantity {
|
|
|
|
let conversion = get_conversion(from.unit);
|
2023-05-28 17:32:31 +00:00
|
|
|
let amount = (from.amount - conversion.offset) * conversion.to.amount / conversion.from;
|
2023-05-13 23:01:50 +00:00
|
|
|
let unit = conversion.to.unit;
|
|
|
|
MetricQuantity { amount, unit }
|
|
|
|
}
|
|
|
|
|
2023-05-13 14:50:18 +00:00
|
|
|
struct Conversion {
|
2023-05-28 17:32:31 +00:00
|
|
|
offset: f64,
|
2023-05-13 14:50:18 +00:00
|
|
|
from: f64,
|
|
|
|
to: MetricQuantity,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_conversion(unit: NonMetric) -> Conversion {
|
2023-05-13 17:29:34 +00:00
|
|
|
let inch_from = 10_000.0;
|
|
|
|
let inch_to = 254.0;
|
|
|
|
|
|
|
|
let pound_from = 100_000.0;
|
|
|
|
let pound_to = 45359237.0;
|
|
|
|
|
2023-05-13 14:50:18 +00:00
|
|
|
match unit {
|
|
|
|
// Length
|
|
|
|
NonMetric::Inch => Conversion {
|
2023-05-28 17:32:31 +00:00
|
|
|
offset: 0.0,
|
2023-05-13 17:29:34 +00:00
|
|
|
from: inch_from,
|
|
|
|
to: MetricQuantity { amount: inch_to, unit: Metric::Metre },
|
2023-05-13 14:50:18 +00:00
|
|
|
},
|
2023-05-13 23:18:27 +00:00
|
|
|
NonMetric::Foot => Conversion {
|
2023-05-28 17:32:31 +00:00
|
|
|
offset: 0.0,
|
2023-05-13 23:18:27 +00:00
|
|
|
from: inch_from,
|
|
|
|
to: MetricQuantity { amount: 12.0 * inch_to, unit: Metric::Metre },
|
|
|
|
},
|
2023-05-13 14:50:18 +00:00
|
|
|
NonMetric::Yard => Conversion {
|
2023-05-28 17:32:31 +00:00
|
|
|
offset: 0.0,
|
2023-05-13 17:29:34 +00:00
|
|
|
from: inch_from,
|
|
|
|
to: MetricQuantity { amount: 3.0 * 12.0 * inch_to, unit: Metric::Metre },
|
2023-05-13 14:50:18 +00:00
|
|
|
},
|
|
|
|
NonMetric::Mile => Conversion {
|
2023-05-28 17:32:31 +00:00
|
|
|
offset: 0.0,
|
2023-05-13 17:29:34 +00:00
|
|
|
from: inch_from,
|
|
|
|
to: MetricQuantity { amount: 1760.0 * 3.0 * 12.0 * inch_to, unit: Metric::Metre },
|
2023-05-13 14:50:18 +00:00
|
|
|
},
|
|
|
|
// Weight
|
|
|
|
NonMetric::Ounce => Conversion {
|
2023-05-28 17:32:31 +00:00
|
|
|
offset: 0.0,
|
2023-05-13 17:29:34 +00:00
|
|
|
from: 16.0 * pound_from,
|
|
|
|
to: MetricQuantity { amount: pound_to, unit: Metric::Gram },
|
2023-05-13 14:50:18 +00:00
|
|
|
},
|
|
|
|
NonMetric::Pound => Conversion {
|
2023-05-28 17:32:31 +00:00
|
|
|
offset: 0.0,
|
2023-05-13 17:29:34 +00:00
|
|
|
from: pound_from,
|
|
|
|
to: MetricQuantity { amount: pound_to, unit: Metric::Gram },
|
2023-05-13 14:50:18 +00:00
|
|
|
},
|
|
|
|
NonMetric::Stone => Conversion {
|
2023-05-28 17:32:31 +00:00
|
|
|
offset: 0.0,
|
2023-05-13 17:29:34 +00:00
|
|
|
from: pound_from,
|
|
|
|
to: MetricQuantity { amount: 14.0 * pound_to, unit: Metric::Gram },
|
2023-05-13 14:50:18 +00:00
|
|
|
},
|
2023-05-28 17:32:31 +00:00
|
|
|
// Temperature
|
|
|
|
NonMetric::Fahrenheit => Conversion {
|
|
|
|
offset: 32.0,
|
|
|
|
from: 9.0,
|
2023-05-29 18:37:56 +00:00
|
|
|
to: MetricQuantity { amount: 5.0, unit: Metric::Celsius },
|
2023-05-28 17:32:31 +00:00
|
|
|
},
|
2023-05-28 23:55:43 +00:00
|
|
|
// Area
|
|
|
|
NonMetric::SquareInch => Conversion {
|
|
|
|
offset: 0.0,
|
|
|
|
from: inch_from * inch_from,
|
|
|
|
to: MetricQuantity { amount: inch_to * inch_to, unit: Metric::SquareMetre },
|
|
|
|
},
|
2023-05-29 00:01:56 +00:00
|
|
|
NonMetric::SquareFoot => Conversion {
|
|
|
|
offset: 0.0,
|
|
|
|
from: inch_from * inch_from,
|
|
|
|
to: MetricQuantity { amount: 12.0 * inch_to * 12.0 * inch_to, unit: Metric::SquareMetre },
|
|
|
|
},
|
2023-05-29 00:18:30 +00:00
|
|
|
NonMetric::Acre => Conversion {
|
|
|
|
offset: 0.0,
|
|
|
|
from: inch_from * inch_from,
|
|
|
|
to: MetricQuantity { amount: 43_560.0 * 12.0 * inch_to * 12.0 * inch_to, unit: Metric::SquareMetre },
|
|
|
|
},
|
2023-05-29 00:27:55 +00:00
|
|
|
NonMetric::SquareMile => Conversion {
|
|
|
|
offset: 0.0,
|
|
|
|
from: inch_from * inch_from,
|
|
|
|
to: MetricQuantity { amount: 1760.0 * 3.0 * 12.0 * inch_to * 1760.0 * 3.0 * 12.0 * inch_to, unit: Metric::SquareMetre },
|
|
|
|
},
|
2023-05-13 14:50:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
struct Test(NonMetric, f64);
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn length() {
|
|
|
|
let tests = [
|
|
|
|
Test(NonMetric::Inch, 0.0254),
|
|
|
|
Test(NonMetric::Foot, 0.3048),
|
|
|
|
Test(NonMetric::Yard, 0.9144),
|
|
|
|
Test(NonMetric::Mile, 1609.344),
|
|
|
|
];
|
|
|
|
run_tests(&tests, Metric::Metre);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn weight() {
|
|
|
|
let tests = [
|
|
|
|
Test(NonMetric::Ounce, 28.349523125),
|
|
|
|
Test(NonMetric::Pound, 453.59237),
|
|
|
|
Test(NonMetric::Stone, 6350.29318),
|
|
|
|
];
|
|
|
|
run_tests(&tests, Metric::Gram);
|
|
|
|
}
|
2023-05-13 23:01:50 +00:00
|
|
|
|
2023-05-28 17:32:31 +00:00
|
|
|
#[test]
|
|
|
|
fn temperature() {
|
|
|
|
assert_eq!(convert(NonMetricQuantity {
|
|
|
|
amount: -40.0,
|
|
|
|
unit: NonMetric::Fahrenheit,
|
|
|
|
}), MetricQuantity {
|
|
|
|
amount: -40.0,
|
2023-05-29 18:37:56 +00:00
|
|
|
unit: Metric::Celsius,
|
2023-05-28 17:32:31 +00:00
|
|
|
});
|
|
|
|
assert_eq!(convert(NonMetricQuantity {
|
|
|
|
amount: 32.0,
|
|
|
|
unit: NonMetric::Fahrenheit,
|
|
|
|
}), MetricQuantity {
|
|
|
|
amount: 0.0,
|
2023-05-29 18:37:56 +00:00
|
|
|
unit: Metric::Celsius,
|
2023-05-28 17:32:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-28 23:55:43 +00:00
|
|
|
#[test]
|
|
|
|
fn area() {
|
|
|
|
let tests = [
|
|
|
|
Test(NonMetric::SquareInch, 0.00064516),
|
2023-05-29 00:01:56 +00:00
|
|
|
Test(NonMetric::SquareFoot, 0.09290304),
|
2023-05-29 00:18:30 +00:00
|
|
|
Test(NonMetric::Acre, 4046.8564224),
|
2023-05-29 00:27:55 +00:00
|
|
|
Test(NonMetric::SquareMile, 2589988.110336),
|
2023-05-28 23:55:43 +00:00
|
|
|
];
|
|
|
|
run_tests(&tests, Metric::SquareMetre);
|
|
|
|
}
|
|
|
|
|
2023-05-13 23:01:50 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-05-13 14:50:18 +00:00
|
|
|
}
|