chore: mv Currenct,Price to utlis

This commit is contained in:
Aravinth Manivannan 2024-09-14 15:56:16 +05:30
parent 90212fb53a
commit cb778aa89e
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 73 additions and 0 deletions

72
src/utils/currency.rs Normal file
View file

@ -0,0 +1,72 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// 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<Self, Self::Err> {
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: T, str_value: &str) -> bool
where
T: ToString + FromStr + std::fmt::Debug + PartialEq,
<T as FromStr>::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));
}
}

View file

@ -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;