Skip to content

Commit

Permalink
This makes the input accepts both a string and a u32 whereas befo…
Browse files Browse the repository at this point in the history
…re only a `string` was accepted:

- "1000" (Absolute)
- "+1000" (Relative)
- 1000 (Absolute, newly added)
  • Loading branch information
Eligioo authored and jsdanielh committed Dec 23, 2024
1 parent 3dc9780 commit 4adc9e9
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions rpc-interface/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use nimiq_transaction::{
},
};
use nimiq_vrf::VrfSeed;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DeserializeFromStr, SerializeDisplay};
use serde::{de::Error, Deserialize, Serialize};
use serde_with::{serde_as, SerializeDisplay};

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -49,7 +49,7 @@ impl From<nimiq_transaction::Transaction> for HashOrTx {
}
}

#[derive(Copy, Clone, Debug, SerializeDisplay, DeserializeFromStr)]
#[derive(Copy, Clone, Debug, SerializeDisplay)]
pub enum ValidityStartHeight {
Absolute(u32),
Relative(u32),
Expand Down Expand Up @@ -79,6 +79,30 @@ impl Display for ValidityStartHeight {
}
}

impl<'de> Deserialize<'de> for ValidityStartHeight {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum StrOrU32 {
Number(u32),
String(String),
}

match StrOrU32::deserialize(deserializer)? {
StrOrU32::Number(height) => Ok(Self::Absolute(height)),
StrOrU32::String(height) => Self::from_str(&height).map_err(|e| {
Error::custom(format!(
"ValidityStartHeight cannot be converted into a `u32`: {}",
e
))
}),
}
}
}

impl FromStr for ValidityStartHeight {
type Err = <u32 as FromStr>::Err;

Expand Down

0 comments on commit 4adc9e9

Please sign in to comment.