diff --git a/src/utils/currency.rs b/src/utils/currency.rs new file mode 100644 index 0000000..12bed8b --- /dev/null +++ b/src/utils/currency.rs @@ -0,0 +1,72 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use std::str::FromStr; + +use async_trait::async_trait; +use cqrs_es::Aggregate; +use derive_builder::Builder; +use derive_getters::Getters; +use derive_more::Display; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +#[derive(Clone, Display, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] +pub enum Currency { + #[display(fmt = "{}", INR)] + INR, +} + +const INR: &str = "INR"; + +impl FromStr for Currency { + type Err = String; + fn from_str(s: &str) -> Result { + let s = s.trim(); + match s { + INR => Ok(Self::INR), + _ => Err("Currency unsupported".into()), + } + } +} + +impl Default for Currency { + fn default() -> Self { + Self::INR + } +} + +#[derive( + Clone, Default, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder, +)] +pub struct Price { + major: usize, + minor: usize, + currency: Currency, +} + +#[cfg(test)] +mod tests { + use super::*; + + fn test_helper(t: T, str_value: &str) -> bool + where + T: ToString + FromStr + std::fmt::Debug + PartialEq, + ::Err: std::fmt::Debug, + { + println!("Testing type: {:?} against value {str_value}", t); + assert_eq!(t.to_string(), str_value.to_string()); + + assert_eq!(T::from_str(str_value).unwrap(), t); + + assert_eq!(T::from_str(t.to_string().as_str()).unwrap(), t,); + + true + } + + #[test] + fn currency_to_string_from_str() { + assert!(test_helper(Currency::INR, INR)); + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 9bcf0b0..499bf66 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later +pub mod currency; pub mod parse_aggregate_id; pub mod random_string; pub mod string;