macro_tools 0.85.0

Tools for writing procedural macroses.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use super :: *;

#[ test ]
fn basic() 
{
  use syn :: { parse_quote, ItemStruct };
  use the_module ::struct_like;

  // - struct

  let item: ItemStruct = parse_quote! {
  struct Example
  {
   field1: i32,
   field2: String
 }
 };
  let exp = struct_like ::StructLike ::Struct(item);

  let got: struct_like ::StructLike = parse_quote! {
  struct Example
  {
   field1: i32,
   field2: String
 }
 };
  assert_eq!(got, exp);

  // - pub struct

  let item: ItemStruct = parse_quote! {
  pub( crate ) struct Example
  {
   field1: i32,
   field2: String
 }
 };
  let exp = struct_like ::StructLike ::Struct(item);

  let got: struct_like ::StructLike = parse_quote! {
  pub( crate ) struct Example
  {
   field1: i32,
   field2: String
 }
 };
  assert_eq!(got, exp);

  // - enum

  let item: syn ::ItemEnum = parse_quote! {
  enum Example
  {
   field1,
   field2( i32 ),
 }
 };
  let exp = struct_like ::StructLike ::Enum(item);

  let got: struct_like ::StructLike = parse_quote! {
  enum Example
  {
   field1,
   field2( i32 ),
 }
 };
  assert_eq!(got, exp);

  // - pub enum

  let item: syn ::ItemEnum = parse_quote! {
  pub( crate ) enum Example
  {
   field1,
   field2( i32 ),
 }
 };
  let exp = struct_like ::StructLike ::Enum(item);

  let got: struct_like ::StructLike = parse_quote! {
  pub( crate ) enum Example
  {
   field1,
   field2( i32 ),
 }
 };
  assert_eq!(got, exp);

  // - unit

  let item: syn ::ItemStruct = parse_quote! {
  struct Unit;
 };
  let exp = struct_like ::StructLike ::Unit(item);

  let got: struct_like ::StructLike = parse_quote! {
  struct Unit;
 };
  assert_eq!(got, exp);

  // - pub unit

  let item: syn ::ItemStruct = parse_quote! {
  pub( crate ) struct Unit;
 };
  let exp = struct_like ::StructLike ::Unit(item);

  let got: struct_like ::StructLike = parse_quote! {
  pub( crate ) struct Unit;
 };
  assert_eq!(got, exp);
}

//

#[ test ]
fn structlike_unit_struct() 
{
  use syn ::parse_quote;
  use the_module ::struct_like ::StructLike;

  let struct_like: StructLike = parse_quote! {
  struct UnitStruct;
 };

  assert!(
  matches!(struct_like, StructLike ::Unit(_)),
  "Expected StructLike ::Unit variant"
 );
  assert_eq!(struct_like.ident().to_string(), "UnitStruct", "Struct name mismatch");
}

#[ test ]
fn structlike_struct() 
{
  use syn ::parse_quote;
  use the_module ::struct_like ::StructLike;

  let struct_like: StructLike = parse_quote! {
  struct RegularStruct
  {
   a: i32,
   b: String,
 }
 };

  assert!(
  matches!(struct_like, StructLike ::Struct(_)),
  "Expected StructLike ::Struct variant"
 );
  assert_eq!(struct_like.ident().to_string(), "RegularStruct", "Struct name mismatch");
  assert_eq!(struct_like.fields().count(), 2, "Expected two fields");
}

#[ test ]
fn structlike_enum() 
{
  use syn ::parse_quote;
  use the_module ::struct_like ::StructLike;

  let struct_like: StructLike = parse_quote! {
  enum TestEnum
  {
   Variant1,
   Variant2 { x: i32, y: String },
 }
 };

  assert!(
  matches!(struct_like, StructLike ::Enum(_)),
  "Expected StructLike ::Enum variant"
 );
  assert_eq!(struct_like.ident().to_string(), "TestEnum", "Enum name mismatch");
}

