1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// SPDX-License-Identifier: CC0-1.0

//! Macros
//!
//! Macros meant to be used inside the Rust Miniscript library

/// Allows tests to create a miniscript directly from string as
/// `ms_str!("c:or_i(pk({}),pk({}))", pk1, pk2)`
#[cfg(test)]
macro_rules! ms_str {
    ($($arg:tt)*) => (Miniscript::from_str_ext(&format!($($arg)*), &$crate::ExtParams::allow_all()).unwrap())
}

/// Allows tests to create a concrete policy directly from string as
/// `policy_str!("wsh(c:or_i(pk({}),pk({})))", pk1, pk2)`
#[cfg(all(feature = "compiler", test))]
macro_rules! policy_str {
    ($($arg:tt)*) => ($crate::policy::Concrete::from_str(&format!($($arg)*)).unwrap())
}

/// 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,
        $(#[$meta:meta])*
        fn $fn:ident ( $($arg:ident : $type:ty),* ) -> $ret:ty
        $body:block
    ) => {
        impl<Pk $(, $gen)*> $crate::expression::FromTree for $name
        where
            Pk: MiniscriptKey + core::str::FromStr,
            Pk::Sha256: core::str::FromStr,
            Pk::Hash256: core::str::FromStr,
            Pk::Ripemd160: core::str::FromStr,
            Pk::Hash160: core::str::FromStr,
            <Pk as core::str::FromStr>::Err: $crate::prelude::ToString,
            <<Pk as MiniscriptKey>::Sha256 as core::str::FromStr>::Err: $crate::prelude::ToString,
            <<Pk as MiniscriptKey>::Hash256 as core::str::FromStr>::Err: $crate::prelude::ToString,
            <<Pk as MiniscriptKey>::Ripemd160 as core::str::FromStr>::Err: $crate::prelude::ToString,
            <<Pk as MiniscriptKey>::Hash160 as core::str::FromStr>::Err: $crate::prelude::ToString,
            $($gen : $gen_con,)*
            {

                $(#[$meta])*
                fn $fn($($arg: $type)* ) -> $ret {
                    $body
                }
            }
    };
}

/// Macro for implementing FromStr 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,
        type Err = $err_ty:ty;,
        $(#[$meta:meta])*
        fn $fn:ident ( $($arg:ident : $type:ty),* ) -> $ret:ty
        $body:block
    ) => {
        impl<Pk $(, $gen)*> core::str::FromStr for $name
        where
            Pk: MiniscriptKey + core::str::FromStr,
            Pk::Sha256: core::str::FromStr,
            Pk::Hash256: core::str::FromStr,
            Pk::Ripemd160: core::str::FromStr,
            Pk::Hash160: core::str::FromStr,
            <Pk as core::str::FromStr>::Err: $crate::prelude::ToString,
            <<Pk as MiniscriptKey>::Sha256 as core::str::FromStr>::Err: $crate::prelude::ToString,
            <<Pk as MiniscriptKey>::Hash256 as core::str::FromStr>::Err: $crate::prelude::ToString,
            <<Pk as MiniscriptKey>::Ripemd160 as core::str::FromStr>::Err: $crate::prelude::ToString,
            <<Pk as MiniscriptKey>::Hash160 as core::str::FromStr>::Err: $crate::prelude::ToString,
            $($gen : $gen_con,)*
            {
                type Err = $err_ty;

                $(#[$meta])*
                fn $fn($($arg: $type)* ) -> $ret {
                    $body
                }
            }
    };
}

