macro-template 0.1.0

Macro to generate repeated Rust code with table-driven inputs.
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// Copyright 2026 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use macro_template::template;

template! {
    for N in 0..3 {
        const _: usize = N;
    }
}

trait TypeTag {
    const TAG: u8;
}

template! {
    for (Ty, Tag) in [
        (bool, 1),
        (usize, 2),
    ] {
        impl TypeTag for Ty {
            const TAG: u8 = Tag;
        }
    }
}

#[test]
fn expands_item_templates() {
    assert_eq!(<bool as TypeTag>::TAG, 1);
    assert_eq!(<usize as TypeTag>::TAG, 2);
}

#[test]
fn expands_statement_templates() {
    let mut total = 0usize;

    template! {
        for (Name, Value) in [
            (first, 1usize),
            (second, 2usize),
        ] {
            let Name = Value;
            total += Name;
        }
    }

    assert_eq!(total, 3);
}

#[test]
fn accepts_trailing_comma_after_single_input_clause() {
    template! {
        for N in [1usize, 2usize],
        {
            const _: usize = N;
        }
    }
}

#[test]
#[allow(clippy::vec_init_then_push)]
fn preserves_grouped_commas_in_single_variable_rows() {
    let mut pairs = vec![];

    template! {
        for Pair in [(1usize, 2usize), (3usize, 4usize)] {
            pairs.push(Pair);
        }
    }

    assert_eq!(pairs, [(1, 2), (3, 4)]);
}

#[test]
#[allow(clippy::vec_init_then_push)]
fn accepts_parenthesized_single_variable_rows() {
    let mut values = vec![];

    template! {
        for Value in [(first), (second)] {
            values.push(stringify!(Value));
        }
    }

    assert_eq!(values, ["(first)", "(second)"]);
}

#[test]
fn preserves_grouped_commas_in_tuple_row_values() {
    let mut pairs = vec![];

    template! {
        for (Name, Pair) in [
            (first, (1usize, 2usize)),
            (second, (3usize, 4usize)),
        ] {
            let Name = Pair;
            pairs.push(Name);
        }
    }

    assert_eq!(pairs, [(1, 2), (3, 4)]);
}

