Add support for cubic yards

This commit is contained in:
Juhani Krekelä 2023-06-01 19:31:55 +03:00
parent 8e4680a3bc
commit e8e81b4564
5 changed files with 38 additions and 0 deletions

View File

@ -65,6 +65,7 @@ Supported units
* cubic inch (in³)
* cubic foot (ft³)
* cubic yard (yd³)
### Imperial fluid volume units

View File

@ -117,6 +117,11 @@ fn get_conversion(unit: NonMetric) -> Conversion {
from: inch_from * inch_from * inch_from,
to: MetricQuantity { amount: 12.0 * inch_to * 12.0 * inch_to * 12.0 * inch_to, unit: Metric::CubicMetre },
},
NonMetric::CubicYard => Conversion {
offset: 0.0,
from: inch_from * inch_from * inch_from,
to: MetricQuantity { amount: 3.0 * 12.0 * inch_to * 3.0 * 12.0 * inch_to * 3.0 * 12.0 * inch_to, unit: Metric::CubicMetre },
},
// Fluid volume
NonMetric::ImperialFluidOunce => Conversion {
offset: 0.0,
@ -240,6 +245,7 @@ mod test {
let tests = [
Test(NonMetric::CubicInch, 1.6387064e-5),
Test(NonMetric::CubicFoot, 0.028316846592),
Test(NonMetric::CubicYard, 0.764554857984),
];
run_tests(&tests, Metric::CubicMetre);
}

View File

@ -90,6 +90,7 @@ fn unit_to_name(unit: NonMetric) -> &'static str {
// Volume
NonMetric::CubicInch => "cubic inches",
NonMetric::CubicFoot => "cubic feet",
NonMetric::CubicYard => "cubic yards",
// Fluid volume
NonMetric::ImperialFluidOunce => "imperial fluid ounces",
NonMetric::ImperialPint => "imperial pints",
@ -147,6 +148,7 @@ mod test {
// Volume
assert_eq!(run("1 in³"), Ok("16.39 cm³".to_string()));
assert_eq!(run("1 ft³"), Ok("28 317 cm³".to_string()));
assert_eq!(run("1 yd³"), Ok("764 555 cm³".to_string()));
// Fluid volume
assert_eq!(run("1 imp fl oz"), Ok("2.841 cl".to_string()));
assert_eq!(run("1 imp pt"), Ok("5.683 dl".to_string()));

View File

@ -213,6 +213,19 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
"feet^3" => Ok(NonMetric::CubicFoot),
"ft^3" => Ok(NonMetric::CubicFoot),
"cubic yard" => Ok(NonMetric::CubicYard),
"cubic yards" => Ok(NonMetric::CubicYard),
"cubic yd" => Ok(NonMetric::CubicYard),
"cu yard" => Ok(NonMetric::CubicYard),
"cu yards" => Ok(NonMetric::CubicYard),
"cu yd" => Ok(NonMetric::CubicYard),
"yard³" => Ok(NonMetric::CubicYard),
"yards³" => Ok(NonMetric::CubicYard),
"yd³" => Ok(NonMetric::CubicYard),
"yard^3" => Ok(NonMetric::CubicYard),
"yards^3" => Ok(NonMetric::CubicYard),
"yd^3" => Ok(NonMetric::CubicYard),
// Fluid volume
"imperial fluid ounce" => Ok(NonMetric::ImperialFluidOunce),
"imperial fluid ounces" => Ok(NonMetric::ImperialFluidOunce),
@ -630,6 +643,21 @@ mod test {
"ft^3",
]);
test_units(NonMetric::CubicYard, &[
"cubic yard",
"cubic yards",
"cubic yd",
"cu yard",
"cu yards",
"cu yd",
"yard³",
"yards³",
"yd³",
"yard^3",
"yards^3",
"yd^3",
]);
// Fluid volume
test_units(NonMetric::ImperialFluidOunce, &[
"imperial fluid ounce",

View File

@ -32,6 +32,7 @@ pub enum NonMetric {
// Volume
CubicInch,
CubicFoot,
CubicYard,
// Fluid volume
ImperialFluidOunce,
ImperialPint,