@@ -200,145 +200,133 @@ impl CompilerExtData {
200
200
201
201
fn time ( ) -> Self { CompilerExtData { branch_prob : None , sat_cost : 0.0 , dissat_cost : None } }
202
202
203
- fn cast_alt ( self ) -> Result < Self , types :: ErrorKind > {
204
- Ok ( CompilerExtData {
203
+ fn cast_alt ( self ) -> Self {
204
+ CompilerExtData {
205
205
branch_prob : None ,
206
206
sat_cost : self . sat_cost ,
207
207
dissat_cost : self . dissat_cost ,
208
- } )
208
+ }
209
209
}
210
210
211
- fn cast_swap ( self ) -> Result < Self , types :: ErrorKind > {
212
- Ok ( CompilerExtData {
211
+ fn cast_swap ( self ) -> Self {
212
+ CompilerExtData {
213
213
branch_prob : None ,
214
214
sat_cost : self . sat_cost ,
215
215
dissat_cost : self . dissat_cost ,
216
- } )
216
+ }
217
217
}
218
218
219
- fn cast_check ( self ) -> Result < Self , types :: ErrorKind > {
220
- Ok ( CompilerExtData {
219
+ fn cast_check ( self ) -> Self {
220
+ CompilerExtData {
221
221
branch_prob : None ,
222
222
sat_cost : self . sat_cost ,
223
223
dissat_cost : self . dissat_cost ,
224
- } )
224
+ }
225
225
}
226
226
227
- fn cast_dupif ( self ) -> Result < Self , types:: ErrorKind > {
228
- Ok ( CompilerExtData {
229
- branch_prob : None ,
230
- sat_cost : 2.0 + self . sat_cost ,
231
- dissat_cost : Some ( 1.0 ) ,
232
- } )
227
+ fn cast_dupif ( self ) -> Self {
228
+ CompilerExtData { branch_prob : None , sat_cost : 2.0 + self . sat_cost , dissat_cost : Some ( 1.0 ) }
233
229
}
234
230
235
- fn cast_verify ( self ) -> Result < Self , types :: ErrorKind > {
236
- Ok ( CompilerExtData { branch_prob : None , sat_cost : self . sat_cost , dissat_cost : None } )
231
+ fn cast_verify ( self ) -> Self {
232
+ CompilerExtData { branch_prob : None , sat_cost : self . sat_cost , dissat_cost : None }
237
233
}
238
234
239
- fn cast_nonzero ( self ) -> Result < Self , types :: ErrorKind > {
240
- Ok ( CompilerExtData { branch_prob : None , sat_cost : self . sat_cost , dissat_cost : Some ( 1.0 ) } )
235
+ fn cast_nonzero ( self ) -> Self {
236
+ CompilerExtData { branch_prob : None , sat_cost : self . sat_cost , dissat_cost : Some ( 1.0 ) }
241
237
}
242
238
243
- fn cast_zeronotequal ( self ) -> Result < Self , types :: ErrorKind > {
244
- Ok ( CompilerExtData {
239
+ fn cast_zeronotequal ( self ) -> Self {
240
+ CompilerExtData {
245
241
branch_prob : None ,
246
242
sat_cost : self . sat_cost ,
247
243
dissat_cost : self . dissat_cost ,
248
- } )
244
+ }
249
245
}
250
246
251
- fn cast_true ( self ) -> Result < Self , types :: ErrorKind > {
252
- Ok ( CompilerExtData { branch_prob : None , sat_cost : self . sat_cost , dissat_cost : None } )
247
+ fn cast_true ( self ) -> Self {
248
+ CompilerExtData { branch_prob : None , sat_cost : self . sat_cost , dissat_cost : None }
253
249
}
254
250
255
- fn cast_unlikely ( self ) -> Result < Self , types:: ErrorKind > {
256
- Ok ( CompilerExtData {
257
- branch_prob : None ,
258
- sat_cost : 2.0 + self . sat_cost ,
259
- dissat_cost : Some ( 1.0 ) ,
260
- } )
251
+ fn cast_unlikely ( self ) -> Self {
252
+ CompilerExtData { branch_prob : None , sat_cost : 2.0 + self . sat_cost , dissat_cost : Some ( 1.0 ) }
261
253
}
262
254
263
- fn cast_likely ( self ) -> Result < Self , types:: ErrorKind > {
264
- Ok ( CompilerExtData {
265
- branch_prob : None ,
266
- sat_cost : 1.0 + self . sat_cost ,
267
- dissat_cost : Some ( 2.0 ) ,
268
- } )
255
+ fn cast_likely ( self ) -> Self {
256
+ CompilerExtData { branch_prob : None , sat_cost : 1.0 + self . sat_cost , dissat_cost : Some ( 2.0 ) }
269
257
}
270
258
271
- fn and_b ( left : Self , right : Self ) -> Result < Self , types :: ErrorKind > {
272
- Ok ( CompilerExtData {
259
+ fn and_b ( left : Self , right : Self ) -> Self {
260
+ CompilerExtData {
273
261
branch_prob : None ,
274
262
sat_cost : left. sat_cost + right. sat_cost ,
275
263
dissat_cost : match ( left. dissat_cost , right. dissat_cost ) {
276
264
( Some ( l) , Some ( r) ) => Some ( l + r) ,
277
265
_ => None ,
278
266
} ,
279
- } )
267
+ }
280
268
}
281
269
282
- fn and_v ( left : Self , right : Self ) -> Result < Self , types :: ErrorKind > {
283
- Ok ( CompilerExtData {
270
+ fn and_v ( left : Self , right : Self ) -> Self {
271
+ CompilerExtData {
284
272
branch_prob : None ,
285
273
sat_cost : left. sat_cost + right. sat_cost ,
286
274
dissat_cost : None ,
287
- } )
275
+ }
288
276
}
289
277
290
- fn or_b ( l : Self , r : Self ) -> Result < Self , types :: ErrorKind > {
278
+ fn or_b ( l : Self , r : Self ) -> Self {
291
279
let lprob = l
292
280
. branch_prob
293
281
. expect ( "BUG: left branch prob must be set for disjunctions" ) ;
294
282
let rprob = r
295
283
. branch_prob
296
284
. expect ( "BUG: right branch prob must be set for disjunctions" ) ;
297
- Ok ( CompilerExtData {
285
+ CompilerExtData {
298
286
branch_prob : None ,
299
287
sat_cost : lprob * ( l. sat_cost + r. dissat_cost . unwrap ( ) )
300
288
+ rprob * ( r. sat_cost + l. dissat_cost . unwrap ( ) ) ,
301
289
dissat_cost : Some ( l. dissat_cost . unwrap ( ) + r. dissat_cost . unwrap ( ) ) ,
302
- } )
290
+ }
303
291
}
304
292
305
- fn or_d ( l : Self , r : Self ) -> Result < Self , types :: ErrorKind > {
293
+ fn or_d ( l : Self , r : Self ) -> Self {
306
294
let lprob = l
307
295
. branch_prob
308
296
. expect ( "BUG: left branch prob must be set for disjunctions" ) ;
309
297
let rprob = r
310
298
. branch_prob
311
299
. expect ( "BUG: right branch prob must be set for disjunctions" ) ;
312
- Ok ( CompilerExtData {
300
+ CompilerExtData {
313
301
branch_prob : None ,
314
302
sat_cost : lprob * l. sat_cost + rprob * ( r. sat_cost + l. dissat_cost . unwrap ( ) ) ,
315
303
dissat_cost : r. dissat_cost . map ( |rd| l. dissat_cost . unwrap ( ) + rd) ,
316
- } )
304
+ }
317
305
}
318
306
319
- fn or_c ( l : Self , r : Self ) -> Result < Self , types :: ErrorKind > {
307
+ fn or_c ( l : Self , r : Self ) -> Self {
320
308
let lprob = l
321
309
. branch_prob
322
310
. expect ( "BUG: left branch prob must be set for disjunctions" ) ;
323
311
let rprob = r
324
312
. branch_prob
325
313
. expect ( "BUG: right branch prob must be set for disjunctions" ) ;
326
- Ok ( CompilerExtData {
314
+ CompilerExtData {
327
315
branch_prob : None ,
328
316
sat_cost : lprob * l. sat_cost + rprob * ( r. sat_cost + l. dissat_cost . unwrap ( ) ) ,
329
317
dissat_cost : None ,
330
- } )
318
+ }
331
319
}
332
320
333
321
#[ allow( clippy:: manual_map) ] // Complex if/let is better as is.
334
- fn or_i ( l : Self , r : Self ) -> Result < Self , types :: ErrorKind > {
322
+ fn or_i ( l : Self , r : Self ) -> Self {
335
323
let lprob = l
336
324
. branch_prob
337
325
. expect ( "BUG: left branch prob must be set for disjunctions" ) ;
338
326
let rprob = r
339
327
. branch_prob
340
328
. expect ( "BUG: right branch prob must be set for disjunctions" ) ;
341
- Ok ( CompilerExtData {
329
+ CompilerExtData {
342
330
branch_prob : None ,
343
331
sat_cost : lprob * ( 2.0 + l. sat_cost ) + rprob * ( 1.0 + r. sat_cost ) ,
344
332
dissat_cost : if let ( Some ( ldis) , Some ( rdis) ) = ( l. dissat_cost , r. dissat_cost ) {
@@ -354,7 +342,7 @@ impl CompilerExtData {
354
342
} else {
355
343
None
356
344
} ,
357
- } )
345
+ }
358
346
}
359
347
360
348
fn and_or ( a : Self , b : Self , c : Self ) -> Result < Self , types:: ErrorKind > {
@@ -434,11 +422,6 @@ impl CompilerExtData {
434
422
Pk : MiniscriptKey ,
435
423
Ctx : ScriptContext ,
436
424
{
437
- let wrap_err = |result : Result < Self , ErrorKind > | {
438
- result
439
- . map_err ( |kind| types:: Error { fragment_string : fragment. to_string ( ) , error : kind } )
440
- } ;
441
-
442
425
match * fragment {
443
426
Terminal :: True => Ok ( Self :: TRUE ) ,
444
427
Terminal :: False => Ok ( Self :: FALSE ) ,
@@ -488,50 +471,53 @@ impl CompilerExtData {
488
471
Terminal :: Hash256 ( ..) => Ok ( Self :: hash ( ) ) ,
489
472
Terminal :: Ripemd160 ( ..) => Ok ( Self :: hash ( ) ) ,
490
473
Terminal :: Hash160 ( ..) => Ok ( Self :: hash ( ) ) ,
491
- Terminal :: Alt ( ref sub) => wrap_err ( Self :: cast_alt ( get_child ( & sub. node , 0 ) ?) ) ,
492
- Terminal :: Swap ( ref sub) => wrap_err ( Self :: cast_swap ( get_child ( & sub. node , 0 ) ?) ) ,
493
- Terminal :: Check ( ref sub) => wrap_err ( Self :: cast_check ( get_child ( & sub. node , 0 ) ?) ) ,
494
- Terminal :: DupIf ( ref sub) => wrap_err ( Self :: cast_dupif ( get_child ( & sub. node , 0 ) ?) ) ,
495
- Terminal :: Verify ( ref sub) => wrap_err ( Self :: cast_verify ( get_child ( & sub. node , 0 ) ?) ) ,
496
- Terminal :: NonZero ( ref sub) => wrap_err ( Self :: cast_nonzero ( get_child ( & sub. node , 0 ) ?) ) ,
474
+ Terminal :: Alt ( ref sub) => Ok ( Self :: cast_alt ( get_child ( & sub. node , 0 ) ?) ) ,
475
+ Terminal :: Swap ( ref sub) => Ok ( Self :: cast_swap ( get_child ( & sub. node , 0 ) ?) ) ,
476
+ Terminal :: Check ( ref sub) => Ok ( Self :: cast_check ( get_child ( & sub. node , 0 ) ?) ) ,
477
+ Terminal :: DupIf ( ref sub) => Ok ( Self :: cast_dupif ( get_child ( & sub. node , 0 ) ?) ) ,
478
+ Terminal :: Verify ( ref sub) => Ok ( Self :: cast_verify ( get_child ( & sub. node , 0 ) ?) ) ,
479
+ Terminal :: NonZero ( ref sub) => Ok ( Self :: cast_nonzero ( get_child ( & sub. node , 0 ) ?) ) ,
497
480
Terminal :: ZeroNotEqual ( ref sub) => {
498
- wrap_err ( Self :: cast_zeronotequal ( get_child ( & sub. node , 0 ) ?) )
481
+ Ok ( Self :: cast_zeronotequal ( get_child ( & sub. node , 0 ) ?) )
499
482
}
500
483
Terminal :: AndB ( ref l, ref r) => {
501
484
let ltype = get_child ( & l. node , 0 ) ?;
502
485
let rtype = get_child ( & r. node , 1 ) ?;
503
- wrap_err ( Self :: and_b ( ltype, rtype) )
486
+ Ok ( Self :: and_b ( ltype, rtype) )
504
487
}
505
488
Terminal :: AndV ( ref l, ref r) => {
506
489
let ltype = get_child ( & l. node , 0 ) ?;
507
490
let rtype = get_child ( & r. node , 1 ) ?;
508
- wrap_err ( Self :: and_v ( ltype, rtype) )
491
+ Ok ( Self :: and_v ( ltype, rtype) )
509
492
}
510
493
Terminal :: OrB ( ref l, ref r) => {
511
494
let ltype = get_child ( & l. node , 0 ) ?;
512
495
let rtype = get_child ( & r. node , 1 ) ?;
513
- wrap_err ( Self :: or_b ( ltype, rtype) )
496
+ Ok ( Self :: or_b ( ltype, rtype) )
514
497
}
515
498
Terminal :: OrD ( ref l, ref r) => {
516
499
let ltype = get_child ( & l. node , 0 ) ?;
517
500
let rtype = get_child ( & r. node , 1 ) ?;
518
- wrap_err ( Self :: or_d ( ltype, rtype) )
501
+ Ok ( Self :: or_d ( ltype, rtype) )
519
502
}
520
503
Terminal :: OrC ( ref l, ref r) => {
521
504
let ltype = get_child ( & l. node , 0 ) ?;
522
505
let rtype = get_child ( & r. node , 1 ) ?;
523
- wrap_err ( Self :: or_c ( ltype, rtype) )
506
+ Ok ( Self :: or_c ( ltype, rtype) )
524
507
}
525
508
Terminal :: OrI ( ref l, ref r) => {
526
509
let ltype = get_child ( & l. node , 0 ) ?;
527
510
let rtype = get_child ( & r. node , 1 ) ?;
528
- wrap_err ( Self :: or_i ( ltype, rtype) )
511
+ Ok ( Self :: or_i ( ltype, rtype) )
529
512
}
530
513
Terminal :: AndOr ( ref a, ref b, ref c) => {
531
514
let atype = get_child ( & a. node , 0 ) ?;
532
515
let btype = get_child ( & b. node , 1 ) ?;
533
516
let ctype = get_child ( & c. node , 2 ) ?;
534
- wrap_err ( Self :: and_or ( atype, btype, ctype) )
517
+ Self :: and_or ( atype, btype, ctype) . map_err ( |kind| types:: Error {
518
+ fragment_string : fragment. to_string ( ) ,
519
+ error : kind,
520
+ } )
535
521
}
536
522
Terminal :: Thresh ( k, ref subs) => {
537
523
if k == 0 {
@@ -548,18 +534,14 @@ impl CompilerExtData {
548
534
}
549
535
550
536
let mut last_err_frag = None ;
551
- let res = Self :: threshold ( k, subs. len ( ) , |n| match get_child ( & subs[ n] . node , n) {
537
+ Self :: threshold ( k, subs. len ( ) , |n| match get_child ( & subs[ n] . node , n) {
552
538
Ok ( x) => Ok ( x) ,
553
539
Err ( e) => {
554
540
last_err_frag = Some ( e. fragment_string ) ;
555
541
Err ( e. error )
556
542
}
557
- } ) ;
558
-
559
- res. map_err ( |kind| types:: Error {
560
- fragment_string : last_err_frag. unwrap_or_else ( || fragment. to_string ( ) ) ,
561
- error : kind,
562
543
} )
544
+ . map_err ( |kind| types:: Error { fragment_string : fragment. to_string ( ) , error : kind } )
563
545
}
564
546
}
565
547
}
@@ -650,7 +632,7 @@ struct Cast<Pk: MiniscriptKey, Ctx: ScriptContext> {
650
632
node : fn ( Arc < Miniscript < Pk , Ctx > > ) -> Terminal < Pk , Ctx > ,
651
633
ast_type : fn ( types:: Type ) -> Result < types:: Type , ErrorKind > ,
652
634
ext_data : fn ( types:: ExtData ) -> types:: ExtData ,
653
- comp_ext_data : fn ( CompilerExtData ) -> Result < CompilerExtData , types :: ErrorKind > ,
635
+ comp_ext_data : fn ( CompilerExtData ) -> CompilerExtData ,
654
636
}
655
637
656
638
impl < Pk : MiniscriptKey , Ctx : ScriptContext > Cast < Pk , Ctx > {
@@ -661,7 +643,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Cast<Pk, Ctx> {
661
643
( self . ast_type ) ( ast. ms . ty ) ?,
662
644
( self . ext_data ) ( ast. ms . ext ) ,
663
645
) ) ,
664
- comp_ext_data : ( self . comp_ext_data ) ( ast. comp_ext_data ) ? ,
646
+ comp_ext_data : ( self . comp_ext_data ) ( ast. comp_ext_data ) ,
665
647
} )
666
648
}
667
649
}
0 commit comments