ushell_dispatcher 0.1.0

Procedural macros for generating command and shortcut dispatchers
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
#![allow(non_snake_case)]

//! # Command dispatcher generator macro
//!
//! This crate generates a no_std, zero-heap command dispatcher from a compact descriptor or mapping.
//!
//! ## Usage
//! - Accepts a module name and a descriptor string or mapping.
//! - Generates a module with a dispatcher, tokenizer, and helpers.
//!
//! ## Descriptor Table
//!
//! Each character in a descriptor represents one parameter type:

//! +------+-------+   +------+------+   +------+------+   +------+------+   +------+------+
//! | Char | Type  |   | Char | Type |   | Char | Type |   | Char | Type |   | Char | Type |
//! +------+-------+   +------+------+   +------+------+   +------+------+   +------+------+
//!
//! +------+-------+   +------+------+   +------+------+   +------+------+   +------+------+
//! | B    | u8    |   | W    | u16  |   | D    | u32  |   | Q    | u64  |   | X    | u128 |
//! +------+-------+   +------+------+   +------+------+   +------+------+   +------+------+
//! | b    | i8    |   | w    | i16  |   | d    | i32  |   | q    | i64  |   | x    | i128 |
//! +------+-------+   +------+------+   +------+------+   +------+------+   +------+------+
//!
//! +------+-------+   +------+------+   +------+------+   +------+------+   +------+------+
//! | Z    | usize |   | F    | f32  |   | c    | char |   | b    | bool |   | v    | void |
//! +------+-------+   +------+------+   +------+------+   +------+------+   +------+------+
//! | z    | isize |   | f    | f64  |   | s    | &str |   | h    | &[u8]|
//!+------+-------+   +------+------+   +------+------+   +------+------+
//!
//! Examples:
//! - "DdFsb" => arguments: u32, i32, f64, &str, bool
//! - "t"     => argument: bool
//! - "v"     => argument: void
//!
//! ## Macro Input Format
//! - DSL: `generate_commands_dispatcher!(mod m; \"dFs: path::to::f1 path::to::f2, t: path::to::f3\");`
//!
//! * Tokenization splits a command line into tokens, respecting **double quotes** for `&str`.
//! * `dispatch(line)` parses the function name + arguments, checks **arity**, parses into a stack
//!   `CallCtx`, and invokes the registered function.
//! * No heap allocations are performed; buffers are compile-time sized from maximums inferred
//!   across all descriptors.
//! ## no_std
//! - Uses `core` only; suitable for embedded/stack-only use.
//!
//! `DispatchError` reports: `Empty`, `UnknownFunction`, `WrongArity` and per-type parsing errors:
//! `BadBool`, `BadChar`, `BadUnsigned`, `BadSigned`, `BadFloat`.
//!
use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{format_ident, quote};
use syn::{Ident, LitStr, Result, Token, parse::Parse, parse_macro_input};

/// A std-like alias used locally during macro input parsing.
type StdResult<T, E> = std::result::Result<T, E>;

/// Per-descriptor maximum counts of each primitive (used to size `CallCtx`).
#[derive(Default, Clone, Copy)]
struct HostCounts {
    // unsigned ints
    u8_c: usize,
    u16_c: usize,
    u32_c: usize,
    u64_c: usize,
    u128_c: usize,

    // signed ints
    i8_c: usize,
    i16_c: usize,
    i32_c: usize,
    i64_c: usize,
    i128_c: usize,

    // sized ints
    usize_c: usize,
    isize_c: usize,

    // floats
    f32_c: usize,
    f64_c: usize,

    // others
    bool_c: usize,
    char_c: usize,
    str_c: usize,

    // hexstring AABBF3C6 => [170, 187, 243, 198]
    hexstr_c: usize,
}

/// Component-wise maximum between two `HostCounts`.
fn host_counts_max(a: HostCounts, b: HostCounts) -> HostCounts {
    macro_rules! m {
        ($f:ident) => {
            if a.$f > b.$f { a.$f } else { b.$f }
        };
    }
    HostCounts {
        u8_c: m!(u8_c),
        u16_c: m!(u16_c),
        u32_c: m!(u32_c),
        u64_c: m!(u64_c),
        u128_c: m!(u128_c),
        i8_c: m!(i8_c),
        i16_c: m!(i16_c),
        i32_c: m!(i32_c),
        i64_c: m!(i64_c),
        i128_c: m!(i128_c),
        usize_c: m!(usize_c),
        isize_c: m!(isize_c),
        f32_c: m!(f32_c),
        f64_c: m!(f64_c),
        bool_c: m!(bool_c),
        char_c: m!(char_c),
        str_c: m!(str_c),
        hexstr_c: m!(hexstr_c),
    }
}

/// Parsed macro input: `mod <ident>;` followed by either a DSL `LitStr`
struct CommandMacroInput {
    mod_ident: Ident,               // Module identifier for the generated dispatcher
    body: LitStr,                   // Macro input body as string
    hexstr_size: Option<syn::Expr>, // Optional size for hexstr buffers
}

/// Implementation for CommandMacroInput structure
impl Parse for CommandMacroInput {
    fn parse(input: syn::parse::ParseStream) -> Result<Self> {
        // Expect: `mod <ident>;`
        input.parse::<Token![mod]>()?;
        let mod_ident: Ident = input.parse()?;
        input.parse::<Token![;]>()?;

        // Optionally parse hexstr_size = <expr>;
        let hexstr_size = if input.peek(syn::Ident) && input.peek2(Token![=]) {
            let key: Ident = input.parse()?;
            if key == "hexstr_size" {
                input.parse::<Token![=]>()?;
                let expr: syn::Expr = input.parse()?;
                input.parse::<Token![;]>()?;
                Some(expr)
            } else {
                return Err(syn::Error::new(
                    key.span(),
                    "Unexpected identifier, expected 'hexstr_size'",
                ));
            }
        } else {
            None
        };

        let body: LitStr = input.parse()?;
        Ok(CommandMacroInput {
            mod_ident,
            hexstr_size,
            body,
        })
    }
}

