1
1
use libc:: { c_char, size_t} ;
2
+ use macro_find_and_replace:: replace_token_sequence;
2
3
use std:: {
3
4
borrow:: Cow ,
4
5
ffi:: { CStr , CString } ,
@@ -44,65 +45,37 @@ impl Language {
44
45
Language ( _) => None ,
45
46
}
46
47
}
48
+ }
49
+
50
+ macro_rules! all_languages {
51
+ ( $self: expr, $ctx: expr, $e: expr) => {
52
+ $self. handle(
53
+ $ctx,
54
+ replace_token_sequence!{ [ LANGUAGE ] , [ bip0039:: English ] , $e} ,
55
+ replace_token_sequence!{ [ LANGUAGE ] , [ bip0039:: ChineseSimplified ] , $e} ,
56
+ replace_token_sequence!{ [ LANGUAGE ] , [ bip0039:: ChineseTraditional ] , $e} ,
57
+ replace_token_sequence!{ [ LANGUAGE ] , [ bip0039:: Czech ] , $e} ,
58
+ replace_token_sequence!{ [ LANGUAGE ] , [ bip0039:: French ] , $e} ,
59
+ replace_token_sequence!{ [ LANGUAGE ] , [ bip0039:: Italian ] , $e} ,
60
+ replace_token_sequence!{ [ LANGUAGE ] , [ bip0039:: Japanese ] , $e} ,
61
+ replace_token_sequence!{ [ LANGUAGE ] , [ bip0039:: Korean ] , $e} ,
62
+ replace_token_sequence!{ [ LANGUAGE ] , [ bip0039:: Portuguese ] , $e} ,
63
+ replace_token_sequence!{ [ LANGUAGE ] , [ bip0039:: Spanish ] , $e} ,
64
+ )
65
+ } ;
66
+ }
47
67
68
+ impl Language {
48
69
fn with_mnemonic_phrase_from_entropy < E : Into < Vec < u8 > > , T > (
49
70
self ,
50
71
entropy : E ,
51
72
f : impl FnOnce ( & str ) -> Option < T > ,
52
73
) -> Option < T > {
53
- self . handle (
54
- ( entropy, f) ,
55
- |( entropy, f) | {
56
- bip0039:: Mnemonic :: < bip0039:: English > :: from_entropy ( entropy)
57
- . ok ( )
58
- . and_then ( |mnemonic| f ( mnemonic. phrase ( ) ) )
59
- } ,
60
- |( entropy, f) | {
61
- bip0039:: Mnemonic :: < bip0039:: ChineseSimplified > :: from_entropy ( entropy)
62
- . ok ( )
63
- . and_then ( |mnemonic| f ( mnemonic. phrase ( ) ) )
64
- } ,
65
- |( entropy, f) | {
66
- bip0039:: Mnemonic :: < bip0039:: ChineseTraditional > :: from_entropy ( entropy)
67
- . ok ( )
68
- . and_then ( |mnemonic| f ( mnemonic. phrase ( ) ) )
69
- } ,
70
- |( entropy, f) | {
71
- bip0039:: Mnemonic :: < bip0039:: Czech > :: from_entropy ( entropy)
72
- . ok ( )
73
- . and_then ( |mnemonic| f ( mnemonic. phrase ( ) ) )
74
- } ,
75
- |( entropy, f) | {
76
- bip0039:: Mnemonic :: < bip0039:: French > :: from_entropy ( entropy)
77
- . ok ( )
78
- . and_then ( |mnemonic| f ( mnemonic. phrase ( ) ) )
79
- } ,
80
- |( entropy, f) | {
81
- bip0039:: Mnemonic :: < bip0039:: Italian > :: from_entropy ( entropy)
82
- . ok ( )
83
- . and_then ( |mnemonic| f ( mnemonic. phrase ( ) ) )
84
- } ,
85
- |( entropy, f) | {
86
- bip0039:: Mnemonic :: < bip0039:: Japanese > :: from_entropy ( entropy)
87
- . ok ( )
88
- . and_then ( |mnemonic| f ( mnemonic. phrase ( ) ) )
89
- } ,
90
- |( entropy, f) | {
91
- bip0039:: Mnemonic :: < bip0039:: Korean > :: from_entropy ( entropy)
92
- . ok ( )
93
- . and_then ( |mnemonic| f ( mnemonic. phrase ( ) ) )
94
- } ,
95
- |( entropy, f) | {
96
- bip0039:: Mnemonic :: < bip0039:: Portuguese > :: from_entropy ( entropy)
97
- . ok ( )
98
- . and_then ( |mnemonic| f ( mnemonic. phrase ( ) ) )
99
- } ,
100
- |( entropy, f) | {
101
- bip0039:: Mnemonic :: < bip0039:: Spanish > :: from_entropy ( entropy)
102
- . ok ( )
103
- . and_then ( |mnemonic| f ( mnemonic. phrase ( ) ) )
104
- } ,
105
- )
74
+ all_languages ! ( self , ( entropy, f) , |( entropy, f) | {
75
+ bip0039:: Mnemonic :: <LANGUAGE >:: from_entropy( entropy)
76
+ . ok( )
77
+ . and_then( |mnemonic| f( mnemonic. phrase( ) ) )
78
+ } )
106
79
}
107
80
108
81
fn with_seed_from_mnemonic_phrase < ' a , P : Into < Cow < ' a , str > > , T > (
@@ -111,75 +84,17 @@ impl Language {
111
84
passphrase : & str ,
112
85
f : impl FnOnce ( [ u8 ; 64 ] ) -> Option < T > ,
113
86
) -> Option < T > {
114
- self . handle (
115
- ( phrase, passphrase, f) ,
116
- |( phrase, passphrase, f) | {
117
- bip0039:: Mnemonic :: < bip0039:: English > :: from_phrase ( phrase)
118
- . ok ( )
119
- . and_then ( |mnemonic| f ( mnemonic. to_seed ( passphrase) ) )
120
- } ,
121
- |( phrase, passphrase, f) | {
122
- bip0039:: Mnemonic :: < bip0039:: ChineseSimplified > :: from_phrase ( phrase)
123
- . ok ( )
124
- . and_then ( |mnemonic| f ( mnemonic. to_seed ( passphrase) ) )
125
- } ,
126
- |( phrase, passphrase, f) | {
127
- bip0039:: Mnemonic :: < bip0039:: ChineseTraditional > :: from_phrase ( phrase)
128
- . ok ( )
129
- . and_then ( |mnemonic| f ( mnemonic. to_seed ( passphrase) ) )
130
- } ,
131
- |( phrase, passphrase, f) | {
132
- bip0039:: Mnemonic :: < bip0039:: Czech > :: from_phrase ( phrase)
133
- . ok ( )
134
- . and_then ( |mnemonic| f ( mnemonic. to_seed ( passphrase) ) )
135
- } ,
136
- |( phrase, passphrase, f) | {
137
- bip0039:: Mnemonic :: < bip0039:: French > :: from_phrase ( phrase)
138
- . ok ( )
139
- . and_then ( |mnemonic| f ( mnemonic. to_seed ( passphrase) ) )
140
- } ,
141
- |( phrase, passphrase, f) | {
142
- bip0039:: Mnemonic :: < bip0039:: Italian > :: from_phrase ( phrase)
143
- . ok ( )
144
- . and_then ( |mnemonic| f ( mnemonic. to_seed ( passphrase) ) )
145
- } ,
146
- |( phrase, passphrase, f) | {
147
- bip0039:: Mnemonic :: < bip0039:: Japanese > :: from_phrase ( phrase)
148
- . ok ( )
149
- . and_then ( |mnemonic| f ( mnemonic. to_seed ( passphrase) ) )
150
- } ,
151
- |( phrase, passphrase, f) | {
152
- bip0039:: Mnemonic :: < bip0039:: Korean > :: from_phrase ( phrase)
153
- . ok ( )
154
- . and_then ( |mnemonic| f ( mnemonic. to_seed ( passphrase) ) )
155
- } ,
156
- |( phrase, passphrase, f) | {
157
- bip0039:: Mnemonic :: < bip0039:: Portuguese > :: from_phrase ( phrase)
158
- . ok ( )
159
- . and_then ( |mnemonic| f ( mnemonic. to_seed ( passphrase) ) )
160
- } ,
161
- |( phrase, passphrase, f) | {
162
- bip0039:: Mnemonic :: < bip0039:: Spanish > :: from_phrase ( phrase)
163
- . ok ( )
164
- . and_then ( |mnemonic| f ( mnemonic. to_seed ( passphrase) ) )
165
- } ,
166
- )
87
+ all_languages ! ( self , ( phrase, passphrase, f) , |( phrase, passphrase, f) | {
88
+ bip0039:: Mnemonic :: <LANGUAGE >:: from_phrase( phrase)
89
+ . ok( )
90
+ . and_then( |mnemonic| f( mnemonic. to_seed( passphrase) ) )
91
+ } )
167
92
}
168
93
169
94
fn validate_mnemonic < ' a , P : Into < Cow < ' a , str > > > ( self , phrase : P ) -> Option < ( ) > {
170
- self . handle (
171
- phrase,
172
- |phrase| bip0039:: Mnemonic :: < bip0039:: English > :: validate ( phrase) . ok ( ) ,
173
- |phrase| bip0039:: Mnemonic :: < bip0039:: ChineseSimplified > :: validate ( phrase) . ok ( ) ,
174
- |phrase| bip0039:: Mnemonic :: < bip0039:: ChineseTraditional > :: validate ( phrase) . ok ( ) ,
175
- |phrase| bip0039:: Mnemonic :: < bip0039:: Czech > :: validate ( phrase) . ok ( ) ,
176
- |phrase| bip0039:: Mnemonic :: < bip0039:: French > :: validate ( phrase) . ok ( ) ,
177
- |phrase| bip0039:: Mnemonic :: < bip0039:: Italian > :: validate ( phrase) . ok ( ) ,
178
- |phrase| bip0039:: Mnemonic :: < bip0039:: Japanese > :: validate ( phrase) . ok ( ) ,
179
- |phrase| bip0039:: Mnemonic :: < bip0039:: Korean > :: validate ( phrase) . ok ( ) ,
180
- |phrase| bip0039:: Mnemonic :: < bip0039:: Portuguese > :: validate ( phrase) . ok ( ) ,
181
- |phrase| bip0039:: Mnemonic :: < bip0039:: Spanish > :: validate ( phrase) . ok ( ) ,
182
- )
95
+ all_languages ! ( self , phrase, |phrase| {
96
+ bip0039:: Mnemonic :: <LANGUAGE >:: validate( phrase) . ok( )
97
+ } )
183
98
}
184
99
}
185
100
0 commit comments