88//
99
1010use crate :: regex:: Regex ;
11- use core:: fmt;
1211use std:: { collections:: HashMap , rc:: Rc } ;
1312
1413pub type VarId = u32 ;
1514
16- #[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
15+ #[ cfg_attr( test, derive( Debug ) ) ]
16+ #[ derive( PartialEq , Eq , Clone , Copy ) ]
1717pub enum OpCode {
1818 // binary operations
1919 Add ,
@@ -108,7 +108,8 @@ pub enum OpCode {
108108 Invalid ,
109109}
110110
111- #[ derive( Clone , Debug , PartialEq ) ]
111+ #[ cfg_attr( test, derive( Debug ) ) ]
112+ #[ derive( Clone , PartialEq ) ]
112113pub enum Constant {
113114 Number ( f64 ) ,
114115 String ( Rc < str > ) ,
@@ -133,38 +134,44 @@ impl From<Rc<Regex>> for Constant {
133134 }
134135}
135136
136- #[ derive( Debug , PartialEq , Clone , Copy ) ]
137+ #[ cfg_attr( test, derive( Debug ) ) ]
138+ #[ derive( PartialEq , Clone , Copy ) ]
137139pub struct SourceLocation {
138140 pub line : u32 ,
139141 pub column : u32 ,
140142}
141143
142- #[ derive( Debug , PartialEq , Default ) ]
144+ #[ cfg_attr( test, derive( Debug ) ) ]
145+ #[ derive( PartialEq , Default ) ]
143146pub struct DebugInfo {
144147 pub file : Rc < str > ,
145148 pub source_locations : Vec < SourceLocation > ,
146149}
147150
148- #[ derive( Debug , PartialEq ) ]
151+ #[ cfg_attr( test, derive( Debug ) ) ]
152+ #[ derive( PartialEq ) ]
149153pub struct Action {
150154 pub instructions : Vec < OpCode > ,
151155 pub debug_info : DebugInfo ,
152156}
153157
154- #[ derive( Debug , PartialEq ) ]
158+ #[ cfg_attr( test, derive( Debug ) ) ]
159+ #[ derive( PartialEq ) ]
155160pub enum Pattern {
156161 Expr ( Action ) ,
157162 Range { start : Action , end : Action } ,
158163 All ,
159164}
160165
161- #[ derive( Debug , PartialEq ) ]
166+ #[ cfg_attr( test, derive( Debug ) ) ]
167+ #[ derive( PartialEq ) ]
162168pub struct AwkRule {
163169 pub pattern : Pattern ,
164170 pub action : Action ,
165171}
166172
167- #[ derive( Debug , PartialEq , Default ) ]
173+ #[ cfg_attr( test, derive( Debug ) ) ]
174+ #[ derive( PartialEq , Default ) ]
168175pub struct Function {
169176 pub name : Rc < str > ,
170177 pub parameters_count : usize ,
@@ -182,13 +189,14 @@ pub struct Program {
182189 pub functions : Vec < Function > ,
183190}
184191
185- impl fmt:: Debug for Program {
186- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
192+ #[ cfg( test) ]
193+ impl core:: fmt:: Debug for Program {
194+ fn fmt ( & self , f : & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
187195 writeln ! ( f, "Program:" ) ?;
188196 for action in & self . begin_actions {
189197 writeln ! ( f, " BEGIN {{" ) ?;
190198 for instruction in & action. instructions {
191- writeln ! ( f, " {:?}" , instruction ) ?;
199+ writeln ! ( f, " {instruction :?}" ) ?;
192200 }
193201 writeln ! ( f, " }}" ) ?;
194202 }
@@ -198,18 +206,18 @@ impl fmt::Debug for Program {
198206 Pattern :: Expr ( action) => {
199207 writeln ! ( f, " /" ) ?;
200208 for instruction in & action. instructions {
201- writeln ! ( f, " {:?}" , instruction ) ?;
209+ writeln ! ( f, " {instruction :?}" ) ?;
202210 }
203211 writeln ! ( f, " / {{" ) ?;
204212 }
205213 Pattern :: Range { start, end } => {
206214 writeln ! ( f, " /" ) ?;
207215 for instruction in & start. instructions {
208- writeln ! ( f, " {:?}" , instruction ) ?;
216+ writeln ! ( f, " {instruction :?}" ) ?;
209217 }
210218 writeln ! ( f, " /, /" ) ?;
211219 for instruction in & end. instructions {
212- writeln ! ( f, " {:?}" , instruction ) ?;
220+ writeln ! ( f, " {instruction :?}" ) ?;
213221 }
214222 writeln ! ( f, " / {{" ) ?;
215223 }
@@ -218,36 +226,37 @@ impl fmt::Debug for Program {
218226 }
219227 }
220228 for instruction in & rule. action . instructions {
221- writeln ! ( f, " {:?}" , instruction ) ?;
229+ writeln ! ( f, " {instruction :?}" ) ?;
222230 }
223231 writeln ! ( f, " }}" ) ?;
224232 }
225233
226234 for action in & self . end_actions {
227235 writeln ! ( f, " END {{" ) ?;
228236 for instruction in & action. instructions {
229- writeln ! ( f, " {:?}" , instruction ) ?;
237+ writeln ! ( f, " {instruction :?}" ) ?;
230238 }
231239 writeln ! ( f, " }}" ) ?;
232240 }
233241 writeln ! ( f, "Functions:" ) ?;
234242 for function in & self . functions {
235243 writeln ! ( f, " {}({}) {{" , function. name, function. parameters_count) ?;
236244 for instruction in & function. instructions {
237- writeln ! ( f, " {:?}" , instruction ) ?;
245+ writeln ! ( f, " {instruction :?}" ) ?;
238246 }
239247 writeln ! ( f, " }}" ) ?;
240248 }
241249 writeln ! ( f, "Constants:" ) ?;
242250 for ( i, constant) in self . constants . iter ( ) . enumerate ( ) {
243- writeln ! ( f, " {}: {:?}" , i , constant ) ?;
251+ writeln ! ( f, " {i }: {constant :?}" ) ?;
244252 }
245253 Ok ( ( ) )
246254 }
247255}
248256
249257#[ repr( u32 ) ]
250- #[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
258+ #[ cfg_attr( test, derive( Debug ) ) ]
259+ #[ derive( PartialEq , Eq , Clone , Copy ) ]
251260pub enum SpecialVar {
252261 Argc ,
253262 Argv ,
@@ -271,7 +280,8 @@ pub enum SpecialVar {
271280}
272281
273282#[ repr( u32 ) ]
274- #[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
283+ #[ cfg_attr( test, derive( Debug ) ) ]
284+ #[ derive( PartialEq , Eq , Clone , Copy ) ]
275285pub enum BuiltinFunction {
276286 // arithmetic functions
277287 Atan2 ,
0 commit comments