/// Generate a no-heap dispatcher module from a DSL mapping.
pub fn generate_dispatcher_from_dsl(input: TokenStream) -> TokenStream {
    let CommandMacroInput {
        mod_ident,
        body,
        hexstr_size,
    } = parse_macro_input!(input as CommandMacroInput);

    // Collect (descriptor, [paths]) pairs from either the DSL

    let mut pairs: Vec<(String, Vec<syn::Path>)> = {
        let s = body.value();
        let mut acc = Vec::new();
        for group in s.split(',') {
            let grp = group.trim();
            if grp.is_empty() {
                continue;
            }
            let (desc, names) = match grp.split_once(':') {
                Some((d, r)) => (d.trim(), r.trim()),
                None => continue,
            };
            if desc.is_empty() || names.is_empty() {
                continue;
            }
            let desc_str = desc.to_string();
            let funcs: StdResult<Vec<_>, _> = names
                .split_whitespace()
                .map(syn::parse_str::<syn::Path>)
                .collect();
            let funcs = match funcs {
                Ok(v) => v,
                Err(_) => continue,
            };
            acc.push((desc_str, funcs));
        }
        acc
    };

    // Deduplicate descriptors, assign indices, gather entries; stable sort by function name.
    let mut unique_desc: Vec<String> = Vec::new();
    let mut entries: Vec<FnEntry> = Vec::new();
    for (desc, funcs) in pairs.drain(..) {
        let idx = match unique_desc.iter().position(|x| x == &desc) {
            Some(i) => i,
            None => {
                unique_desc.push(desc.clone());
                unique_desc.len() - 1
            }
        };
        for p in funcs {
            let name_str = path_last_ident(&p).unwrap_or_else(|| "unknown".into());
            entries.push(FnEntry {
                name_str,
                path: p,
                spec: desc.clone(),
                spec_idx: idx,
            });
        }
    }

    // Stable sort entries by function name
    entries.sort_by(|a, b| a.name_str.cmp(&b.name_str));

    // Get the largest name for a function
    let function_name_max_len = entries.iter().map(|e| e.name_str.len()).max().unwrap_or(0) + 1;

    // Human-readable registry of function names for diagnostics/UI.
    let fn_names: Vec<LitStr> = entries
        .iter()
        .map(|e| LitStr::new(&e.name_str, Span::call_site()))
        .collect();

    // Generated registry function
    let registry_fn = quote! {
        /// Return function names in the generated table (sorted).
        pub fn get_function_names() -> Vec<&'static str> {
            vec![ #( #fn_names ),* ]
        }
    };

    // Compute per-spec counts for each primitive type and the overall max arity.
    let mut max_counts = HostCounts::default();
    let mut max_arity: usize = 0;

    for desc in &unique_desc {
        let mut c = HostCounts::default();
        for ch in desc.chars() {
            match ch {
                // unsigned (lowercase)
                'B' => c.u8_c += 1,   // u8
                'W' => c.u16_c += 1,  // u16
                'D' => c.u32_c += 1,  // u32
                'Q' => c.u64_c += 1,  // u64
                'X' => c.u128_c += 1, // u128

                // signed (uppercase)
                'b' => c.i8_c += 1,   // i8
                'w' => c.i16_c += 1,  // i16
                'd' => c.i32_c += 1,  // i32
                'q' => c.i64_c += 1,  // i64
                'x' => c.i128_c += 1, // i128

                // sized
                'Z' => c.usize_c += 1, // usize
                'z' => c.isize_c += 1, // isize

                // floats
                'f' => c.f32_c += 1, // f32
                'F' => c.f64_c += 1, // f64

                // bool, char, string, hexstring
                't' => c.bool_c += 1,   // bool
                'c' => c.char_c += 1,   // char
                's' => c.str_c += 1,    // &str
                'h' => c.hexstr_c += 1, // hex &str

                // void
                'v' => {}
                _ => {}
            }
        }

        let arity = if desc == "v" {
            0
        } else {
            c.u8_c
                + c.u16_c
                + c.u32_c
                + c.u64_c
                + c.u128_c
                + c.i8_c
                + c.i16_c
                + c.i32_c
                + c.i64_c
                + c.i128_c
                + c.usize_c
                + c.isize_c
                + c.f32_c
                + c.f64_c
                + c.bool_c
                + c.char_c
                + c.str_c
                + c.hexstr_c
        };

        if arity > max_arity {
            max_arity = arity;
        }
        max_counts = host_counts_max(max_counts, c);
    }

    // Keep raw descriptor strings for diagnostics in the generated module.
    let param_specs: Vec<LitStr> = unique_desc
        .iter()
        .map(|s| LitStr::new(s, Span::call_site()))
        .collect();
    let param_specs_len = param_specs.len();

    // Generate maximals as constants
    let max_u8 = max_counts.u8_c;
    let max_u16 = max_counts.u16_c;
    let max_u32 = max_counts.u32_c;
    let max_u64 = max_counts.u64_c;
    let max_u128 = max_counts.u128_c;
    let max_i8 = max_counts.i8_c;
    let max_i16 = max_counts.i16_c;
    let max_i32 = max_counts.i32_c;
    let max_i64 = max_counts.i64_c;
    let max_i128 = max_counts.i128_c;
    let max_usize = max_counts.usize_c;
    let max_isize = max_counts.isize_c;
    let max_f32 = max_counts.f32_c;
    let max_f64 = max_counts.f64_c;
    let max_bool = max_counts.bool_c;
    let max_char = max_counts.char_c;
    let max_str = max_counts.str_c;
    let max_hexstr = max_counts.hexstr_c;
    let max_arity_num = max_arity;

    // Generate per-descriptor parsers that fill `CallCtx` from `&[&str]`.
    let mut parsers: Vec<TokenStream2> = Vec::new();
    for (sid, spec) in unique_desc.iter().enumerate() {
        let fn_ident = format_ident!("__parse_spec_{}", sid);
        let header = quote! {
            // `k` indexes into the argument tokens slice; individual idx_* track per-type positions.
            let mut k = 0usize;
            // per-type indices
            let mut idx_b=0usize; let mut idx_w=0usize; let mut idx_d=0usize; let mut idx_q=0usize; let mut idx_x=0usize;
            let mut idx_B=0usize; let mut idx_W=0usize; let mut idx_D=0usize; let mut idx_Q=0usize; let mut idx_X=0usize;
            let mut idx_z=0usize; let mut idx_Z=0usize;
            let mut idx_f=0usize; let mut idx_F=0usize;
            let mut idx_t=0usize; let mut idx_c=0usize; let mut idx_s=0usize; let mut idx_h=0usize;
        };

        let mut stmts: Vec<TokenStream2> = Vec::new();
        for ch in spec.chars() {
            let stmt = match ch {
                // unsigned
                'B' => {
                    quote! { ctx.u8s   [idx_b] = parse_u8   (args[k]).ok_or(DispatchError::BadUnsigned)?; idx_b+=1; k+=1; }
                }
                'W' => {
                    quote! { ctx.u16s  [idx_w] = parse_u16  (args[k]).ok_or(DispatchError::BadUnsigned)?; idx_w+=1; k+=1; }
                }
                'D' => {
                    quote! { ctx.u32s  [idx_d] = parse_u32  (args[k]).ok_or(DispatchError::BadUnsigned)?; idx_d+=1; k+=1; }
                }
                'Q' => {
                    quote! { ctx.u64s  [idx_q] = parse_u64  (args[k]).ok_or(DispatchError::BadUnsigned)?; idx_q+=1; k+=1; }
                }
                'X' => {
                    quote! { ctx.u128s [idx_x] = parse_u128 (args[k]).ok_or(DispatchError::BadUnsigned)?; idx_x+=1; k+=1; }
                }
                // signed
                'b' => {
                    quote! { ctx.i8s   [idx_B] = parse_i8   (args[k]).ok_or(DispatchError::BadSigned  )?; idx_B+=1; k+=1; }
                }
                'w' => {
                    quote! { ctx.i16s  [idx_W] = parse_i16  (args[k]).ok_or(DispatchError::BadSigned  )?; idx_W+=1; k+=1; }
                }
                'd' => {
                    quote! { ctx.i32s  [idx_D] = parse_i32  (args[k]).ok_or(DispatchError::BadSigned  )?; idx_D+=1; k+=1; }
                }
                'q' => {
                    quote! { ctx.i64s  [idx_Q] = parse_i64  (args[k]).ok_or(DispatchError::BadSigned  )?; idx_Q+=1; k+=1; }
                }
                'x' => {
                    quote! { ctx.i128s [idx_X] = parse_i128 (args[k]).ok_or(DispatchError::BadSigned  )?; idx_X+=1; k+=1; }
                }
                // sized
                'Z' => {
                    quote! { ctx.usizes[idx_z] = parse_usize(args[k]).ok_or(DispatchError::BadUnsigned)?; idx_z+=1; k+=1; }
                }
                'z' => {
                    quote! { ctx.isizes[idx_Z] = parse_isize(args[k]).ok_or(DispatchError::BadSigned  )?; idx_Z+=1; k+=1; }
                }
                // floats
                'f' => {
                    quote! { ctx.f32s  [idx_f] = parse_f::<f32  >(args[k]).ok_or(DispatchError::BadFloat)?; idx_f+=1; k+=1; }
                }
                'F' => {
                    quote! { ctx.f64s  [idx_F] = parse_f::<f64  >(args[k]).ok_or(DispatchError::BadFloat)?; idx_F+=1; k+=1; }
                }
                //  bool, char, string, hexstring
                't' => {
                    quote! { ctx.bools [idx_t] = parse_bool(args[k]).ok_or(DispatchError::BadBool)?; idx_t+=1; k+=1; }
                }
                'c' => {
                    quote! { ctx.chars [idx_c] = parse_char(args[k]).ok_or(DispatchError::BadChar)?; idx_c+=1; k+=1; }
                }
                's' => quote! { ctx.strs  [idx_s] = args[k]; idx_s+=1; k+=1; },
                'h' => {
                    quote! { ctx.hexstrs[idx_h]= parse_hexstr(args[k]).ok_or(DispatchError::BadHexStr)?; idx_h+=1; k+=1; }
                }
                _ => quote! {},
            };
            stmts.push(stmt);
        }
        parsers.push(quote! {

            /// Parse arguments for this descriptor into `CallCtx`.
            #[inline(always)]
            fn #fn_ident<'a>(ctx: &mut CallCtx<'a>, args: &[&'a str]) -> Result<(), DispatchError> {
                #header
                #(#stmts)*
                Ok(())
            }
        });
    }

    // Generate per-function wrappers and entries + match arms for lookup
    let mut wrappers: Vec<TokenStream2> = Vec::new();
    let mut entry_inits: Vec<TokenStream2> = Vec::new();
    let mut match_arms: Vec<TokenStream2> = Vec::new();

    // Pairs of (function name, descriptor) for diagnostics / UI
    let name_spec_pairs: Vec<TokenStream2> = entries
        .iter()
        .map(|e| {
            let name_lit = LitStr::new(&e.name_str, Span::call_site());
            let spec_lit = LitStr::new(&e.spec, Span::call_site());
            quote! { (#name_lit, #spec_lit) }
        })
        .collect();

    for (pos, e) in entries.iter().enumerate() {
        let name_lit = LitStr::new(&e.name_str, Span::call_site());
        let spec_str = &e.spec;
        //let arity_u8 = (spec_str.chars().count()) as u8;
        let arity_u8 = if spec_str == "v" {
            0
        } else {
            spec_str.chars().count() as u8
        };
        let wrapper_ident = format_ident!("__call_{}", sanitize_ident(&e.name_str));
        let path = &e.path;
        let spec_idx_u16 = e.spec_idx as u16;
        let parser_ident = format_ident!("__parse_spec_{}", e.spec_idx);

        // Build type list and extraction expressions according to the descriptor order.
        let mut arg_types: Vec<TokenStream2> = Vec::new();
        let mut arg_exprs: Vec<TokenStream2> = Vec::new();
        let mut idx_b = 0usize;
        let mut idx_w = 0usize;
        let mut idx_d = 0usize;
        let mut idx_q = 0usize;
        let mut idx_x = 0usize;
        let mut idx_B = 0usize;
        let mut idx_W = 0usize;
        let mut idx_D = 0usize;
        let mut idx_Q = 0usize;
        let mut idx_X = 0usize;
        let mut idx_z = 0usize;
        let mut idx_Z = 0usize;
        let mut idx_f = 0usize;
        let mut idx_F = 0usize;
        let mut idx_t = 0usize;
        let mut idx_c = 0usize;
        let mut idx_s = 0usize;
        let mut idx_h = 0usize;

        for ch in spec_str.chars() {
            match ch {
                // unsigned
                'B' => {
                    arg_types.push(quote! { u8    });
                    arg_exprs.push(quote! { ctx.u8s    [#idx_b] });
                    idx_b += 1;
                }
                'W' => {
                    arg_types.push(quote! { u16   });
                    arg_exprs.push(quote! { ctx.u16s   [#idx_w] });
                    idx_w += 1;
                }
                'D' => {
                    arg_types.push(quote! { u32   });
                    arg_exprs.push(quote! { ctx.u32s   [#idx_d] });
                    idx_d += 1;
                }
                'Q' => {
                    arg_types.push(quote! { u64   });
                    arg_exprs.push(quote! { ctx.u64s   [#idx_q] });
                    idx_q += 1;
                }
                'X' => {
                    arg_types.push(quote! { u128  });
                    arg_exprs.push(quote! { ctx.u128s  [#idx_x] });
                    idx_x += 1;
                }

                // signed
                'b' => {
                    arg_types.push(quote! { i8    });
                    arg_exprs.push(quote! { ctx.i8s    [#idx_B] });
                    idx_B += 1;
                }
                'w' => {
                    arg_types.push(quote! { i16   });
                    arg_exprs.push(quote! { ctx.i16s   [#idx_W] });
                    idx_W += 1;
                }
                'd' => {
                    arg_types.push(quote! { i32   });
                    arg_exprs.push(quote! { ctx.i32s   [#idx_D] });
                    idx_D += 1;
                }
                'q' => {
                    arg_types.push(quote! { i64   });
                    arg_exprs.push(quote! { ctx.i64s   [#idx_Q] });
                    idx_Q += 1;
                }
                'x' => {
                    arg_types.push(quote! { i128  });
                    arg_exprs.push(quote! { ctx.i128s  [#idx_X] });
                    idx_X += 1;
                }

                // sized
                'Z' => {
                    arg_types.push(quote! { usize });
                    arg_exprs.push(quote! { ctx.usizes [#idx_z] });
                    idx_z += 1;
                }
                'z' => {
                    arg_types.push(quote! { isize });
                    arg_exprs.push(quote! { ctx.isizes [#idx_Z] });
                    idx_Z += 1;
                }

                // floats
                'f' => {
                    arg_types.push(quote! { f32   });
                    arg_exprs.push(quote! { ctx.f32s   [#idx_f] });
                    idx_f += 1;
                }
                'F' => {
                    arg_types.push(quote! { f64   });
                    arg_exprs.push(quote! { ctx.f64s   [#idx_F] });
                    idx_F += 1;
                }

                // others
                't' => {
                    arg_types.push(quote! { bool  });
                    arg_exprs.push(quote! { ctx.bools  [#idx_t] });
                    idx_t += 1;
                }
                'c' => {
                    arg_types.push(quote! { char  });
                    arg_exprs.push(quote! { ctx.chars  [#idx_c] });
                    idx_c += 1;
                }
                's' => {
                    arg_types.push(quote! { &str  });
                    arg_exprs.push(quote! { ctx.strs   [#idx_s] });
                    idx_s += 1;
                }
                'h' => {
                    arg_types.push(quote! { &[u8] });
                    arg_exprs.push(quote! { &ctx.hexstrs[#idx_h] });
                    idx_h += 1;
                }
                _ => {}
            }
        }

        // Compile-time signature check: ensures `path` has the expected arity/types.
        let sig_check = {
            let fn_type = quote! { fn(#(#arg_types),*) -> _ };
            quote! {
                const _: fn() = || {
                    let _check: #fn_type = #path;
                    let _ = _check;
                };
            }
        };

        wrappers.push(quote! {
            #sig_check

            /// Wrapper that extracts arguments from `CallCtx` and calls the target function.
            #[inline(always)]
            fn #wrapper_ident<'__ctx>(ctx: &mut CallCtx<'__ctx>, _av: ArgsView<'__ctx>) -> Result<(), DispatchError> {
                let _ = #path( #(#arg_exprs),* );
                Ok(())
            }
        });

        entry_inits.push(quote! {
            Entry {
                name: #name_lit,
                arity: #arity_u8,
                parser: #parser_ident,
                caller: #wrapper_ident,
                spec_idx: #spec_idx_u16,
            }
        });

        match_arms.push(quote! { #name_lit => Some(&ENTRIES[#pos]), });
    }

    let max_hexstr_len_expr = if let Some(expr) = &hexstr_size {
        quote! { #expr }
    } else {
        // Emit a compile error at macro expansion time
        return syn::Error::new(
            Span::call_site(),
            "You must provide `hexstr_size = ...;` in the macro input.",
        )
        .to_compile_error()
        .into();
    };

    let out = quote! {
        #[allow(dead_code)]
        #[allow(non_snake_case, non_camel_case_types, unused_imports)]
        pub mod #mod_ident {

            //! Generated by `generate_commands_dispatcher!`. See the macro docs for usage and the descriptor table.
            extern crate core;

            // Macro and parse functions for integer parsing with base detection
            macro_rules! parse_int {
                ($name:ident, $ty:ty) => {
                    fn $name(s: &str) -> Option<$ty> {
                        let s = s.trim();
                        if let Some(stripped) = s.strip_prefix("0x") {
                            <$ty>::from_str_radix(stripped, 16).ok()
                        } else if let Some(stripped) = s.strip_prefix("0o") {
                            <$ty>::from_str_radix(stripped, 8).ok()
                        } else if let Some(stripped) = s.strip_prefix("0b") {
                            <$ty>::from_str_radix(stripped, 2).ok()
                        } else {
                            s.parse::<$ty>().ok()
                        }
                    }
                };
            }

            parse_int!(parse_u8, u8);
            parse_int!(parse_u16, u16);
            parse_int!(parse_u32, u32);
            parse_int!(parse_u64, u64);
            parse_int!(parse_u128, u128);

            parse_int!(parse_i8, i8);
            parse_int!(parse_i16, i16);
            parse_int!(parse_i32, i32);
            parse_int!(parse_i64, i64);
            parse_int!(parse_i128, i128);

            parse_int!(parse_usize, usize);
            parse_int!(parse_isize, isize);

            /// All unique parameter descriptors encountered (for diagnostics/UIs).
            pub static PARAM_SPECS: [&'static str; #param_specs_len] = [ #( #param_specs ),* ];

            /// Descriptor character to Rust type mapping (for help/diagnostics).
            pub static DESCRIPTOR_HELP: &str = "B:u8   | W:u16  | D:u32 | Q:u64 | X:u128 | Z:usize | F:f64\nb:i8   | w:i16  | d:i32 | q:i64 | x:i128 | z:isize | f:f32\nv:void | c:char | s:str | t:bool | h:hexstr\n";

            /// Maximum counts per primitive across all descriptors. These sizes define the
            pub const MAX_U8:    usize = #max_u8;
            pub const MAX_U16:   usize = #max_u16;
            pub const MAX_U32:   usize = #max_u32;
            pub const MAX_U64:   usize = #max_u64;
            pub const MAX_U128:  usize = #max_u128;

            pub const MAX_I8:    usize = #max_i8;
            pub const MAX_I16:   usize = #max_i16;
            pub const MAX_I32:   usize = #max_i32;
            pub const MAX_I64:   usize = #max_i64;
            pub const MAX_I128:  usize = #max_i128;

            pub const MAX_USIZE: usize = #max_usize;
            pub const MAX_ISIZE: usize = #max_isize;

            pub const MAX_F32:   usize = #max_f32;
            pub const MAX_F64:   usize = #max_f64;

            pub const MAX_BOOL:  usize = #max_bool;
            pub const MAX_CHAR:  usize = #max_char;
            pub const MAX_HEXSTR:usize = #max_hexstr;
            pub const MAX_STR:   usize = #max_str;
            pub const MAX_HEXSTR_LEN: usize = #max_hexstr_len_expr;

            /// Maximum arity across all functions; token buffers use `1 + MAX_ARITY`.
            pub const MAX_ARITY: usize = #max_arity_num;

            /// Maximum number of commands
            pub const NUM_COMMANDS: usize = ENTRIES.len();

            // Largest function name
            pub const MAX_FUNCTION_NAME_LEN: usize = #function_name_max_len;

            /// One entry per function available to the dispatcher.
            pub struct Entry {

                /// Function name used in textual calls (first token).
                pub name: &'static str,

                /// Required positional arity.
                pub arity: u8,

                /// Descriptor-specific parser filling `CallCtx` from `&[&str]`.
                pub parser: for<'ctx> fn(&mut CallCtx<'ctx>, &[&'ctx str]) -> Result<(), DispatchError>,

                /// Wrapper invoking the target function.
                pub caller: for<'ctx> fn(&mut CallCtx<'ctx>, ArgsView<'ctx>) -> Result<(), DispatchError>,

                /// Index into `PARAM_SPECS` (for diagnostics).
                pub spec_idx: u16,
            }

            /// A lightweight view over the raw tokens for advanced callers.
            pub struct ArgsView<'a> {
                pub tokens: &'a [&'a str],
                pub len: usize,
            }

            /// Errors Generateted by tokenization, arity check, or per-type parsing.
            #[derive(Debug, Clone, Copy, PartialEq, Eq)]
            pub enum DispatchError {

                /// Input line contains no tokens.
                Empty,

                /// No function with the given name exists in the table.
                UnknownFunction,

                /// Function exists, but arity mismatched.
                WrongArity { expected: u8 },

                /// Failed to parse a `bool`.
                BadBool,

                /// Failed to parse a `char` (must be exactly one Unicode scalar).
                BadChar,

                /// Failed to parse an unsigned integer (`u*`).
                BadUnsigned,

                /// Failed to parse a signed integer (`i*`).
                BadSigned,

                /// Failed to parse a float (`f64`).
                BadFloat,

                /// Failed to parse a hexlified string.
                BadHexStr,
            }

            /// Stack-only argument storage sized by the `MAX_*` constants.
            pub struct CallCtx<'a> {
                pub u8s:    [u8;    MAX_U8],
                pub u16s:   [u16;   MAX_U16],
                pub u32s:   [u32;   MAX_U32],
                pub u64s:   [u64;   MAX_U64],
                pub u128s:  [u128;  MAX_U128],

                pub i8s:    [i8;    MAX_I8],
                pub i16s:   [i16;   MAX_I16],
                pub i32s:   [i32;   MAX_I32],
                pub i64s:   [i64;   MAX_I64],
                pub i128s:  [i128;  MAX_I128],

                pub usizes: [usize; MAX_USIZE],
                pub isizes: [isize; MAX_ISIZE],

                pub f32s:   [f32;   MAX_F32],
                pub f64s:   [f64;   MAX_F64],

                pub bools:  [bool;  MAX_BOOL],
                pub chars:  [char;  MAX_CHAR],
                pub strs:   [&'a str; MAX_STR],
                pub hexstrs: [heapless::Vec<u8, MAX_HEXSTR_LEN>; MAX_HEXSTR],
            }

            impl<'a> CallCtx<'a> {
                /// Construct a zero-initialized `CallCtx`.
                #[inline(always)]
                pub fn new() -> Self {
                    Self {
                        u8s:    [0;    MAX_U8],
                        u16s:   [0;    MAX_U16],
                        u32s:   [0;    MAX_U32],
                        u64s:   [0;    MAX_U64],
                        u128s:  [0;    MAX_U128],

                        i8s:    [0;    MAX_I8],
                        i16s:   [0;    MAX_I16],
                        i32s:   [0;    MAX_I32],
                        i64s:   [0;    MAX_I64],
                        i128s:  [0;    MAX_I128],

                        usizes: [0;    MAX_USIZE],
                        isizes: [0;    MAX_ISIZE],

                        f32s:   [0.0;  MAX_F32],
                        f64s:   [0.0;  MAX_F64],

                        bools:  [false; MAX_BOOL],
                        chars:  ['\0'; MAX_CHAR],
                        strs:   ["";   MAX_STR],
                        hexstrs: core::array::from_fn(|_| heapless::Vec::new()),
                    }
                }
            }

            /// Generated per-spec parsers
            #( #parsers )*

            /// Generated per-function wrappers
            #( #wrappers )*

            /// Function registry and lookup
            #registry_fn

            /// Static function table (sorted by name).
            pub static ENTRIES: &[Entry] = &[
                #( #entry_inits ),*
            ];

            /// Fast string-table lookup (match on string literal).
            #[inline(always)]
            fn find_entry(name: &str) -> Option<&'static Entry> {
                match name {
                    #( #match_arms )*
                    _ => None,
                }
            }

            /// Static pairs of (function name, parameter descriptor).
            pub static NAME_AND_SPEC: &[(&'static str, &'static str)] = &[
                #( #name_spec_pairs ),*
            ];

            /// Return (function name, descriptor) pairs. No allocations.
            #[inline(always)]
            pub fn get_commands() -> &'static [(&'static str, &'static str)] {
                NAME_AND_SPEC
            }

            /// Return descriptor help string (character to type mapping).
            #[inline(always)]
            pub fn get_datatypes() -> &'static str {
                DESCRIPTOR_HELP
            }

            /// Parse a hexlified string (even-length, non-empty, valid hex).
            #[inline(always)]
            pub fn parse_hexstr(s: &str) -> Option<heapless::Vec<u8, MAX_HEXSTR_LEN>> {
                if s.len() % 2 != 0 || s.is_empty() || (s.len() / 2) > MAX_HEXSTR_LEN {
                    return None;
                }
                (0..s.len())
                    .step_by(2)
                    .map(|i| u8::from_str_radix(&s[i..i+2], 16).ok())
                    .collect()
            }

            // Quotes-aware tokenizer (no heap). Caller provides the buffer.
            /// Splits by ASCII space or tab. A pair of `"` quotes groups a token (quotes
            /// Returns `Empty` if no tokens were produced.
            pub fn tokenize<'a>(line: &'a str, out: &mut [&'a str]) -> Result<usize, DispatchError> {
                let bytes = line.as_bytes();
                let mut i = 0usize;
                let mut n = 0usize;

                while i < bytes.len() {
                    // Skip leading spaces
                    while i < bytes.len() && is_space(bytes[i]) { i += 1; }
                    if i >= bytes.len() { break; }

                    if bytes[i] == b'"' {
                        // Quoted token
                        let start = i + 1;
                        i = start;
                        while i < bytes.len() && bytes[i] != b'"' { i += 1; }
                        if n < out.len() { out[n] = &line[start..i]; n += 1; }
                        if i < bytes.len() { i += 1; }
                        // Consume trailing non-space until next whitespace to match original behavior.
                        while i < bytes.len() && !is_space(bytes[i]) { i += 1; }
                    } else {
                        // Unquoted token
                        let start = i;
                        while i < bytes.len() && !is_space(bytes[i]) { i += 1; }
                        if n < out.len() { out[n] = &line[start..i]; n += 1; }
                    }
                }

                if n == 0 { return Err(DispatchError::Empty); }
                Ok(n)
            }

            /// ASCII space or tab.
            #[inline(always)]
            const fn is_space(b: u8) -> bool { b == b' ' || b == b'\t' }

            /// Accepts `1|true|True|TRUE` as `true`, and `0|false|False|FALSE` as `false`.
            #[inline(always)]
            fn parse_bool(s: &str) -> Option<bool> {
                match s {
                    "1" | "true" | "True" | "TRUE" => Some(true),
                    "0" | "false" | "False" | "FALSE" => Some(false),
                    _ => None,
                }
            }

            /// One-character string => `char`.
            #[inline(always)]
            fn parse_char(s: &str) -> Option<char> {
                let mut it = s.chars();
                let c = it.next()?;
                if it.next().is_none() { Some(c) } else { None }
            }

            #[inline(always)]
            fn parse_f<T>(s: &str) -> Option<T> where T: core::str::FromStr { s.parse::<T>().ok() }

            #[inline(always)]
            pub fn dispatch(line: &str) -> Result<(), DispatchError> {
                // + 2 in order to detect if more args than expected are provided..
                let mut toks: [&str; 2 + MAX_ARITY] = [""; 2 + MAX_ARITY];
                dispatch_with_buf(line, &mut toks)
            }

            /// Embedded-friendly entry point: caller supplies the token buffer.
            #[inline(always)]
            pub fn dispatch_with_buf<'a>(line: &'a str, toks: &mut [&'a str]) -> Result<(), DispatchError> {
                let len = tokenize(line, toks)?;
                let name = toks[0];
                let got_arity = (len - 1) as u16;
                let ent = find_entry(name).ok_or(DispatchError::UnknownFunction)?;
                if got_arity != ent.arity as u16 {
                    return Err(DispatchError::WrongArity { expected: ent.arity });
                }

                // Fill CallCtx from raw &str tokens (no heap).
                let mut ctx = CallCtx::new();
                let args_tokens: &[&str] = &toks[1..len];
                (ent.parser)(&mut ctx, args_tokens)?;

                // Provide a view for advanced use (currently unused by wrappers).
                let args = ArgsView { tokens: args_tokens, len: len - 1 };
                (ent.caller)(&mut ctx, args)
            }
        }
    };

    out.into()
}

/// Internal representation of one function to register (pre-codegen).
struct FnEntry {
    name_str: String,
    path: syn::Path,
    spec: String,
    spec_idx: usize,
}

/// Last path segment (function ident) as a `String`.
fn path_last_ident(p: &syn::Path) -> Option<String> {
    p.segments.last().map(|s| s.ident.to_string())
}

/// Make a valid identifier for wrapper functions (replace non-ASCII-alnum with `_`).
fn sanitize_ident(s: &str) -> String {
    s.chars()
        .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
        .collect()
}

pub fn generate_commands_dispatcher_from_file(input: TokenStream) -> TokenStream {
    use syn::{Expr, parse::ParseStream};

    struct FileMacroInput {
        _mod_token: Token![mod],   // Token for `mod` keyword
        mod_name: Ident,           // Name of the module to generate
        _semi1: Token![;],         // Semicolon after module declaration
        _hexstr_size_token: Ident, // Identifier for hexstr_size
        _eq_token: Token![=],      // Equals token for hexstr_size assignment
        hexstr_size: Expr,         // Expression for hexstr_size value
        _semi2: Token![;],         // Semicolon after hexstr_size assignment
        _path_token: Ident,        // Identifier for path
        _eq_token2: Token![=],     // Equals token for path assignment
        path: LitStr,              // Literal string for file path
    }

    impl Parse for FileMacroInput {
        fn parse(input: ParseStream) -> syn::Result<Self> {
            Ok(FileMacroInput {
                _mod_token: input.parse()?,
                mod_name: input.parse()?,
                _semi1: input.parse()?,
                _hexstr_size_token: input.parse()?,
                _eq_token: input.parse()?,
                hexstr_size: input.parse()?,
                _semi2: input.parse()?,
                _path_token: input.parse()?,
                _eq_token2: input.parse()?,
                path: input.parse()?,
            })
        }
    }

    let FileMacroInput {
        mod_name,
        hexstr_size,
        path,
        ..
    } = parse_macro_input!(input as FileMacroInput);

    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
    let full_path = std::path::Path::new(&manifest_dir).join(path.value());

    let raw_dsl = std::fs::read_to_string(&full_path)
        .unwrap_or_else(|_| panic!("Failed to read command descriptor file: {:?}", full_path));

    let macro_input = quote! {
        mod #mod_name;
        hexstr_size = #hexstr_size;
        #raw_dsl
    };

    generate_dispatcher_from_dsl(macro_input.into())
}

// ========================= TESTS ==========================================

#[cfg(test)]
mod tests {
    use super::*;
    use quote::quote;

    // Helper to generate dispatcher code and return it as a string for inspection
    fn generate_dispatcher(descriptor: &str) -> String {
        let input = quote! {
            mod test_dispatcher;
            hexstr_size = 64;
            #descriptor
        };

        // Call the internal implementation that returns proc_macro2::TokenStream
        let output = {
            let parsed = syn::parse2::<CommandMacroInput>(input).expect("Failed to parse input");

            // Now manually extract and process just like generate_dispatcher_from_dsl does
            let CommandMacroInput {
                mod_ident,
                body,
                hexstr_size,
            } = parsed;

            let pairs: Vec<(String, Vec<syn::Path>)> = {
                let s = body.value();
                let mut acc = Vec::new();
                for group in s.split(',') {
                    let grp = group.trim();
                    if grp.is_empty() {
                        continue;
                    }
                    let (desc, names) = match grp.split_once(':') {
                        Some((d, r)) => (d.trim(), r.trim()),
                        None => continue,
                    };
                    if desc.is_empty() || names.is_empty() {
                        continue;
                    }
                    let desc_str = desc.to_string();
                    let funcs: StdResult<Vec<_>, _> = names
                        .split_whitespace()
                        .map(syn::parse_str::<syn::Path>)
                        .collect();
                    let funcs = match funcs {
                        Ok(v) => v,
                        Err(_) => continue,
                    };
                    acc.push((desc_str, funcs));
                }
                acc
            };

            // Check we got at least some entries for non-empty, non-whitespace descriptors
            if !descriptor.trim().is_empty() && pairs.is_empty() {
                panic!("Failed to parse descriptor: {}", descriptor);
            }

            // Verify parsing worked
            assert!(!mod_ident.to_string().is_empty());

            quote! {
                // Simplified version for testing
                pub mod #mod_ident {
                    pub const DESCRIPTOR: &str = #descriptor;
                    pub const HEXSTR_SIZE: usize = #hexstr_size;
                }
            }
        };

        output.to_string()
    }

    // ============================================================================
    // Basic Parsing Tests
    // ============================================================================

    #[test]
    fn test_parse_simple_descriptor() {
        let code = generate_dispatcher("DD: test::add");
        assert!(code.contains("test_dispatcher"));
        assert!(code.contains("DESCRIPTOR"));
    }

    #[test]
    fn test_parse_multiple_functions() {
        let code = generate_dispatcher("DD: test::add test::sub");
        assert!(code.contains("test_dispatcher"));
    }

    #[test]
    fn test_parse_multiple_descriptors() {
        let code = generate_dispatcher("DD: test::add, dd: test::sub");
        assert!(code.contains("test_dispatcher"));
    }

    #[test]
    fn test_parse_void_descriptor() {
        let code = generate_dispatcher("v: test::no_args");
        assert!(code.contains("test_dispatcher"));
    }

    #[test]
    fn test_parse_all_unsigned() {
        let code = generate_dispatcher("BWDQX: test::unsigned");
        assert!(code.contains("test_dispatcher"));
    }

    #[test]
    fn test_parse_all_signed() {
        let code = generate_dispatcher("bwdqx: test::signed");
        assert!(code.contains("test_dispatcher"));
    }

    #[test]
    fn test_parse_floats() {
        let code = generate_dispatcher("fF: test::floats");
        assert!(code.contains("test_dispatcher"));
    }

    #[test]
    fn test_parse_special_types() {
        let code = generate_dispatcher("tcs: test::special");
        assert!(code.contains("test_dispatcher"));
    }

    #[test]
    fn test_parse_hexstr() {
        let code = generate_dispatcher("h: test::hexstr");
        assert!(code.contains("test_dispatcher"));
    }

    #[test]
    fn test_custom_module_name() {
        let input = quote! {
            mod my_custom_name;
            hexstr_size = 64;
            "DD: test::add"
        };

        let parsed = syn::parse2::<CommandMacroInput>(input).expect("Failed to parse");
        assert_eq!(parsed.mod_ident.to_string(), "my_custom_name");
    }

    #[test]
    fn test_custom_hexstr_size() {
        let input = quote! {
            mod test_dispatcher;
            hexstr_size = 256;
            "h: test::hexstr"
        };

        let parsed = syn::parse2::<CommandMacroInput>(input).expect("Failed to parse");
        // Verify hexstr_size was captured
        assert!(parsed.hexstr_size.is_some());
    }

    // ============================================================================
    // HostCounts Tests
    // ============================================================================

    #[test]
    fn test_host_counts_default() {
        let counts = HostCounts::default();
        assert_eq!(counts.u8_c, 0);
        assert_eq!(counts.i32_c, 0);
        assert_eq!(counts.f64_c, 0);
        assert_eq!(counts.bool_c, 0);
    }

    #[test]
    fn test_host_counts_max() {
        let a = HostCounts {
            u8_c: 5,
            u16_c: 2,
            u32_c: 0,
            ..Default::default()
        };
        let b = HostCounts {
            u8_c: 3,
            u16_c: 7,
            u32_c: 1,
            ..Default::default()
        };

        let max = host_counts_max(a, b);
        assert_eq!(max.u8_c, 5);
        assert_eq!(max.u16_c, 7);
        assert_eq!(max.u32_c, 1);
    }

    #[test]
    fn test_host_counts_all_types() {
        let mut counts = HostCounts::default();
        counts.u8_c = 1;
        counts.u16_c = 2;
        counts.u32_c = 3;
        counts.u64_c = 4;
        counts.u128_c = 5;
        counts.i8_c = 1;
        counts.i16_c = 2;
        counts.i32_c = 3;
        counts.i64_c = 4;
        counts.i128_c = 5;
        counts.usize_c = 1;
        counts.isize_c = 2;
        counts.f32_c = 1;
        counts.f64_c = 2;
        counts.bool_c = 1;
        counts.char_c = 1;
        counts.str_c = 1;
        counts.hexstr_c = 1;

        // Verify all fields can be set
        assert_eq!(counts.u8_c, 1);
        assert_eq!(counts.hexstr_c, 1);
    }

    // ============================================================================
    // Path Utility Tests
    // ============================================================================

    #[test]
    fn test_path_last_ident_simple() {
        let path: syn::Path = syn::parse_str("test::add").unwrap();
        let last = path_last_ident(&path);
        assert_eq!(last, Some("add".to_string()));
    }

    #[test]
    fn test_path_last_ident_single() {
        let path: syn::Path = syn::parse_str("add").unwrap();
        let last = path_last_ident(&path);
        assert_eq!(last, Some("add".to_string()));
    }

    #[test]
    fn test_path_last_ident_long() {
        let path: syn::Path = syn::parse_str("crate::module::submodule::function").unwrap();
        let last = path_last_ident(&path);
        assert_eq!(last, Some("function".to_string()));
    }

    // ============================================================================
    // Sanitize Identifier Tests
    // ============================================================================

    #[test]
    fn test_sanitize_ident_normal() {
        assert_eq!(sanitize_ident("my_function"), "my_function");
    }

    #[test]
    fn test_sanitize_ident_hyphen() {
        assert_eq!(sanitize_ident("my-function"), "my_function");
    }

    #[test]
    fn test_sanitize_ident_special_chars() {
        assert_eq!(sanitize_ident("my.func@name"), "my_func_name");
    }

    #[test]
    fn test_sanitize_ident_numbers() {
        assert_eq!(sanitize_ident("func123"), "func123");
    }

    #[test]
    fn test_sanitize_ident_unicode() {
        assert_eq!(sanitize_ident("func€name"), "func_name");
    }

    #[test]
    fn test_sanitize_ident_empty() {
        assert_eq!(sanitize_ident(""), "");
    }

    // ============================================================================
    // Descriptor Parsing Logic Tests
    // ============================================================================

    #[test]
    fn test_descriptor_split_by_comma() {
        let input = "DD: test::add, dd: test::sub";
        let parts: Vec<_> = input.split(',').map(|s| s.trim()).collect();
        assert_eq!(parts.len(), 2);
        assert!(parts[0].contains("DD"));
        assert!(parts[1].contains("dd"));
    }

    #[test]
    fn test_descriptor_split_by_colon() {
        let input = "DD: test::add test::sub";
        let (desc, funcs) = input.split_once(':').unwrap();
        assert_eq!(desc.trim(), "DD");
        assert!(funcs.contains("add"));
        assert!(funcs.contains("sub"));
    }

    #[test]
    fn test_descriptor_multiple_functions() {
        let input = "test::add test::sub test::mul";
        let funcs: Vec<_> = input.split_whitespace().collect();
        assert_eq!(funcs.len(), 3);
    }

    #[test]
    fn test_descriptor_with_extra_whitespace() {
        let input = "  DD:  test::add  ,  dd: test::sub  ";
        let parts: Vec<_> = input.split(',').map(|s| s.trim()).collect();
        assert_eq!(parts.len(), 2);
    }

    // ============================================================================
    // FnEntry Tests
    // ============================================================================

    #[test]
    fn test_fn_entry_creation() {
        let path: syn::Path = syn::parse_str("test::add").unwrap();
        let entry = FnEntry {
            name_str: "add".to_string(),
            path: path.clone(),
            spec: "DD".to_string(),
            spec_idx: 0,
        };

        assert_eq!(entry.name_str, "add");
        assert_eq!(entry.spec, "DD");
        assert_eq!(entry.spec_idx, 0);
    }

    #[test]
    fn test_fn_entry_sorting() {
        let mut entries = vec![
            FnEntry {
                name_str: "zebra".to_string(),
                path: syn::parse_str("test::zebra").unwrap(),
                spec: "v".to_string(),
                spec_idx: 0,
            },
            FnEntry {
                name_str: "apple".to_string(),
                path: syn::parse_str("test::apple").unwrap(),
                spec: "v".to_string(),
                spec_idx: 0,
            },
            FnEntry {
                name_str: "middle".to_string(),
                path: syn::parse_str("test::middle").unwrap(),
                spec: "v".to_string(),
                spec_idx: 0,
            },
        ];

        entries.sort_by(|a, b| a.name_str.cmp(&b.name_str));

        assert_eq!(entries[0].name_str, "apple");
        assert_eq!(entries[1].name_str, "middle");
        assert_eq!(entries[2].name_str, "zebra");
    }

    // ============================================================================
    // Descriptor Character Analysis Tests
    // ============================================================================

    #[test]
    fn test_count_descriptor_unsigned() {
        let desc = "BWDQX";
        let mut count = 0;
        for ch in desc.chars() {
            match ch {
                'B' | 'W' | 'D' | 'Q' | 'X' => count += 1,
                _ => {}
            }
        }
        assert_eq!(count, 5);
    }

    #[test]
    fn test_count_descriptor_signed() {
        let desc = "bwdqx";
        let mut count = 0;
        for ch in desc.chars() {
            match ch {
                'b' | 'w' | 'd' | 'q' | 'x' => count += 1,
                _ => {}
            }
        }
        assert_eq!(count, 5);
    }

    #[test]
    fn test_count_descriptor_mixed() {
        let desc = "DDstf";
        let mut u32_count = 0;
        let mut str_count = 0;
        let mut bool_count = 0;
        let mut f32_count = 0;

        for ch in desc.chars() {
            match ch {
                'D' => u32_count += 1,
                's' => str_count += 1,
                't' => bool_count += 1,
                'f' => f32_count += 1,
                _ => {}
            }
        }

        assert_eq!(u32_count, 2);
        assert_eq!(str_count, 1);
        assert_eq!(bool_count, 1);
        assert_eq!(f32_count, 1);
    }

    #[test]
    fn test_arity_calculation() {
        let desc = "DDst";
        let arity = desc.chars().count();
        assert_eq!(arity, 4);
    }

    #[test]
    fn test_void_arity() {
        let desc = "v";
        let arity = if desc == "v" { 0 } else { desc.chars().count() };
        assert_eq!(arity, 0);
    }

    // ============================================================================
    // CommandMacroInput Parsing Tests
    // ============================================================================

    #[test]
    fn test_parse_basic_input() {
        let input = quote! {
            mod test_dispatcher;
            hexstr_size = 64;
            "DD: test::add"
        };

        let parsed = syn::parse2::<CommandMacroInput>(input);
        assert!(parsed.is_ok());

        let cmd = parsed.unwrap();
        assert_eq!(cmd.mod_ident.to_string(), "test_dispatcher");
        assert_eq!(cmd.body.value(), "DD: test::add");
    }

    #[test]
    fn test_parse_without_hexstr_size() {
        let input = quote! {
            mod test_dispatcher;
            "DD: test::add"
        };

        let parsed = syn::parse2::<CommandMacroInput>(input);
        assert!(parsed.is_ok());
        assert!(parsed.unwrap().hexstr_size.is_none());
    }

    #[test]
    fn test_parse_with_const_hexstr_size() {
        let input = quote! {
            mod test_dispatcher;
            hexstr_size = crate::MAX_SIZE;
            "DD: test::add"
        };

        let parsed = syn::parse2::<CommandMacroInput>(input);
        assert!(parsed.is_ok());
        assert!(parsed.unwrap().hexstr_size.is_some());
    }

    #[test]
    fn test_parse_complex_descriptor() {
        let input = quote! {
            mod dispatcher;
            hexstr_size = 128;
            "DD: test::add test::sub, dd: test::neg, s: test::greet, v: test::noop"
        };

        let parsed = syn::parse2::<CommandMacroInput>(input);
        assert!(parsed.is_ok());
    }

    // ============================================================================
    // Edge Cases
    // ============================================================================

    #[test]
    fn test_empty_descriptor_string() {
        let code = generate_dispatcher("");
        assert!(code.contains("test_dispatcher"));
    }

    #[test]
    fn test_descriptor_only_whitespace() {
        let code = generate_dispatcher("   ");
        assert!(code.contains("test_dispatcher"));
    }

    #[test]
    fn test_descriptor_trailing_comma() {
        let code = generate_dispatcher("DD: test::add,");
        assert!(code.contains("test_dispatcher"));
    }

    #[test]
    fn test_descriptor_multiple_commas() {
        let code = generate_dispatcher("DD: test::add,, dd: test::sub");
        assert!(code.contains("test_dispatcher"));
    }

    #[test]
    fn test_long_function_name() {
        let code = generate_dispatcher(
            "DD: test::this_is_a_very_long_function_name_that_should_still_work",
        );
        assert!(code.contains("test_dispatcher"));
    }

    // ============================================================================
    // Integration Tests
    // ============================================================================

    #[test]
    fn test_realistic_command_set() {
        let code = generate_dispatcher(
            "v: test::help test::version, \
             D: test::delay, \
             DD: test::add test::sub test::mul test::div, \
             s: test::echo test::print, \
             t: test::enable test::disable, \
             Dst: test::set_config",
        );
        assert!(code.contains("test_dispatcher"));
    }

    #[test]
    fn test_all_descriptor_types() {
        let code = generate_dispatcher(
            "B: test::u8_func, \
             W: test::u16_func, \
             D: test::u32_func, \
             Q: test::u64_func, \
             X: test::u128_func, \
             b: test::i8_func, \
             w: test::i16_func, \
             d: test::i32_func, \
             q: test::i64_func, \
             x: test::i128_func, \
             Z: test::usize_func, \
             z: test::isize_func, \
             f: test::f32_func, \
             F: test::f64_func, \
             t: test::bool_func, \
             c: test::char_func, \
             s: test::str_func, \
             h: test::hex_func, \
             v: test::void_func",
        );
        assert!(code.contains("test_dispatcher"));
    }

    // ============================================================================
    // Descriptor Uniqueness Tests
    // ============================================================================

    #[test]
    fn test_duplicate_descriptors_dedup() {
        let descriptor = "DD: test::add, DD: test::sub, DD: test::mul";
        let mut unique: Vec<String> = Vec::new();

        for group in descriptor.split(',') {
            let grp = group.trim();
            if let Some((desc, _)) = grp.split_once(':') {
                let desc_str = desc.trim().to_string();
                if !unique.contains(&desc_str) {
                    unique.push(desc_str);
                }
            }
        }

        assert_eq!(unique.len(), 1);
        assert_eq!(unique[0], "DD");
    }

    #[test]
    fn test_different_descriptors_no_dedup() {
        let descriptor = "DD: test::add, dd: test::sub, D: test::third";
        let mut unique: Vec<String> = Vec::new();

        for group in descriptor.split(',') {
            let grp = group.trim();
            if let Some((desc, _)) = grp.split_once(':') {
                let desc_str = desc.trim().to_string();
                if !unique.contains(&desc_str) {
                    unique.push(desc_str);
                }
            }
        }

        assert_eq!(unique.len(), 3);
    }

    // ============================================================================
    // Maximum Length Tests
    // ============================================================================

    #[test]
    fn test_max_function_name_length() {
        let names = vec!["a", "abc", "very_long_name", "x"];
        let max_len = names.iter().map(|n| n.len()).max().unwrap_or(0) + 1;
        assert_eq!(max_len, 15); // "very_long_name" + 1
    }

    #[test]
    fn test_count_commands() {
        let descriptor = "DD: test::add test::sub, d: test::neg";
        let count = descriptor
            .split(',')
            .map(|group| {
                if let Some((_, names)) = group.split_once(':') {
                    names.split_whitespace().count()
                } else {
                    0
                }
            })
            .sum::<usize>();

        assert_eq!(count, 3);
    }
}