diff --git a/src/blockdata/locktime.rs b/src/blockdata/locktime.rs index 84427f88..ebdae544 100644 --- a/src/blockdata/locktime.rs +++ b/src/blockdata/locktime.rs @@ -28,6 +28,7 @@ use crate::consensus::encode::{self, Decodable, Encodable}; use crate::io::{self, Read, Write}; use crate::prelude::*; use crate::internal_macros::write_err; +use crate::impl_parse_str_through_int; /// The Threshold for deciding whether a lock time value is a height or a time (see [Bitcoin Core]). /// @@ -135,38 +136,7 @@ impl From for u32 { } } -/// Implements `TryFrom<$from> for $to` using `parse::int`, mapping the output using `fn` -macro_rules! impl_tryfrom_str_single { - ($($from:ty, $to:ident $(, $fn:ident)?);*) => { - $( - impl TryFrom<$from> for $to { - type Error = ParseIntError; - - fn try_from(s: $from) -> Result { - parse::int(s).map($to $(:: $fn)?) - } - } - )* - } -} - -/// Implements `TryFrom<{&str, String, Box}> for $to` using `parse::int`, mapping the output using `fn` -macro_rules! impl_tryfrom_str { - ($to:ident $(, $fn:ident)?) => { - impl_tryfrom_str_single!(&str, $to $(, $fn)?; String, $to $(, $fn)?; Box, $to $(, $fn)?); - - impl FromStr for $to { - type Err = ParseIntError; - - fn from_str(s: &str) -> Result { - parse::int(s).map($to $(:: $fn)?) - } - } - - } -} - -impl_tryfrom_str!(PackedLockTime); +impl_parse_str_through_int!(PackedLockTime); impl fmt::LowerHex for PackedLockTime { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -376,7 +346,7 @@ impl LockTime { } } -impl_tryfrom_str!(LockTime, from_consensus); +impl_parse_str_through_int!(LockTime, from_consensus); impl From for LockTime { fn from(h: Height) -> Self { diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index e4affe71..5ffa542d 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -32,6 +32,7 @@ use crate::hash_types::{Sighash, Txid, Wtxid}; use crate::VarInt; use crate::util::sighash::UINT256_ONE; use crate::internal_macros::{impl_consensus_encoding, serde_string_impl, serde_struct_human_string_impl, write_err}; +use crate::impl_parse_str_through_int; #[cfg(doc)] use crate::util::sighash::SchnorrSighashType; @@ -422,6 +423,8 @@ impl fmt::Display for RelativeLockTimeError { } } +impl_parse_str_through_int!(Sequence); + #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl std::error::Error for RelativeLockTimeError { diff --git a/src/parse.rs b/src/parse.rs index 9f17880a..54f82415 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -86,3 +86,38 @@ pub(crate) fn int + Into>(s: S) -> Result for $to` using `parse::int`, mapping the output using `fn` +#[macro_export] +macro_rules! impl_tryfrom_str_through_int_single { + ($($from:ty, $to:ident $(, $fn:ident)?);*) => { + $( + impl core::convert::TryFrom<$from> for $to { + type Error = $crate::error::ParseIntError; + + fn try_from(s: $from) -> Result { + $crate::parse::int(s).map($to $(:: $fn)?) + } + } + )* + } +} + +/// Implements `FromStr` and `TryFrom<{&str, String, Box}> for $to` using `parse::int`, mapping the output using `fn` +/// +/// The `Error` type is `ParseIntError` +#[macro_export] +macro_rules! impl_parse_str_through_int { + ($to:ident $(, $fn:ident)?) => { + $crate::impl_tryfrom_str_through_int_single!(&str, $to $(, $fn)?; String, $to $(, $fn)?; Box, $to $(, $fn)?); + + impl core::str::FromStr for $to { + type Err = $crate::error::ParseIntError; + + fn from_str(s: &str) -> Result { + $crate::parse::int(s).map($to $(:: $fn)?) + } + } + + } +}