Crates.io | amount_conversion |
lib.rs | amount_conversion |
version | 0.1.1 |
source | src |
created_at | 2023-03-03 11:43:02.957307 |
updated_at | 2023-03-05 12:17:23.874479 |
description | Convert Amount conversion from lower denomination to higher denomination and vice-versa |
homepage | https://github.com/jarnura/amount_conversion |
repository | |
max_upload_size | |
id | 799686 |
size | 26,446 |
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);