#[test]
fn expands_hash_paren_splice_without_repeating_surrounding_tokens() {
    template! {
        for N in 0..=2 {
            let values = [100usize, #(N),*, 200usize];
        }
    }

    assert_eq!(values, [100, 0, 1, 2, 200]);
}

#[test]
#[allow(clippy::vec_init_then_push)]
fn expands_hash_paren_splice_with_statements() {
    let mut values = vec![100usize];

    template! {
        for N in 0..=2 {
            #( values.push(N); )*
            values.push(200usize);
        }
    }

    assert_eq!(values, [100, 0, 1, 2, 200]);
}

#[test]
fn expands_hash_paren_splice_with_separator() {
    template! {
        for N in 0..=2 {
            let values = [#(N),*];
        }
    }

    assert_eq!(values, [0, 1, 2]);
}

#[test]
fn accepts_token_tree_as_hash_paren_splice_separator() {
    macro_rules! stringify_tokens {
        ($($tokens:tt)*) => {
            stringify!($($tokens)*)
        };
    }

    template! {
        for N in [first, second] {
            let tokens = stringify_tokens! { #(N)(separator)* };
        }
    }

    assert_eq!(tokens, "first(separator) second");
}

template! {
    for Variant in [First, Second] {
        #[derive(Debug, PartialEq, Eq)]
        enum SpliceEnum {
            #(Variant),*,
            Other,
        }
    }
}

#[test]
fn expands_splice_in_item_groups() {
    assert_eq!(format!("{:?}", SpliceEnum::First), "First");
    assert_eq!(format!("{:?}", SpliceEnum::Second), "Second");
    assert_eq!(format!("{:?}", SpliceEnum::Other), "Other");
}

template! {
    for (Name, Variant) in [
        (IgnoredOne, Alpha),
        (IgnoredTwo, Beta),
    ] {
        #[derive(Debug, PartialEq, Eq)]
        enum Name {
            #(Variant),*
        }
    }
}

#[test]
fn preserves_outer_variable_tokens_in_splice_templates() {
    assert_eq!(format!("{:?}", Name::Alpha), "Alpha");
    assert_eq!(format!("{:?}", Name::Beta), "Beta");
}

#[test]
fn preserves_hash_paren_non_repetition_for_downstream_macros() {
    macro_rules! stringify_tokens {
        ($($tokens:tt)*) => {
            stringify!($($tokens)*)
        };
    }

    template! {
        for N in [0] {
            let tokens = stringify_tokens! { #(N)+ };
        }
    }

    assert_eq!(tokens, "# (0) +");
}

#[test]
fn preserves_at_ident_for_downstream_macros() {
    macro_rules! stringify_tokens {
        ($($tokens:tt)*) => {
            stringify!($($tokens)*)
        };
    }

    template! {
        for N in [0] {
            let tokens = stringify_tokens! { @N };
        }
    }

    assert_eq!(tokens, "@ 0");
}

#[test]
fn preserves_bare_at_brace_for_downstream_macros() {
    macro_rules! stringify_tokens {
        ($($tokens:tt)*) => {
            stringify!($($tokens)*)
        };
    }

    template! {
        for N in [0] {
            let tokens = stringify_tokens! { @{ N } };
        }
    }

    assert_eq!(tokens, "@ { 0 }");
}

#[test]
fn expands_match_arms_from_splice() {
    fn parse_keyword(text: &str) -> Option<u8> {
        template! {
            for (Pat, Value) in [
                ("async", 1u8),
                ("await", 2u8),
            ] {
                match text {
                    #(Pat => Some(Value)),*,
                    _ => None,
                }
            }
        }
    }

    assert_eq!(parse_keyword("async"), Some(1));
    assert_eq!(parse_keyword("await"), Some(2));
    assert_eq!(parse_keyword("fn"), None);
}

#[test]
fn treats_fat_arrow_as_plain_row_tokens() {
    fn classify(value: u8) -> Option<&'static str> {
        template! {
            for Arm in [0 => Some("zero"), 1 => Some("one")] {
                match value {
                    #(Arm),*,
                    _ => None,
                }
            }
        }
    }

    assert_eq!(classify(0), Some("zero"));
    assert_eq!(classify(1), Some("one"));
    assert_eq!(classify(2), None);
}

#[test]
fn expands_integer_range_input_for_tuple_fields() {
    let tuple = (1usize, 2usize, 3usize);
    let mut sum = 0usize;

    template! {
        for N in 0..=2 {
            sum += tuple.N;
        }
    }

    assert_eq!(sum, 6);
}

#[test]
#[allow(clippy::vec_init_then_push)]
fn expands_reversed_range_input() {
    let mut values = vec![];

    template! {
        for N in (0..=3).rev() {
            values.push(N);
        }
    }

    assert_eq!(values, [3, 2, 1, 0]);
}

#[test]
#[allow(clippy::vec_init_then_push)]
fn expands_character_range_input() {
    let mut chars = vec![];

    template! {
        for C in 'a'..='c' {
            chars.push(C);
        }
    }

    assert_eq!(chars, ['a', 'b', 'c']);
}

#[test]
#[allow(clippy::vec_init_then_push)]
fn expands_byte_range_input() {
    let mut bytes = Vec::<u8>::new();

    template! {
        for B in b'x'..=b'z' {
            bytes.push(B);
        }
    }

    assert_eq!(bytes, b"xyz");
}

#[test]
#[allow(clippy::vec_init_then_push)]
fn preserves_integer_range_radix() {
    let mut lower_hex = vec![];
    template! {
        for N in 0x08..=0x0b {
            lower_hex.push(stringify!(N));
        }
    }
    assert_eq!(lower_hex, ["0x08", "0x09", "0x0a", "0x0b"]);

    let mut upper_hex = vec![];
    template! {
        for N in 0x08..=0x0B {
            upper_hex.push(stringify!(N));
        }
    }
    assert_eq!(upper_hex, ["0x08", "0x09", "0x0A", "0x0B"]);

    let mut upper_hex_prefix = vec![];
    template! {
        for N in 0X09..0X10 {
            upper_hex_prefix.push(stringify!(N));
        }
    }
    assert_eq!(
        upper_hex_prefix,
        ["0x09", "0x0A", "0x0B", "0x0C", "0x0D", "0x0E", "0x0F"]
    );

    let mut binary = vec![];
    template! {
        for N in 0b001..=0b011 {
            binary.push(stringify!(N));
        }
    }
    assert_eq!(binary, ["0b001", "0b010", "0b011"]);

    let mut octal = vec![];
    template! {
        for N in 0o06..=0o10 {
            octal.push(stringify!(N));
        }
    }
    assert_eq!(octal, ["0o06", "0o07", "0o10"]);
}

#[test]
#[allow(clippy::vec_init_then_push)]
fn preserves_integer_range_padding() {
    let mut decimal = vec![];
    template! {
        for N in 098..=100 {
            decimal.push(stringify!(N));
        }
    }
    assert_eq!(decimal, ["098", "099", "100"]);

    let mut padded_start = vec![];
    template! {
        for N in 00..=03 {
            padded_start.push(stringify!(N));
        }
    }
    assert_eq!(padded_start, ["00", "01", "02", "03"]);

    let mut padded_end = vec![];
    template! {
        for N in 008..=010 {
            padded_end.push(stringify!(N));
        }
    }
    assert_eq!(padded_end, ["008", "009", "010"]);

    let mut padded_hex_start = vec![];
    template! {
        for N in 0x0008..=0x000A {
            padded_hex_start.push(stringify!(N));
        }
    }
    assert_eq!(padded_hex_start, ["0x0008", "0x0009", "0x000A"]);

    let mut seq_macro_style_hex_padding = vec![];
    template! {
        for N in 0x000..=0x00F {
            seq_macro_style_hex_padding.push(stringify!(N));
        }
    }
    assert_eq!(
        seq_macro_style_hex_padding,
        [
            "0x000", "0x001", "0x002", "0x003", "0x004", "0x005", "0x006", "0x007", "0x008",
            "0x009", "0x00A", "0x00B", "0x00C", "0x00D", "0x00E", "0x00F",
        ]
    );
}

#[test]
#[allow(clippy::vec_init_then_push)]
fn uses_narrower_padding_width_when_only_one_bound_is_padded() {
    let mut one_sided_padding = vec![];
    template! {
        for N in 00..=3 {
            one_sided_padding.push(stringify!(N));
        }
    }
    assert_eq!(one_sided_padding, ["0", "1", "2", "3"]);

    let mut one_sided_hex_padding = vec![];
    template! {
        for N in 0x0008..=0x0A {
            one_sided_hex_padding.push(stringify!(N));
        }
    }
    assert_eq!(one_sided_hex_padding, ["0x08", "0x09", "0x0A"]);
}

#[test]
#[allow(clippy::vec_init_then_push)]
fn preserves_integer_range_suffix() {
    let mut values = Vec::<u16>::new();

    template! {
        for N in 0u16..=2u16 {
            values.push(N);
        }
    }

    assert_eq!(values, [0, 1, 2]);
}

#[test]
#[allow(clippy::vec_init_then_push)]
fn accepts_literal_range_bounds_from_macro_rules() {
    macro_rules! collect_range {
        ($end:literal) => {{
            let mut values = Vec::<usize>::new();
            template! {
                for N in 0..$end {
                    values.push(N);
                }
            }
            values
        }};
    }

    assert_eq!(collect_range!(4usize), [0, 1, 2, 3]);
}

trait Kernel<T> {
    fn run(input: T) -> T;
}

struct Cpu;
struct Gpu;

template! {
    for Backend in [Cpu, Gpu],
    for Ty in [f32, f64],
    {
        impl Kernel<Ty> for Backend {
            fn run(input: Ty) -> Ty {
                input
            }
        }
    }
}

#[test]
fn expands_cartesian_product_items() {
    assert_eq!(<Cpu as Kernel<f32>>::run(1.0), 1.0);
    assert_eq!(<Cpu as Kernel<f64>>::run(1.0), 1.0);
    assert_eq!(<Gpu as Kernel<f32>>::run(1.0), 1.0);
    assert_eq!(<Gpu as Kernel<f64>>::run(1.0), 1.0);
}

struct DatabasesView;
struct SchemasView;

trait SystemView<T> {
    const NAME: &'static str;
}

template! {
    for (Variant, View) in [
        (Databases, DatabasesView),
        (Schemas, SchemasView),
    ],
    for Ty in [u8, u16] {
        impl SystemView<Ty> for View {
            const NAME: &'static str = stringify!(Variant);
        }
    }
}

#[test]
fn combines_tuple_rows_and_list_inputs() {
    assert_eq!(<DatabasesView as SystemView<u8>>::NAME, "Databases");
    assert_eq!(<DatabasesView as SystemView<u16>>::NAME, "Databases");
    assert_eq!(<SchemasView as SystemView<u8>>::NAME, "Schemas");
    assert_eq!(<SchemasView as SystemView<u16>>::NAME, "Schemas");
}

struct Login;
struct Data;

trait MessageCode<const VERSION: usize> {
    const CODE: u16;
}

template! {
    for (Message, BaseCode) in [
        (Login, 0x1000u16),
        (Data, 0x2000u16),
    ],
    for Version in 1..=2 {
        impl MessageCode<Version> for Message {
            const CODE: u16 = BaseCode + Version;
        }
    }
}

#[test]
fn combines_tuple_rows_and_range_inputs() {
    assert_eq!(<Login as MessageCode<1>>::CODE, 0x1001);
    assert_eq!(<Login as MessageCode<2>>::CODE, 0x1002);
    assert_eq!(<Data as MessageCode<1>>::CODE, 0x2001);
    assert_eq!(<Data as MessageCode<2>>::CODE, 0x2002);
}

#[test]
fn expands_splice_over_cartesian_rows() {
    template! {
        for Left in [1usize, 2usize],
        for Right in [10usize, 20usize] {
            const PAIRS: &[(usize, usize)] = &[
                #((Left, Right)),*
            ];
        }
    }

    assert_eq!(PAIRS, [(1, 10), (1, 20), (2, 10), (2, 20)]);
}

#[test]
fn works_with_paste_for_range_ident_pasting() {
    template! {
        for N in 64..=66 {
            paste::paste! {
                #[derive(Debug, PartialEq, Eq)]
                enum Demo {
                    #( [<Variant N>], )*
                }
            }
        }
    }

    assert_eq!(format!("{:?}", Demo::Variant64), "Variant64");
    assert_eq!(format!("{:?}", Demo::Variant65), "Variant65");
    assert_eq!(format!("{:?}", Demo::Variant66), "Variant66");
}

#[test]
fn works_with_paste_for_padded_decimal_ident_pasting() {
    template! {
        for P in 000..=002 {
            paste::paste! {
                #( struct [<Pin P>]; )*
            }
        }
    }

    let _ = (Pin000, Pin001, Pin002);
}

#[test]
fn works_with_paste_for_stripped_hex_prefix_ident_pasting() {
    template! {
        for P in (0x00A..=0x00C).rev().strip_prefix() {
            paste::paste! {
                enum Pins {
                    #( [<Pin P>], )*
                }
            }
        }
    }

    assert_eq!(Pins::Pin00C as usize, 0);
    assert_eq!(Pins::Pin00B as usize, 1);
    assert_eq!(Pins::Pin00A as usize, 2);
}

#[test]
#[allow(clippy::vec_init_then_push)]
fn expands_statement_cartesian_product() {
    let mut values = vec![];

    template! {
        for Prefix in ["read", "write"],
        for Code in 200..=201 {
            values.push((Prefix, Code));
        }
    }

    assert_eq!(
        values,
        [("read", 200), ("read", 201), ("write", 200), ("write", 201),]
    );
}