Add support for imperial gallons

This commit is contained in:
Juhani Krekelä 2023-05-30 20:01:35 +03:00
parent 782e4a18a7
commit 9ef0c9655d
5 changed files with 73 additions and 0 deletions

View File

@ -20,6 +20,9 @@ fn get_conversion(unit: NonMetric) -> Conversion {
let pound_from = 100_000.0; let pound_from = 100_000.0;
let pound_to = 45359237.0; let pound_to = 45359237.0;
let imperial_gallon_from = 100_000.0;
let imperial_gallon_to = 454609.0;
match unit { match unit {
// Length // Length
NonMetric::Inch => Conversion { NonMetric::Inch => Conversion {
@ -96,6 +99,12 @@ fn get_conversion(unit: NonMetric) -> Conversion {
from: inch_from * inch_from * inch_from, 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 }, to: MetricQuantity { amount: 12.0 * inch_to * 12.0 * inch_to * 12.0 * inch_to, unit: Metric::CubicMetre },
}, },
// Fluid volume
NonMetric::ImperialGallon => Conversion {
offset: 0.0,
from: imperial_gallon_from,
to: MetricQuantity { amount: imperial_gallon_to, unit: Metric::Litre },
},
} }
} }
@ -164,6 +173,14 @@ mod test {
run_tests(&tests, Metric::CubicMetre); run_tests(&tests, Metric::CubicMetre);
} }
#[test]
fn fluid_volume() {
let tests = [
Test(NonMetric::ImperialGallon, 4.54609),
];
run_tests(&tests, Metric::Litre);
}
fn run_tests(tests: &[Test], unit: Metric) { fn run_tests(tests: &[Test], unit: Metric) {
for test in tests { for test in tests {
let from = NonMetricQuantity { let from = NonMetricQuantity {

View File

@ -106,6 +106,17 @@ fn prefixed_unit(quantity: MetricQuantity) -> PrefixedUnit {
return PrefixedUnit(0.001 * 0.001 * 0.001, "mm³"); return PrefixedUnit(0.001 * 0.001 * 0.001, "mm³");
} }
} }
Metric::Litre => {
if absolute >= 1.0 {
return PrefixedUnit(1.0, "l");
} else if absolute >= 0.1 {
return PrefixedUnit(0.1, "dl");
} else if absolute >= 0.01 {
return PrefixedUnit(0.01, "cl");
} else {
return PrefixedUnit(0.001, "ml");
}
}
} }
} }
@ -392,4 +403,32 @@ mod test {
unit: Metric::CubicMetre, unit: Metric::CubicMetre,
})); }));
} }
#[test]
fn litre() {
assert_eq!(PrefixedUnit(0.001, "ml"), prefixed_unit(MetricQuantity {
amount: 0.000_1,
unit: Metric::Litre,
}));
assert_eq!(PrefixedUnit(0.001, "ml"), prefixed_unit(MetricQuantity {
amount: 0.001,
unit: Metric::Litre,
}));
assert_eq!(PrefixedUnit(0.01, "cl"), prefixed_unit(MetricQuantity {
amount: 0.01,
unit: Metric::Litre,
}));
assert_eq!(PrefixedUnit(0.1, "dl"), prefixed_unit(MetricQuantity {
amount: 0.1,
unit: Metric::Litre,
}));
assert_eq!(PrefixedUnit(1.0, "l"), prefixed_unit(MetricQuantity {
amount: 1.0,
unit: Metric::Litre,
}));
assert_eq!(PrefixedUnit(1.0, "l"), prefixed_unit(MetricQuantity {
amount: 10.0,
unit: Metric::Litre,
}));
}
} }

View File

@ -84,6 +84,8 @@ fn unit_to_name(unit: NonMetric) -> &'static str {
// Volume // Volume
NonMetric::CubicInch => "cubic inches", NonMetric::CubicInch => "cubic inches",
NonMetric::CubicFoot => "cubic feet", NonMetric::CubicFoot => "cubic feet",
// Fluid volume
NonMetric::ImperialGallon => "imperial gallons",
} }
} }
@ -125,5 +127,7 @@ mod test {
// Volume // Volume
assert_eq!(run("1 in³"), Ok("16.39 cm³".to_string())); 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 ft³"), Ok("28 317 cm³".to_string()));
// Fluid volume
assert_eq!(run("1 imp gal"), Ok("4.546 l".to_string()));
} }
} }

View File

@ -185,6 +185,11 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
"feet^3" => Ok(NonMetric::CubicFoot), "feet^3" => Ok(NonMetric::CubicFoot),
"ft^3" => Ok(NonMetric::CubicFoot), "ft^3" => Ok(NonMetric::CubicFoot),
// Fluid volume
"imperial gallon" => Ok(NonMetric::ImperialGallon),
"imperial gallons" => Ok(NonMetric::ImperialGallon),
"imp gal" => Ok(NonMetric::ImperialGallon),
_ => Err(ParseError::UnknownUnit(input)), _ => Err(ParseError::UnknownUnit(input)),
} }
} }
@ -419,6 +424,11 @@ mod test {
assert_eq!(parse_unit("feet^3".to_string()), Ok(NonMetric::CubicFoot)); assert_eq!(parse_unit("feet^3".to_string()), Ok(NonMetric::CubicFoot));
assert_eq!(parse_unit("ft^3".to_string()), Ok(NonMetric::CubicFoot)); assert_eq!(parse_unit("ft^3".to_string()), Ok(NonMetric::CubicFoot));
// Fluid volume
assert_eq!(parse_unit("imperial gallon".to_string()), Ok(NonMetric::ImperialGallon));
assert_eq!(parse_unit("imperial gallons".to_string()), Ok(NonMetric::ImperialGallon));
assert_eq!(parse_unit("imp gal".to_string()), Ok(NonMetric::ImperialGallon));
// Unknown unit // Unknown unit
assert_eq!(parse_unit("hutenosa".to_string()), Err(ParseError::UnknownUnit("hutenosa".to_string()))); assert_eq!(parse_unit("hutenosa".to_string()), Err(ParseError::UnknownUnit("hutenosa".to_string())));
} }

View File

@ -5,6 +5,7 @@ pub enum Metric {
Celsius, Celsius,
SquareMetre, SquareMetre,
CubicMetre, CubicMetre,
Litre,
} }
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
@ -28,6 +29,8 @@ pub enum NonMetric {
// Volume // Volume
CubicInch, CubicInch,
CubicFoot, CubicFoot,
// Fluid volume
ImperialGallon,
} }
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]