@@ -19,8 +19,8 @@ use crate::miniscript::{types, ScriptContext};
1919use crate :: prelude:: * ;
2020use crate :: util:: MsKeyBuilder ;
2121use crate :: {
22- errstr , expression, AbsLockTime , Error , FromStrKey , Miniscript , MiniscriptKey , RelLockTime ,
23- Terminal , ToPublicKey ,
22+ expression, AbsLockTime , Error , FromStrKey , Miniscript , MiniscriptKey , RelLockTime , Terminal ,
23+ ToPublicKey ,
2424} ;
2525
2626impl < Pk : MiniscriptKey , Ctx : ScriptContext > Terminal < Pk , Ctx > {
@@ -81,7 +81,13 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
8181 {
8282 fmt_2 ( f, "or_i(" , l, r, is_debug)
8383 }
84- Terminal :: Thresh ( k, ref subs) => fmt_n ( f, "thresh(" , k, subs, is_debug) ,
84+ Terminal :: Thresh ( ref thresh) => {
85+ if is_debug {
86+ fmt:: Debug :: fmt ( & thresh. debug ( "thresh" , true ) , f)
87+ } else {
88+ fmt:: Display :: fmt ( & thresh. display ( "thresh" , true ) , f)
89+ }
90+ }
8591 Terminal :: Multi ( ref thresh) => {
8692 if is_debug {
8793 fmt:: Debug :: fmt ( & thresh. debug ( "multi" , true ) , f)
@@ -168,21 +174,6 @@ fn fmt_2<D: fmt::Debug + fmt::Display>(
168174 conditional_fmt ( f, b, is_debug) ?;
169175 f. write_str ( ")" )
170176}
171- fn fmt_n < D : fmt:: Debug + fmt:: Display > (
172- f : & mut fmt:: Formatter ,
173- name : & str ,
174- first : usize ,
175- list : & [ D ] ,
176- is_debug : bool ,
177- ) -> fmt:: Result {
178- f. write_str ( name) ?;
179- write ! ( f, "{}" , first) ?;
180- for el in list {
181- f. write_str ( "," ) ?;
182- conditional_fmt ( f, el, is_debug) ?;
183- }
184- f. write_str ( ")" )
185- }
186177fn conditional_fmt < D : fmt:: Debug + fmt:: Display > (
187178 f : & mut fmt:: Formatter ,
188179 data : & D ,
@@ -307,25 +298,11 @@ impl<Pk: FromStrKey, Ctx: ScriptContext> crate::expression::FromTree for Termina
307298 ( "or_d" , 2 ) => expression:: binary ( top, Terminal :: OrD ) ,
308299 ( "or_c" , 2 ) => expression:: binary ( top, Terminal :: OrC ) ,
309300 ( "or_i" , 2 ) => expression:: binary ( top, Terminal :: OrI ) ,
310- ( "thresh" , n) => {
311- if n == 0 {
312- return Err ( errstr ( "no arguments given" ) ) ;
313- }
314- let k = expression:: terminal ( & top. args [ 0 ] , expression:: parse_num) ? as usize ;
315- if k > n - 1 {
316- return Err ( errstr ( "higher threshold than there are subexpressions" ) ) ;
317- }
318- if n == 1 {
319- return Err ( errstr ( "empty thresholds not allowed in descriptors" ) ) ;
320- }
321-
322- let subs: Result < Vec < Arc < Miniscript < Pk , Ctx > > > , _ > = top. args [ 1 ..]
323- . iter ( )
324- . map ( expression:: FromTree :: from_tree)
325- . collect ( ) ;
326-
327- Ok ( Terminal :: Thresh ( k, subs?) )
328- }
301+ ( "thresh" , _) => top
302+ . to_null_threshold ( )
303+ . map_err ( Error :: ParseThreshold ) ?
304+ . translate_by_index ( |i| Miniscript :: from_tree ( & top. args [ 1 + i] ) . map ( Arc :: new) )
305+ . map ( Terminal :: Thresh ) ,
329306 ( "multi" , _) => top
330307 . to_null_threshold ( )
331308 . map_err ( Error :: ParseThreshold ) ?
@@ -475,13 +452,13 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
475452 . push_opcode ( opcodes:: all:: OP_ELSE )
476453 . push_astelem ( right)
477454 . push_opcode ( opcodes:: all:: OP_ENDIF ) ,
478- Terminal :: Thresh ( k , ref subs ) => {
479- builder = builder. push_astelem ( & subs [ 0 ] ) ;
480- for sub in & subs [ 1 ..] {
455+ Terminal :: Thresh ( ref thresh ) => {
456+ builder = builder. push_astelem ( & thresh . data ( ) [ 0 ] ) ;
457+ for sub in & thresh . data ( ) [ 1 ..] {
481458 builder = builder. push_astelem ( sub) . push_opcode ( opcodes:: all:: OP_ADD ) ;
482459 }
483460 builder
484- . push_int ( k as i64 )
461+ . push_int ( thresh . k ( ) as i64 )
485462 . push_opcode ( opcodes:: all:: OP_EQUAL )
486463 }
487464 Terminal :: Multi ( ref thresh) => {
0 commit comments