clvm_traits/
macros.rs

1/// Converts a list of CLVM values into a series of nested pairs.
2#[macro_export]
3macro_rules! clvm_list {
4    () => {
5        ()
6    };
7    ( $first:expr $( , $rest:expr )* $(,)? ) => {
8        ($first, $crate::clvm_list!( $( $rest ),* ))
9    };
10}
11
12/// Converts a tuple of CLVM values into a series of nested pairs.
13#[macro_export]
14macro_rules! clvm_tuple {
15    () => {
16        ()
17    };
18    ( $first:expr $(,)? ) => {
19        $first
20    };
21    ( $first:expr $( , $rest:expr )* $(,)? ) => {
22        ($first, $crate::clvm_tuple!( $( $rest ),* ))
23    };
24}
25
26/// Quotes a CLVM value.
27#[macro_export]
28macro_rules! clvm_quote {
29    ( $value:expr ) => {
30        (1, $value)
31    };
32}
33
34/// Constructs a sequence of nested pairs that represents a set of curried arguments.
35#[macro_export]
36macro_rules! clvm_curried_args {
37    () => {
38        1
39    };
40    ( $first:expr $( , $rest:expr )* $(,)? ) => {
41        (4, ($crate::clvm_quote!($first), ($crate::clvm_curried_args!( $( $rest ),* ), ())))
42    };
43}
44
45/// Creates the type needed to represent a list of CLVM types.
46#[macro_export]
47macro_rules! match_list {
48    () => {
49        ()
50    };
51    ( $first:ty $( , $rest:ty )* $(,)? ) => {
52        ($first, $crate::match_list!( $( $rest ),* ))
53    };
54}
55
56/// Creates the type needed to represent a tuple of CLVM types.
57#[macro_export]
58macro_rules! match_tuple {
59    () => {
60        ()
61    };
62    ( $first:ty $(,)? ) => {
63        $first
64    };
65    ( $first:ty $( , $rest:ty )* $(,)? ) => {
66        ($first, $crate::match_tuple!( $( $rest ),* ))
67    };
68}
69
70/// Creates the type needed to represent a quoted CLVM type.
71#[macro_export]
72macro_rules! match_quote {
73    ( $type:ty ) => {
74        ($crate::MatchByte::<1>, $type)
75    };
76}
77
78/// Creates the type needed to represent a set of curried arguments.
79#[macro_export]
80macro_rules! match_curried_args {
81    () => {
82        $crate::MatchByte::<1>
83    };
84    ( $first:ty $( , $rest:ty )* $(,)? ) => {
85        (
86            $crate::MatchByte::<4>,
87            (
88                $crate::match_quote!($first),
89                ($crate::match_curried_args!( $( $rest ),* ), ()),
90            ),
91        )
92    };
93}
94
95/// Deconstructs a CLVM list that has been matched.
96#[macro_export]
97macro_rules! destructure_list {
98    () => {
99        _
100    };
101    ( $first:pat $( , $rest:pat )* $(,)? ) => {
102        ($first, $crate::destructure_list!( $( $rest ),* ))
103    };
104}
105
106/// Deconstructs a CLVM tuple that has been matched.
107#[macro_export]
108macro_rules! destructure_tuple {
109    () => {
110        _
111    };
112    ( $first:pat $(,)? ) => {
113        $first
114    };
115    ( $first:pat $( , $rest:pat )* $(,)? ) => {
116        ($first, $crate::destructure_tuple!( $( $rest ),* ))
117    };
118}
119
120/// Deconstructs a quoted CLVM value that has been matched.
121#[macro_export]
122macro_rules! destructure_quote {
123    ( $name:pat ) => {
124        (_, $name)
125    };
126}
127
128/// Deconstructs a set of curried arguments that has been matched.
129#[macro_export]
130macro_rules! destructure_curried_args {
131    () => {
132        _
133    };
134    ( $first:pat $( , $rest:pat )* $(,)? ) => {
135        (
136            _,
137            (
138                $crate::destructure_quote!($first),
139                ($crate::destructure_curried_args!( $( $rest ),* ), ()),
140            ),
141        )
142    };
143}