/// Macro for impl Struct with associated bounds. This avoids copying all the Pk::Associated type bounds
/// throughout the codebase.
macro_rules! impl_block_str {
    ($(;$gen:ident; $gen_con:ident, )* $name: ty,
        $(#[$meta:meta])*
        $v:vis fn $fn:ident ( $($arg:ident : $type:ty, )* ) -> $ret:ty
        $body:block
    ) => {
        impl<Pk $(, $gen)*> $name
        where
            Pk: MiniscriptKey + core::str::FromStr,
            Pk::Sha256: core::str::FromStr,
            Pk::Hash256: core::str::FromStr,
            Pk::Ripemd160: core::str::FromStr,
            Pk::Hash160: core::str::FromStr,
            <Pk as core::str::FromStr>::Err: $crate::prelude::ToString,
            <<Pk as MiniscriptKey>::Sha256 as core::str::FromStr>::Err: $crate::prelude::ToString,
            <<Pk as MiniscriptKey>::Hash256 as core::str::FromStr>::Err: $crate::prelude::ToString,
            <<Pk as MiniscriptKey>::Ripemd160 as core::str::FromStr>::Err: $crate::prelude::ToString,
            <<Pk as MiniscriptKey>::Hash160 as core::str::FromStr>::Err: $crate::prelude::ToString,
            $($gen : $gen_con,)*
            {
                $(#[$meta])*
                $v fn $fn($($arg: $type,)* ) -> $ret {
                    $body
                }
            }
    };
}

/// A macro that implements serde serialization and deserialization using the
/// `fmt::Display` and `str::FromStr` traits.
macro_rules! serde_string_impl_pk {
    ($name:ident, $expecting:expr $(, $gen:ident; $gen_con:ident)*) => {
        #[cfg(feature = "serde")]
        impl<'de, Pk $(, $gen)*> $crate::serde::Deserialize<'de> for $name<Pk $(, $gen)*>
        where
            Pk: $crate::MiniscriptKey + core::str::FromStr,
            Pk::Sha256: core::str::FromStr,
            Pk::Hash256: core::str::FromStr,
            Pk::Ripemd160: core::str::FromStr,
            Pk::Hash160: core::str::FromStr,
            <Pk as core::str::FromStr>::Err: core::fmt::Display,
            <<Pk as $crate::MiniscriptKey>::Sha256 as core::str::FromStr>::Err:
                core::fmt::Display,
            <<Pk as $crate::MiniscriptKey>::Hash256 as core::str::FromStr>::Err:
                core::fmt::Display,
            <<Pk as $crate::MiniscriptKey>::Ripemd160 as core::str::FromStr>::Err:
                core::fmt::Display,
            <<Pk as $crate::MiniscriptKey>::Hash160 as core::str::FromStr>::Err:
                core::fmt::Display,
            $($gen : $gen_con,)*
        {
            fn deserialize<D>(deserializer: D) -> Result<$name<Pk $(, $gen)*>, D::Error>
            where
                D: $crate::serde::de::Deserializer<'de>,
            {
                use core::fmt::{self, Formatter};
                use core::marker::PhantomData;
                use core::str::FromStr;

                #[allow(unused_parens)]
                struct Visitor<Pk $(, $gen)*>(PhantomData<(Pk $(, $gen)*)>);
                impl<'de, Pk $(, $gen)*> $crate::serde::de::Visitor<'de> for Visitor<Pk $(, $gen)*>
                where
                    Pk: $crate::MiniscriptKey + core::str::FromStr,
                    Pk::Sha256: core::str::FromStr,
                    Pk::Hash256: core::str::FromStr,
                    Pk::Ripemd160: core::str::FromStr,
                    Pk::Hash160: core::str::FromStr,
                    <Pk as core::str::FromStr>::Err: core::fmt::Display,
                    <<Pk as $crate::MiniscriptKey>::Sha256 as core::str::FromStr>::Err:
                        core::fmt::Display,
                    <<Pk as $crate::MiniscriptKey>::Hash256 as core::str::FromStr>::Err:
                        core::fmt::Display,
                    <<Pk as $crate::MiniscriptKey>::Ripemd160 as core::str::FromStr>::Err:
                        core::fmt::Display,
                    <<Pk as $crate::MiniscriptKey>::Hash160 as core::str::FromStr>::Err:
                        core::fmt::Display,
                    $($gen: $gen_con,)*
                {
                    type Value = $name<Pk $(, $gen)*>;

                    fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
                        formatter.write_str($expecting)
                    }

                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
                    where
                        E: $crate::serde::de::Error,
                    {
                        $name::from_str(v).map_err(E::custom)
                    }

                    fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
                    where
                        E: $crate::serde::de::Error,
                    {
                        self.visit_str(v)
                    }

                    fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
                    where
                        E: $crate::serde::de::Error,
                    {
                        self.visit_str(&v)
                    }
                }

                deserializer.deserialize_str(Visitor(PhantomData))
            }
        }

        #[cfg(feature = "serde")]
        impl<'de, Pk $(, $gen)*> $crate::serde::Serialize for $name<Pk $(, $gen)*>
        where
            Pk: $crate::MiniscriptKey,
            $($gen: $gen_con,)*
        {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: $crate::serde::Serializer,
            {
                serializer.collect_str(&self)
            }
        }
    };
}