amount_conversion

Crates.ioamount_conversion
lib.rsamount_conversion
version0.1.1
sourcesrc
created_at2023-03-03 11:43:02.957307
updated_at2023-03-05 12:17:23.874479
descriptionConvert Amount conversion from lower denomination to higher denomination and vice-versa
homepagehttps://github.com/jarnura/amount_conversion
repository
max_upload_size
id799686
size26,446
Arun Raj M (jarnura)

documentation

https://docs.rs/amount_conversion

README

Money_conversion

Money conversion from lower subunit to higher unit and vice-versa


use amount_conversion::factor::{Currency::{self,*}, FromCurrency};
   
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, serde::Deserialize, serde::Serialize)]
enum UserCurrency {
    Inr,
    Usd,
}

impl FromCurrency for UserCurrency {
fn currency(&self) -> Currency {
   match self {
       UserCurrency::Inr => INR,
       UserCurrency::Usd => USD,
   }
}   

type Money = MoneyInner<LowestSubunit, Currency>;
type MoneyH = MoneyInner<HighestUnit, Currency>;

#[derive(serde::Deserialize)]
struct Request {
    #[serde(flatten)]
    amount: Money,
    id: i8,
}

let amount_str = r#"{
    "amount": 1,
    "currency": "Inr",
    "id": 1
}"#;

let request = serde_json::from_str::<Request>(amount_str)?;

let highest_unit: MoneyH = request.amount.convert()?;
let lowest_unit: Money = highest_unit.convert()?;
assert_eq!(request.amount, lowest_unit);
Commit count: 0

cargo fmt