#[ test ]
fn test_field_or_variant_field() 
{
  use syn ::parse_quote;
  use the_module ::struct_like :: { FieldOrVariant, StructLike };

  let input: StructLike = parse_quote! {
  struct MyStruct
  {
   my_field: i32,
 }
 };

  let field = input.fields().next().expect("Expected at least one field");
  let field_or_variant = FieldOrVariant ::from(field);

  match field_or_variant 
  {
  FieldOrVariant ::Field(f) => assert_eq!(f.ty, parse_quote!(i32)),
  FieldOrVariant ::Variant(_) => panic!("Expected Field variant"),
 }
}

#[ test ]
fn test_field_or_variant_variant() 
{
  use syn ::parse_quote;
  use the_module ::struct_like :: { FieldOrVariant, StructLike };

  let input: StructLike = parse_quote! {
  enum MyEnum
  {
   Variant1,
 }
 };

  let variant = input.elements().next().expect("Expected at least one variant");
  let field_or_variant = variant;

  match field_or_variant 
  {
  FieldOrVariant ::Variant(v) =>
  {
   let exp: syn ::Ident = parse_quote!(Variant1);
   assert_eq!(v.ident, exp);
 }
  FieldOrVariant ::Field(_) => panic!("Expected Variant variant"),
 }
}

#[ test ]
fn test_typ() 
{
  use syn ::parse_quote;
  use the_module ::struct_like :: { FieldOrVariant, StructLike };

  let input: StructLike = parse_quote! {
  struct MyStruct
  {
   my_field: i32,
 }
 };

  let field = input.fields().next().expect("Expected at least one field");
  let field_or_variant = FieldOrVariant ::from(field);
  assert_eq!(field_or_variant.typ(), Some(&parse_quote!(i32)));
}

#[ test ]
fn test_attrs() 
{
  use syn ::parse_quote;
  use the_module ::struct_like :: { FieldOrVariant, StructLike };

  let input: StructLike = parse_quote! {
  struct MyStruct
  {
   #[ some_attr ]
   my_field: i32,
 }
 };

  let field = input.fields().next().expect("Expected at least one field");
  let field_or_variant = FieldOrVariant ::from(field);
  assert!(field_or_variant.attrs().iter().any(|attr| attr.path().is_ident("some_attr")));
}

#[ test ]
fn test_vis() 
{
  use syn ::parse_quote;
  use the_module ::struct_like :: { FieldOrVariant, StructLike };

  let input: StructLike = parse_quote! {
  struct MyStruct
  {
   pub my_field: i32,
 }
 };

  let field = input.fields().next().expect("Expected at least one field");
  let field_or_variant = FieldOrVariant ::from(field);
  assert!(matches!(field_or_variant.vis(), Some(syn ::Visibility ::Public(_))));
}

#[ test ]
fn test_ident() 
{
  use the_module ::struct_like ::StructLike;
  use syn ::parse_quote;
  use the_module ::struct_like ::FieldOrVariant;

  let input: StructLike = parse_quote! {
  struct MyStruct
  {
   my_field: i32,
 }
 };

  // Extract the first field using the fields iterator from StructLike
  let field = input.fields().next().expect("Expected at least one field");

  let field_or_variant = FieldOrVariant ::from(field);
  assert_eq!(field_or_variant.ident().unwrap(), "my_field");
}

//

#[ test ]
fn struct_with_attrs() 
{
  use the_module ::struct_like ::StructLike;

  let input: proc_macro2 ::TokenStream = quote ::quote! {
  #[ derive( From, InnerFrom, Display, FromStr, PartialEq, Debug ) ]
  #[ display( "{a}-{b}" ) ]
  pub struct Struct1
  {
   a: i32,
   b: i32,
 }
 };

  let ast: StructLike = syn ::parse2(input).unwrap();
  let field = ast.fields().next().unwrap();
  let field_or_variant = the_module ::struct_like ::FieldOrVariant ::from(field);
  assert_eq!(field_or_variant.ident().unwrap(), "a");
}

