From d8c397da78eedc8cdf52ac586dc9205e2324d815 Mon Sep 17 00:00:00 2001 From: sanket1729 Date: Sun, 5 Jun 2022 22:41:43 -0700 Subject: [PATCH] Fix some macro bugs for 1.41.1 --- src/descriptor/mod.rs | 2 +- src/descriptor/tr.rs | 3 ++- src/macros.rs | 37 +++++++++++++++++++++++++++++++------ src/miniscript/mod.rs | 2 +- src/policy/concrete.rs | 8 ++++---- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/descriptor/mod.rs b/src/descriptor/mod.rs index 5f7028176..c7a85b3cb 100644 --- a/src/descriptor/mod.rs +++ b/src/descriptor/mod.rs @@ -653,7 +653,7 @@ impl Descriptor { impl_from_tree!( Descriptor, - /// Parse an expression tree into a descriptor + /// Parse an expression tree into a descriptor. fn from_tree(top: &expression::Tree) -> Result, Error> { Ok(match (top.name, top.args.len() as u32) { ("pkh", 1) => Descriptor::Pkh(Pkh::from_tree(top)?), diff --git a/src/descriptor/tr.rs b/src/descriptor/tr.rs index ee747182c..b7edad6d0 100644 --- a/src/descriptor/tr.rs +++ b/src/descriptor/tr.rs @@ -388,10 +388,11 @@ where } } +#[rustfmt::skip] impl_block_str!( Tr, // Helper function to parse taproot script path - fn parse_tr_script_spend(tree: &expression::Tree) -> Result, Error> { + fn parse_tr_script_spend(tree: &expression::Tree,) -> Result, Error> { match tree { expression::Tree { name, args } if !name.is_empty() && args.is_empty() => { let script = Miniscript::::from_str(name)?; diff --git a/src/macros.rs b/src/macros.rs index 77057806a..e90bffb72 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -19,7 +19,11 @@ macro_rules! policy_str { /// Macro for implementing FromTree trait. This avoids copying all the Pk::Associated type bounds /// throughout the codebase. macro_rules! impl_from_tree { - ($(;$gen:ident; $gen_con:ident, )* $name: ty, $fn: item) => { + ($(;$gen:ident; $gen_con:ident, )* $name: ty, + $(#[$meta:meta])* + fn $fn:ident ( $($arg:ident : $type:ty),* ) -> $ret:ty + $body:block + ) => { impl $crate::expression::FromTree for $name where Pk: MiniscriptKey + core::str::FromStr, @@ -28,7 +32,11 @@ macro_rules! impl_from_tree { <::Hash as core::str::FromStr>::Err: $crate::prelude::ToString, $($gen : $gen_con,)* { - $fn + + $(#[$meta])* + fn $fn($($arg: $type)* ) -> $ret { + $body + } } }; } @@ -36,7 +44,12 @@ macro_rules! impl_from_tree { /// Macro for implementing FromTree trait. This avoids copying all the Pk::Associated type bounds /// throughout the codebase. macro_rules! impl_from_str { - ($(;$gen:ident; $gen_con:ident, )* $name: ty $(, $it: item)*) => { + ($(;$gen:ident; $gen_con:ident, )* $name: ty, + type Err = $err_ty:ty;, + $(#[$meta:meta])* + fn $fn:ident ( $($arg:ident : $type:ty),* ) -> $ret:ty + $body:block + ) => { impl core::str::FromStr for $name where Pk: MiniscriptKey + core::str::FromStr, @@ -45,7 +58,12 @@ macro_rules! impl_from_str { <::Hash as core::str::FromStr>::Err: $crate::prelude::ToString, $($gen : $gen_con,)* { - $($it)* + type Err = $err_ty; + + $(#[$meta])* + fn $fn($($arg: $type)* ) -> $ret { + $body + } } }; } @@ -53,7 +71,11 @@ macro_rules! impl_from_str { /// Macro for implementing FromTree trait. This avoids copying all the Pk::Associated type bounds /// throughout the codebase. macro_rules! impl_block_str { - ($(;$gen:ident; $gen_con:ident, )* $name: ty $(, $it: item)*) => { + ($(;$gen:ident; $gen_con:ident, )* $name: ty, + $(#[$meta:meta])* + $v:vis fn $fn:ident ( $($arg:ident : $type:ty, )* ) -> $ret:ty + $body:block + ) => { impl $name where Pk: MiniscriptKey + std::str::FromStr, @@ -62,7 +84,10 @@ macro_rules! impl_block_str { <::Hash as std::str::FromStr>::Err: std::string::ToString, $($gen : $gen_con,)* { - $($it)* + $(#[$meta])* + $v fn $fn($($arg: $type,)* ) -> $ret { + $body + } } }; } diff --git a/src/miniscript/mod.rs b/src/miniscript/mod.rs index 8b359e99f..2c07c2bab 100644 --- a/src/miniscript/mod.rs +++ b/src/miniscript/mod.rs @@ -350,7 +350,7 @@ impl_block_str!( /// Some of the analysis guarantees of miniscript are lost when dealing with /// insane scripts. In general, in a multi-party setting users should only /// accept sane scripts. - pub fn from_str_insane(s: &str) -> Result, Error> + pub fn from_str_insane(s: &str,) -> Result, Error> { // This checks for invalid ASCII chars let top = expression::Tree::from_str(s)?; diff --git a/src/policy/concrete.rs b/src/policy/concrete.rs index e7deb23c7..8691237f3 100644 --- a/src/policy/concrete.rs +++ b/src/policy/concrete.rs @@ -698,14 +698,14 @@ impl_from_str!( serde_string_impl_pk!(Policy, "a miniscript concrete policy"); +#[rustfmt::skip] impl_block_str!( Policy, /// Helper function for `from_tree` to parse subexpressions with /// names of the form x@y - fn from_tree_prob( - top: &expression::Tree, - allow_prob: bool, - ) -> Result<(usize, Policy), Error> { + fn from_tree_prob(top: &expression::Tree, allow_prob: bool,) + -> Result<(usize, Policy), Error> + { let frag_prob; let frag_name; let mut name_split = top.name.split('@');