Skip to content

Commit

Permalink
update itf::value::BigInt deserialize for num_bigint::BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Nov 18, 2023
1 parent a56363c commit c03bec1
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions itf/src/value/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,31 @@ impl<'de> Deserialize<'de> for BigInt {
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
struct BigInt {
#[serde(rename = "#bigint")]
bigint: String,
#[serde(untagged)]
enum BigInt {
// deserialized serde_json::Value
Itf {
#[serde(rename = "#bigint")]
bigint: String,
},
// deserialized itf::Value
BigInt(i64, Vec<u32>),
}

let inner = BigInt::deserialize(deserializer)?;
let bigint = inner.bigint.parse().map_err(serde::de::Error::custom)?;
Ok(Self(bigint))
match BigInt::deserialize(deserializer)? {
BigInt::Itf { bigint } => {
let bigint: num_bigint::BigInt =
bigint.parse().map_err(serde::de::Error::custom)?;
Ok(Self::new(bigint))
}
BigInt::BigInt(sign, digits) => {
let sign = match sign.cmp(&0) {
std::cmp::Ordering::Less => num_bigint::Sign::Minus,
std::cmp::Ordering::Equal => num_bigint::Sign::NoSign,
std::cmp::Ordering::Greater => num_bigint::Sign::Plus,
};
Ok(Self::new(num_bigint::BigInt::new(sign, digits)))
}
}
}
}

0 comments on commit c03bec1

Please sign in to comment.