//

// #[ test ]
// fn struct_with_attrs2()
// {
//   use the_module ::struct_like ::StructLike;
//
//   let input: proc_macro2 ::TokenStream = quote ::quote!
//   {
//     #[ derive( Debug, PartialEq, the_module ::From ) ]
//     #[ debug ]
//     pub enum GetData
//     {
//       #[ allow( dead_code ) ]
//       Nothing,
//       FromString( String ),
//       FromBin( &'static [ u8 ] ),
// }
// };
//
//   let ast: StructLike = syn ::parse2( input ).unwrap();
//   let field = ast.elements().next().unwrap();
//   let field_or_variant = the_module ::struct_like ::FieldOrVariant ::from( field );
//   assert_eq!( field_or_variant.ident().unwrap().to_string(), "Nothing" );
//
// }

#[ test ]
fn struct_with_attrs2() 
{
  use quote ::ToTokens;
  use the_module ::struct_like :: { StructLike, FieldOrVariant };

  let input: proc_macro2 ::TokenStream = quote ::quote! {
  #[ derive( Debug, PartialEq, the_module ::From ) ]
  #[ debug ]
  pub enum GetData
  {
   #[ allow( dead_code ) ]
   Nothing,
   FromString( String ),
   FromBin( & 'static [u8] ),
 }
 };

  // Test StructLike's ability to handle enum declarations
  let ast: StructLike = syn ::parse2(input).unwrap();

  // Verify that StructLike correctly identifies enum variant type
  assert!(matches!(ast, StructLike ::Enum(_)), "Expected StructLike ::Enum variant");

  // Check the attributes of the enum
  let attrs = ast.attrs();
  assert!(
  attrs.iter().any(|attr| attr.path().is_ident("derive")),
  "Missing derive attribute"
 );
  assert!(
  attrs.iter().any(|attr| attr.path().is_ident("debug")),
  "Missing debug attribute"
 );

  // Check the visibility of the enum
  assert!(matches!(ast.vis(), syn ::Visibility ::Public(_)), "Expected public visibility");

  // Check all elements
  let elements: Vec< FieldOrVariant<'_ >> = ast.elements().collect();

  // Check the first variant
  let first_field_or_variant = &elements[0];
  assert_eq!(first_field_or_variant.ident().unwrap().to_string(), "Nothing");

  // Check the attributes of the first variant
  let variant_attrs = first_field_or_variant.attrs();
  assert!(
  variant_attrs.iter().any(|attr| attr.path().is_ident("allow")),
  "Missing allow attribute"
 );

  // Check all variant names
  let variant_names: Vec< String > = elements.iter().map(|elem| elem.ident().unwrap().to_string()).collect();
  assert_eq!(
  variant_names,
  vec!["Nothing", "FromString", "FromBin"],
  "Variant names do not match"
 );

  // Check the types of the variants
  let variant_types: Vec< Option<&syn ::Type >> = elements.iter().map(|elem| elem.typ()).collect();

  // let variant_fields: Vec< syn ::Fields > = ast.elements().map( | e | e.fields() ).collect();
  let variant_fields: Vec< syn ::Fields > = elements.iter().filter_map(|elem| elem.fields().cloned()).collect();
  // dbg!( &variant_types );

  assert_eq!(variant_types.len(), 3, "Expected three variants");
  assert!(variant_types[0].is_none(), "First variant should have no type");

  assert!(variant_types[0].is_none());
  assert!(variant_types[1].is_none());
  assert!(variant_types[2].is_none());

  // tree_print!( variant_fields[1] );
  assert_eq!(
  variant_fields[1].to_token_stream().to_string(),
  "(String)",
  "Second variant should be of type String"
 );
  assert_eq!(
  variant_fields[2].to_token_stream().to_string(),
  "(& 'static [u8])",
  "Third variant should be of type & 'static [u8]"
 );
}