diff --git a/src/core/pay.rs b/src/core/pay.rs index ec45549..b1ec841 100644 --- a/src/core/pay.rs +++ b/src/core/pay.rs @@ -15,7 +15,13 @@ pub struct Currency { pub symbol: String, pub decimals: u8, pub multiplier: f64, - pub convertible: bool, + pub convertible: Option, +} + +#[derive(Clone, Debug)] +pub struct CurrencyConvertible { + pub min: u64, + pub max: u64, } #[derive(Clone, Debug)] @@ -66,8 +72,14 @@ mod serde { pub symbol: &'a str, pub decimals: u8, pub multiplier: f64, - #[serde(default)] - pub convertible: bool, + #[serde(skip_serializing_if = "Option::is_none")] + pub convertible: Option, + } + + #[derive(Deserialize, Serialize)] + pub struct CurrencyConvertible { + pub min: u64, + pub max: u64, } #[derive(Deserialize, Serialize)] diff --git a/src/core/pay/client.rs b/src/core/pay/client.rs index 5802d14..0beba56 100644 --- a/src/core/pay/client.rs +++ b/src/core/pay/client.rs @@ -33,7 +33,10 @@ impl TryFrom<&[u8]> for Entrypoint { symbol: String::from(c.symbol), decimals: c.decimals, multiplier: c.multiplier, - convertible: c.convertible, + convertible: c.convertible.map(|c| super::CurrencyConvertible { + min: c.min, + max: c.max, + }), }) .collect() }); @@ -404,7 +407,10 @@ mod tests { "symbol": "R$", "multiplier": 314.15, "decimals": 2, - "convertible": true + "convertible": { + "min": 100, + "max": 999 + } }, { "code": "USD", @@ -424,14 +430,17 @@ mod tests { assert_eq!(currencies[0].symbol, "R$"); assert_eq!(currencies[0].decimals, 2); assert!((currencies[0].multiplier - 314.15).abs() < f64::EPSILON); - assert!(currencies[0].convertible); + + let convertible = currencies[0].convertible.as_ref().unwrap(); + assert_eq!(convertible.min, 100); + assert_eq!(convertible.max, 999); assert_eq!(currencies[1].code, "USD"); assert_eq!(currencies[1].name, "Dólar"); assert_eq!(currencies[1].symbol, "$"); assert_eq!(currencies[1].decimals, 6); assert!((currencies[1].multiplier - 14.5).abs() < f64::EPSILON); - assert!(!currencies[1].convertible); + assert!(currencies[1].convertible.is_none()); } #[test] diff --git a/src/core/pay/server.rs b/src/core/pay/server.rs index dc4d976..753fd7d 100644 --- a/src/core/pay/server.rs +++ b/src/core/pay/server.rs @@ -58,7 +58,12 @@ impl TryFrom for Vec { symbol: &c.symbol, decimals: c.decimals, multiplier: c.multiplier, - convertible: c.convertible, + convertible: c.convertible.as_ref().map(|c| { + super::serde::CurrencyConvertible { + min: c.min, + max: c.max, + } + }), }) .collect() }), @@ -395,7 +400,7 @@ mod tests { symbol: String::from("R$"), decimals: 2, multiplier: 314.15, - convertible: true, + convertible: Some(super::super::CurrencyConvertible { min: 100, max: 999 }), }, super::super::Currency { code: String::from("USD"), @@ -403,7 +408,7 @@ mod tests { symbol: String::from("$"), decimals: 6, multiplier: 123.321, - convertible: false, + convertible: None, }, ]), payer: None, @@ -411,7 +416,7 @@ mod tests { assert_eq!( Vec::::try_from(query).unwrap(), - br#"{"tag":"payRequest","metadata":"[[\"text/plain\",\"boneco do steve magal\"]]","callback":"https://yuri/?o=callback","minSendable":314,"maxSendable":315,"commentAllowed":0,"currencies":[{"code":"BRL","name":"Reais","symbol":"R$","decimals":2,"multiplier":314.15,"convertible":true},{"code":"USD","name":"Dolar","symbol":"$","decimals":6,"multiplier":123.321,"convertible":false}]}"# + br#"{"tag":"payRequest","metadata":"[[\"text/plain\",\"boneco do steve magal\"]]","callback":"https://yuri/?o=callback","minSendable":314,"maxSendable":315,"commentAllowed":0,"currencies":[{"code":"BRL","name":"Reais","symbol":"R$","decimals":2,"multiplier":314.15,"convertible":{"min":100,"max":999}},{"code":"USD","name":"Dolar","symbol":"$","decimals":6,"multiplier":123.321}]}"# ); }