Add support for imperial quarts

This commit is contained in:
Juhani Krekelä 2023-05-30 20:08:42 +03:00
parent 9ef0c9655d
commit 8d50c9643a
4 changed files with 17 additions and 0 deletions

View File

@ -105,6 +105,11 @@ fn get_conversion(unit: NonMetric) -> Conversion {
from: imperial_gallon_from,
to: MetricQuantity { amount: imperial_gallon_to, unit: Metric::Litre },
},
NonMetric::ImperialQuart => Conversion {
offset: 0.0,
from: 4.0 * imperial_gallon_from,
to: MetricQuantity { amount: imperial_gallon_to, unit: Metric::Litre },
},
}
}
@ -177,6 +182,7 @@ mod test {
fn fluid_volume() {
let tests = [
Test(NonMetric::ImperialGallon, 4.54609),
Test(NonMetric::ImperialQuart, 1.1365225),
];
run_tests(&tests, Metric::Litre);
}

View File

@ -86,6 +86,7 @@ fn unit_to_name(unit: NonMetric) -> &'static str {
NonMetric::CubicFoot => "cubic feet",
// Fluid volume
NonMetric::ImperialGallon => "imperial gallons",
NonMetric::ImperialQuart => "imperial quarts",
}
}
@ -129,5 +130,6 @@ mod test {
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()));
assert_eq!(run("1 imp qt"), Ok("1.137 l".to_string()));
}
}

View File

@ -190,6 +190,10 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
"imperial gallons" => Ok(NonMetric::ImperialGallon),
"imp gal" => Ok(NonMetric::ImperialGallon),
"imperial quart" => Ok(NonMetric::ImperialQuart),
"imperial quarts" => Ok(NonMetric::ImperialQuart),
"imp qt" => Ok(NonMetric::ImperialQuart),
_ => Err(ParseError::UnknownUnit(input)),
}
}
@ -429,6 +433,10 @@ mod test {
assert_eq!(parse_unit("imperial gallons".to_string()), Ok(NonMetric::ImperialGallon));
assert_eq!(parse_unit("imp gal".to_string()), Ok(NonMetric::ImperialGallon));
assert_eq!(parse_unit("imperial quart".to_string()), Ok(NonMetric::ImperialQuart));
assert_eq!(parse_unit("imperial quarts".to_string()), Ok(NonMetric::ImperialQuart));
assert_eq!(parse_unit("imp qt".to_string()), Ok(NonMetric::ImperialQuart));
// Unknown unit
assert_eq!(parse_unit("hutenosa".to_string()), Err(ParseError::UnknownUnit("hutenosa".to_string())));
}

View File

@ -31,6 +31,7 @@ pub enum NonMetric {
CubicFoot,
// Fluid volume
ImperialGallon,
ImperialQuart,
}
#[derive(Clone, Copy, Debug, PartialEq)]