Skip to content

Commit

Permalink
Use parse::<Txid> instead of encode::deserialize_hex
Browse files Browse the repository at this point in the history
Currently, in some places, we use the bespoke `encode::deserialize_hex`
function to deserialize txid from a hex string. The `Txid` type provides a
`FromStr` impl so we can use `parse::<Txid>()`. This makes the code
more easily readable because its a common pattern in Rust.
  • Loading branch information
fedemagnani committed Dec 3, 2024
1 parent dada062 commit 6a465b4
Showing 1 changed file with 9 additions and 25 deletions.
34 changes: 9 additions & 25 deletions types/src/v17/blockchain/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl GetBlockVerbosityOne {
let tx = self
.tx
.iter()
.map(|t| encode::deserialize_hex::<Txid>(t).map_err(E::Tx))
.map(|t| t.parse::<Txid>().map_err(E::Hash))
.collect::<Result<Vec<_>, _>>()?;
let median_time = self.median_time.map(|t| crate::to_u32(t, "median_time")).transpose()?;
let bits = CompactTarget::from_unprefixed_hex(&self.bits).map_err(E::Bits)?;
Expand Down Expand Up @@ -328,12 +328,8 @@ impl GetDifficulty {

impl GetMempoolAncestors {
/// Converts version specific type to a version nonspecific, more strongly typed type.
pub fn into_model(self) -> Result<model::GetMempoolAncestors, encode::FromHexError> {
let v = self
.0
.iter()
.map(|t| encode::deserialize_hex::<Txid>(t))
.collect::<Result<Vec<_>, _>>()?;
pub fn into_model(self) -> Result<model::GetMempoolAncestors, hex::HexToArrayError> {
let v = self.0.iter().map(|t| t.parse::<Txid>()).collect::<Result<Vec<_>, _>>()?;
Ok(model::GetMempoolAncestors(v))
}
}
Expand All @@ -355,12 +351,8 @@ impl GetMempoolAncestorsVerbose {

impl GetMempoolDescendants {
/// Converts version specific type to a version nonspecific, more strongly typed type.
pub fn into_model(self) -> Result<model::GetMempoolDescendants, encode::FromHexError> {
let v = self
.0
.iter()
.map(|t| encode::deserialize_hex::<Txid>(t))
.collect::<Result<Vec<_>, _>>()?;
pub fn into_model(self) -> Result<model::GetMempoolDescendants, hex::HexToArrayError> {
let v = self.0.iter().map(|t| t.parse::<Txid>()).collect::<Result<Vec<_>, _>>()?;
Ok(model::GetMempoolDescendants(v))
}
}
Expand Down Expand Up @@ -460,12 +452,8 @@ impl GetMempoolInfo {

impl GetRawMempool {
/// Converts version specific type to a version nonspecific, more strongly typed type.
pub fn into_model(self) -> Result<model::GetRawMempool, encode::FromHexError> {
let v = self
.0
.iter()
.map(|t| encode::deserialize_hex::<Txid>(t))
.collect::<Result<Vec<_>, _>>()?;
pub fn into_model(self) -> Result<model::GetRawMempool, hex::HexToArrayError> {
let v = self.0.iter().map(|t| t.parse::<Txid>()).collect::<Result<Vec<_>, _>>()?;
Ok(model::GetRawMempool(v))
}
}
Expand Down Expand Up @@ -551,12 +539,8 @@ impl GetTxOutSetInfo {

impl VerifyTxOutProof {
/// Converts version specific type to a version nonspecific, more strongly typed type.
pub fn into_model(self) -> Result<model::VerifyTxOutProof, encode::FromHexError> {
let proofs = self
.0
.iter()
.map(|t| encode::deserialize_hex::<Txid>(t))
.collect::<Result<Vec<_>, _>>()?;
pub fn into_model(self) -> Result<model::VerifyTxOutProof, hex::HexToArrayError> {
let proofs = self.0.iter().map(|t| t.parse::<Txid>()).collect::<Result<Vec<_>, _>>()?;
Ok(model::VerifyTxOutProof(proofs))
}
}

0 comments on commit 6a465b4

Please sign in to comment.