@@ -3,12 +3,15 @@ use bitcoin::blockdata::script::{Instruction, PushBytes, PushBytesBuf, ScriptBuf
33use bitcoin:: opcodes:: { OP_0 , OP_TRUE } ;
44use bitcoin:: script:: write_scriptint;
55use bitcoin:: Witness ;
6- use serde:: { Deserialize , Serialize } ;
76use std:: collections:: HashMap ;
87use std:: convert:: TryFrom ;
98use std:: hash:: { DefaultHasher , Hash , Hasher } ;
109
11- #[ derive( Clone , Debug , Hash , Serialize , Deserialize , PartialEq ) ]
10+ #[ cfg( feature = "serde" ) ]
11+ use serde:: { Deserialize , Serialize } ;
12+
13+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
14+ #[ derive( Clone , Debug , Hash , PartialEq ) ]
1215pub enum Block {
1316 Call ( u64 ) ,
1417 Script ( ScriptBuf ) ,
@@ -21,7 +24,8 @@ impl Block {
2124 }
2225}
2326
24- #[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
27+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
28+ #[ derive( Clone , Debug , PartialEq ) ]
2529pub struct StructuredScript {
2630 size : usize ,
2731 pub debug_identifier : String ,
@@ -43,15 +47,18 @@ fn calculate_hash<T: Hash>(t: &T) -> u64 {
4347
4448impl StructuredScript {
4549 pub fn new ( debug_info : & str ) -> Self {
46- let blocks = Vec :: new ( ) ;
4750 StructuredScript {
4851 size : 0 ,
4952 debug_identifier : debug_info. to_string ( ) ,
50- blocks,
53+ blocks : Vec :: new ( ) ,
5154 script_map : HashMap :: new ( ) ,
5255 }
5356 }
5457
58+ pub fn is_empty ( & self ) -> bool {
59+ self . size == 0
60+ }
61+
5562 pub fn len ( & self ) -> usize {
5663 self . size
5764 }
@@ -63,7 +70,7 @@ impl StructuredScript {
6370 pub fn get_structured_script ( & self , id : & u64 ) -> & StructuredScript {
6471 self . script_map
6572 . get ( id)
66- . expect ( & format ! ( "script id: {} not found in script_map." , id) )
73+ . unwrap_or_else ( || panic ! ( "script id: {} not found in script_map." , id) )
6774 }
6875
6976 // Return the debug information of the Opcode at position
@@ -133,10 +140,10 @@ impl StructuredScript {
133140 }
134141
135142 pub fn push_env_script ( mut self , mut data : StructuredScript ) -> StructuredScript {
136- if data. len ( ) == 0 {
143+ if data. is_empty ( ) {
137144 return self ;
138145 }
139- if self . len ( ) == 0 {
146+ if self . is_empty ( ) {
140147 return data;
141148 }
142149
@@ -342,8 +349,7 @@ impl NotU8Pushable for u32 {
342349}
343350impl NotU8Pushable for usize {
344351 fn bitcoin_script_push ( self , builder : StructuredScript ) -> StructuredScript {
345- builder
346- . push_int ( i64:: try_from ( self ) . unwrap_or_else ( |_| panic ! ( "Usize does not fit in i64" ) ) )
352+ builder. push_int ( i64:: try_from ( self ) . expect ( "Usize does not fit in i64" ) )
347353 }
348354}
349355impl NotU8Pushable for Vec < u8 > {
0 commit comments