blockfrost-rust
blockfrost-rust copied to clipboard
Deserialize and serialize quantities as integers
Some JSON bodies pass integers as strings.
{
"unit": "lovelace",
"quantity": "500000"
}
Here's the current struct for this (simplified):
#[derive(Deserialize)]
pub struct Amount {
pub unit: String,
pub quantity: String,
}
It must be changed to:
#[derive(Deserialize)]
pub struct Amount {
pub unit: String,
#[serde_hacks]
pub quantity: Integer,
}
Where Integer is our type alias for i128 and serde_hacks is the custom deserialization for that specific field.
Some options to customize this:
- https://docs.rs/serde_with/1.10.0/serde_with/#displayfromstr
- https://serde.rs/variant-attrs.html# (+ https://docs.rs/serde/1.0.130/serde/de/trait.Error.html)
Could be convenient to catch errors in the deserialization step and the users of the crate won't have to manually convert each field while trying to use them.
However, the conversion to integer needs to be treated.