From c16befe55cc9496c90d0f23f7742591f3fbad5d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Wed, 31 May 2023 22:29:58 +0300 Subject: [PATCH] Add support for US tablespoons --- src/conversions.rs | 6 ++++++ src/lib.rs | 2 ++ src/parse.rs | 30 ++++++++++++++++++++++++++++++ src/units.rs | 1 + 4 files changed, 39 insertions(+) diff --git a/src/conversions.rs b/src/conversions.rs index cf54f3c..1ca7d4e 100644 --- a/src/conversions.rs +++ b/src/conversions.rs @@ -123,6 +123,11 @@ fn get_conversion(unit: NonMetric) -> Conversion { from: imperial_gallon_from, to: MetricQuantity { amount: imperial_gallon_to, unit: Metric::Litre }, }, + NonMetric::USTablespoon => Conversion { + offset: 0.0, + from: 2.0 * 16.0 * 2.0 * 4.0 * us_gallon_from, + to: MetricQuantity { amount: us_gallon_to, unit: Metric::Litre }, + }, NonMetric::USFluidOunce => Conversion { offset: 0.0, from: 16.0 * 2.0 * 4.0 * us_gallon_from, @@ -223,6 +228,7 @@ mod test { Test(NonMetric::ImperialPint, 0.56826125), Test(NonMetric::ImperialQuart, 1.1365225), Test(NonMetric::ImperialGallon, 4.54609), + Test(NonMetric::USTablespoon, 0.01478676478125), Test(NonMetric::USFluidOunce, 0.0295735295625), Test(NonMetric::USCup, 0.2365882365), Test(NonMetric::USLiquidPint, 0.473176473), diff --git a/src/lib.rs b/src/lib.rs index 6c01b17..7ee7a40 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,6 +89,7 @@ fn unit_to_name(unit: NonMetric) -> &'static str { NonMetric::ImperialPint => "imperial pints", NonMetric::ImperialQuart => "imperial quarts", NonMetric::ImperialGallon => "imperial gallons", + NonMetric::USTablespoon => "US tablespoons", NonMetric::USFluidOunce => "US fluid ounces", NonMetric::USCup => "US cups", NonMetric::USLiquidPint => "US liquid pints", @@ -140,6 +141,7 @@ mod test { assert_eq!(run("1 imp pt"), Ok("5.683 dl".to_string())); assert_eq!(run("1 imp qt"), Ok("1.137 l".to_string())); assert_eq!(run("1 imp gal"), Ok("4.546 l".to_string())); + assert_eq!(run("1 Tbsp"), Ok("1.479 cl".to_string())); assert_eq!(run("1 US fl oz"), Ok("2.957 cl".to_string())); assert_eq!(run("1 US cup"), Ok("2.366 dl".to_string())); assert_eq!(run("1 US pt"), Ok("4.732 dl".to_string())); diff --git a/src/parse.rs b/src/parse.rs index b455bcd..142bcbe 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -205,6 +205,21 @@ fn parse_unit(input: String) -> Result { "imperial gallons" => Ok(NonMetric::ImperialGallon), "imp gal" => Ok(NonMetric::ImperialGallon), + "US tablespoon" => Ok(NonMetric::USTablespoon), + "US tablespoons" => Ok(NonMetric::USTablespoon), + "US Tbsp." => Ok(NonMetric::USTablespoon), + "US Tbsp" => Ok(NonMetric::USTablespoon), + "us tablespoon" => Ok(NonMetric::USTablespoon), + "us tablespoons" => Ok(NonMetric::USTablespoon), + "us tbsp." => Ok(NonMetric::USTablespoon), + "us tbsp" => Ok(NonMetric::USTablespoon), + "tablespoon" => Ok(NonMetric::USTablespoon), + "tablespoons" => Ok(NonMetric::USTablespoon), + "Tbsp." => Ok(NonMetric::USTablespoon), + "Tbsp" => Ok(NonMetric::USTablespoon), + "tbsp." => Ok(NonMetric::USTablespoon), + "tbsp" => Ok(NonMetric::USTablespoon), + "US fluid ounce" => Ok(NonMetric::USFluidOunce), "US fluid ounces" => Ok(NonMetric::USFluidOunce), "US fl oz" => Ok(NonMetric::USFluidOunce), @@ -506,6 +521,21 @@ 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("US tablespoon".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("US tablespoons".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("US Tbsp.".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("US Tbsp".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("us tablespoon".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("us tablespoons".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("us tbsp.".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("us tbsp".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("tablespoon".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("tablespoons".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("Tbsp.".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("Tbsp".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("tbsp.".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("tbsp".to_string()), Ok(NonMetric::USTablespoon)); + assert_eq!(parse_unit("US fluid ounce".to_string()), Ok(NonMetric::USFluidOunce)); assert_eq!(parse_unit("US fluid ounces".to_string()), Ok(NonMetric::USFluidOunce)); assert_eq!(parse_unit("US fl oz".to_string()), Ok(NonMetric::USFluidOunce)); diff --git a/src/units.rs b/src/units.rs index 680fe94..0952fd6 100644 --- a/src/units.rs +++ b/src/units.rs @@ -34,6 +34,7 @@ pub enum NonMetric { ImperialPint, ImperialQuart, ImperialGallon, + USTablespoon, USFluidOunce, USCup, USLiquidPint,