2个版本
0.1.1 | 2023年3月5日 |
---|---|
0.1.0 | 2023年3月3日 |
#199 in 金融
14KB
341 行
Money_conversion
从低子单位到高单位及其反向转换货币
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);
依赖
~0.5–1.1MB
~25K SLoC