From 3a6b20077372d787cd968edd0a821fc998686afd Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Mon, 16 Sep 2024 15:38:41 +0530 Subject: [PATCH] feat: convert quantity to minor --- src/types/quantity.rs | 94 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/src/types/quantity.rs b/src/types/quantity.rs index ab238b6..1d373d9 100644 --- a/src/types/quantity.rs +++ b/src/types/quantity.rs @@ -6,7 +6,7 @@ use std::str::FromStr; use derive_builder::Builder; use derive_getters::Getters; -use derive_more::Display; +use derive_more::{Display, Error}; use serde::{Deserialize, Serialize}; #[derive( @@ -17,6 +17,9 @@ pub enum QuantityUnit { Kilogram, #[display(fmt = "{}", GRAM)] Gram, + #[display(fmt = "{}", MILLI_GRAM)] + Milligram, + #[default] #[display(fmt = "{}", DISCRETE_NUMBER)] DiscreteNumber, // example: 1 sofa, 2 bed, etc. @@ -28,6 +31,7 @@ pub enum QuantityUnit { const KILO_GRAM: &str = "kg"; const GRAM: &str = "g"; +const MILLI_GRAM: &str = "mg"; const DISCRETE_NUMBER: &str = "discrete_number"; const MILLI_LITER: &str = "ml"; const LITER: &str = "l"; @@ -41,6 +45,7 @@ impl FromStr for QuantityUnit { DISCRETE_NUMBER => Ok(Self::DiscreteNumber), MILLI_LITER => Ok(Self::MilliLiter), LITER => Ok(Self::Liter), + MILLI_GRAM => Ok(Self::Milligram), _ => Err("Currency unsupported".into()), } } @@ -58,6 +63,17 @@ impl QuantityPart { pub fn is_empty(&self) -> bool { self.number == 0 } + + pub fn as_minor(&self) -> QuantityResult { + match self.unit { + QuantityUnit::Kilogram | QuantityUnit::Liter | QuantityUnit::Gram => { + Ok(self.number * 1000) + } + QuantityUnit::Milligram | QuantityUnit::MilliLiter | QuantityUnit::DiscreteNumber => { + Err(QuantityError::QuantityCantBeDivided) + } + } + } } #[derive( @@ -67,11 +83,21 @@ pub struct Quantity { minor: QuantityPart, major: QuantityPart, } +pub type QuantityResult = Result; + +#[derive(Debug, Error, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] +pub enum QuantityError { + QuantityCantBeDivided, +} impl Quantity { pub fn is_empty(&self) -> bool { self.minor.is_empty() == true && self.major.is_empty() == true } + + pub fn major_as_minor(&self) -> QuantityResult { + self.major.as_minor() + } } #[cfg(test)] @@ -180,6 +206,11 @@ mod tests { assert!(test_helper(QuantityUnit::Liter, LITER)); } + #[test] + fn quantity_unit_milli_gram() { + assert!(test_helper(QuantityUnit::Milligram, MILLI_GRAM)); + } + impl Quantity { pub fn get_quantity() -> Self { let q = QuantityPartBuilder::default() @@ -195,4 +226,65 @@ mod tests { .unwrap() } } + + #[test] + fn test_quantity_as_minor() { + assert_eq!( + QuantityPart { + unit: QuantityUnit::Kilogram, + number: 1 + } + .as_minor() + .unwrap(), + 1000 + ); + + assert_eq!( + QuantityPart { + unit: QuantityUnit::Gram, + number: 1 + } + .as_minor() + .unwrap(), + 1000 + ); + + assert_eq!( + QuantityPart { + unit: QuantityUnit::Liter, + number: 1 + } + .as_minor() + .unwrap(), + 1000 + ); + + // error cases + assert_eq!( + QuantityPart { + unit: QuantityUnit::Milligram, + number: 1 + } + .as_minor(), + Err(QuantityError::QuantityCantBeDivided) + ); + assert_eq!( + QuantityPart { + unit: QuantityUnit::DiscreteNumber, + number: 1 + } + .as_minor(), + Err(QuantityError::QuantityCantBeDivided) + ); + assert_eq!( + QuantityPart { + unit: QuantityUnit::Milligram, + number: 1 + } + .as_minor(), + Err(QuantityError::QuantityCantBeDivided) + ); + + + } }