diff --git a/src/types/mod.rs b/src/types/mod.rs index ea1860a..24b59cd 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -1 +1,5 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + pub mod quantity; diff --git a/src/types/quantity.rs b/src/types/quantity.rs index 590efca..ab238b6 100644 --- a/src/types/quantity.rs +++ b/src/types/quantity.rs @@ -54,6 +54,12 @@ pub struct QuantityPart { unit: QuantityUnit, } +impl QuantityPart { + pub fn is_empty(&self) -> bool { + self.number == 0 + } +} + #[derive( Clone, Default, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Builder, Getters, )] @@ -62,10 +68,78 @@ pub struct Quantity { major: QuantityPart, } +impl Quantity { + pub fn is_empty(&self) -> bool { + self.minor.is_empty() == true && self.major.is_empty() == true + } +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn quantity_part_empty() { + let q = QuantityPartBuilder::default() + .number(0) + .unit(QuantityUnit::DiscreteNumber) + .build() + .unwrap(); + assert!(q.is_empty()); + let q_not_empty = QuantityPartBuilder::default() + .number(1) + .unit(QuantityUnit::DiscreteNumber) + .build() + .unwrap(); + assert!(!q_not_empty.is_empty()); + } + + #[test] + fn quantity_empty() { + let q = QuantityPartBuilder::default() + .number(0) + .unit(QuantityUnit::DiscreteNumber) + .build() + .unwrap(); + let q_not_empty = QuantityPartBuilder::default() + .number(1) + .unit(QuantityUnit::DiscreteNumber) + .build() + .unwrap(); + + // not empty; major has items + assert!(!QuantityBuilder::default() + .minor(q.clone()) + .major(q_not_empty.clone()) + .build() + .unwrap() + .is_empty()); + + // not empty; minor has items + assert!(!QuantityBuilder::default() + .minor(q.clone()) + .major(q_not_empty.clone()) + .build() + .unwrap() + .is_empty()); + + // not empty; minor & major have items + assert!(!QuantityBuilder::default() + .minor(q_not_empty.clone()) + .major(q_not_empty.clone()) + .build() + .unwrap() + .is_empty()); + + // empty + assert!(QuantityBuilder::default() + .minor(q.clone()) + .major(q) + .build() + .unwrap() + .is_empty()); + } + fn test_helper(t: T, str_value: &str) -> bool where T: ToString + FromStr + std::fmt::Debug + PartialEq, @@ -105,4 +179,20 @@ mod tests { fn quantity_unit_liter() { assert!(test_helper(QuantityUnit::Liter, LITER)); } + + impl Quantity { + pub fn get_quantity() -> Self { + let q = QuantityPartBuilder::default() + .number(1) + .unit(QuantityUnit::DiscreteNumber) + .build() + .unwrap(); + + QuantityBuilder::default() + .minor(q.clone()) + .major(q.clone()) + .build() + .unwrap() + } + } }