From 6a465b4e881cdab009a447a55d57142d8ed6f0a7 Mon Sep 17 00:00:00 2001 From: fedemagnani <0xdrun@gmail.com> Date: Mon, 2 Dec 2024 18:38:07 +0100 Subject: [PATCH] Use parse:: instead of encode::deserialize_hex 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::()`. This makes the code more easily readable because its a common pattern in Rust. --- types/src/v17/blockchain/into.rs | 34 +++++++++----------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/types/src/v17/blockchain/into.rs b/types/src/v17/blockchain/into.rs index e81b429..c2638b5 100644 --- a/types/src/v17/blockchain/into.rs +++ b/types/src/v17/blockchain/into.rs @@ -41,7 +41,7 @@ impl GetBlockVerbosityOne { let tx = self .tx .iter() - .map(|t| encode::deserialize_hex::(t).map_err(E::Tx)) + .map(|t| t.parse::().map_err(E::Hash)) .collect::, _>>()?; 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)?; @@ -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 { - let v = self - .0 - .iter() - .map(|t| encode::deserialize_hex::(t)) - .collect::, _>>()?; + pub fn into_model(self) -> Result { + let v = self.0.iter().map(|t| t.parse::()).collect::, _>>()?; Ok(model::GetMempoolAncestors(v)) } } @@ -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 { - let v = self - .0 - .iter() - .map(|t| encode::deserialize_hex::(t)) - .collect::, _>>()?; + pub fn into_model(self) -> Result { + let v = self.0.iter().map(|t| t.parse::()).collect::, _>>()?; Ok(model::GetMempoolDescendants(v)) } } @@ -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 { - let v = self - .0 - .iter() - .map(|t| encode::deserialize_hex::(t)) - .collect::, _>>()?; + pub fn into_model(self) -> Result { + let v = self.0.iter().map(|t| t.parse::()).collect::, _>>()?; Ok(model::GetRawMempool(v)) } } @@ -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 { - let proofs = self - .0 - .iter() - .map(|t| encode::deserialize_hex::(t)) - .collect::, _>>()?; + pub fn into_model(self) -> Result { + let proofs = self.0.iter().map(|t| t.parse::()).collect::, _>>()?; Ok(model::VerifyTxOutProof(proofs)) } }