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
|
//! Mingling Macros Crate
//!
//! This crate provides procedural macros for the Mingling framework.
//! Macros are implemented in separate modules and re-exported here.
//!
//! # Architecture Overview
//!
//! The Mingling macros crate provides the following categories of macros:
//!
//! - **Command definition**: `dispatcher!`, `dispatcher_clap!`, `node!`, `pack!`
//! - **Chain processing**: `#[chain]`, `gen_program!`, `route!`, `empty_result!`
//! - **Rendering**: `#[renderer]`, `r_print!`, `r_println!`
//! - **Help system**: `#[help]`, `register_help!`
//! - **Derive macros**: `#[derive(Groupped)]`, `#[derive(EnumTag)]`, `#[derive(GrouppedSerialize)]`
//! - **Program setup**: `#[program_setup]`
//! - **Completion (comp feature)**: `#[completion]`, `suggest!`, `suggest_enum!`
//! - **Internal registration**: `register_type!`, `register_chain!`, `register_renderer!`,
//! `program_fallback_gen!`, `program_final_gen!`, `program_comp_gen!`
use once_cell::sync::Lazy;
use proc_macro::TokenStream;
use proc_macro2::Ident;
use quote::quote;
use std::collections::BTreeSet;
use std::sync::Mutex;
use syn::parse_macro_input;
mod chain;
#[cfg(feature = "comp")]
mod completion;
#[cfg(feature = "dispatch_tree")]
mod dispatch_tree_gen;
mod dispatcher;
#[cfg(feature = "clap")]
mod dispatcher_clap;
mod enum_tag;
mod groupped;
mod help;
mod node;
mod pack;
mod program_setup;
mod render;
mod renderer;
#[cfg(feature = "comp")]
mod suggest;
pub(crate) const DEFAULT_PROGRAM_NAME: &str = "ThisProgram";
#[allow(dead_code)]
pub(crate) fn default_program_ident() -> Ident {
Ident::new(DEFAULT_PROGRAM_NAME, proc_macro2::Span::call_site())
}
pub(crate) fn default_program_path() -> proc_macro2::TokenStream {
quote::quote! { crate::ThisProgram }
}
// Global variables
#[cfg(feature = "general_renderer")]
pub(crate) static GENERAL_RENDERERS: Lazy<Mutex<BTreeSet<String>>> =
Lazy::new(|| Mutex::new(BTreeSet::new()));
#[cfg(feature = "comp")]
pub(crate) static COMPLETIONS: Lazy<Mutex<BTreeSet<String>>> =
Lazy::new(|| Mutex::new(BTreeSet::new()));
#[cfg(feature = "dispatch_tree")]
pub(crate) static COMPILE_TIME_DISPATCHERS: Lazy<Mutex<BTreeSet<String>>> =
Lazy::new(|| Mutex::new(BTreeSet::new()));
pub(crate) static PACKED_TYPES: Lazy<Mutex<BTreeSet<String>>> =
Lazy::new(|| Mutex::new(BTreeSet::new()));
pub(crate) static CHAINS: Lazy<Mutex<BTreeSet<String>>> = Lazy::new(|| Mutex::new(BTreeSet::new()));
pub(crate) static RENDERERS: Lazy<Mutex<BTreeSet<String>>> =
Lazy::new(|| Mutex::new(BTreeSet::new()));
pub(crate) static CHAINS_EXIST: Lazy<Mutex<BTreeSet<String>>> =
Lazy::new(|| Mutex::new(BTreeSet::new()));
pub(crate) static RENDERERS_EXIST: Lazy<Mutex<BTreeSet<String>>> =
Lazy::new(|| Mutex::new(BTreeSet::new()));
pub(crate) static HELP_REQUESTS: Lazy<Mutex<BTreeSet<String>>> =
Lazy::new(|| Mutex::new(BTreeSet::new()));
/// Checks that a TypePath is a simple single-segment identifier (no `::` in the path).
///
/// This is used by `#[renderer]`, `#[help]`, `#[chain]`, and `#[completion]` attribute macros
/// to ensure that the type in the function signature is a bare identifier like `Empty`,
/// not a qualified path like `other::Empty`.
///
/// Returns `None` if the type is valid, or a `compile_error!` token stream if it contains `::`.
pub(crate) fn check_single_segment_type(
type_path: &syn::TypePath,
attr_name: &str,
) -> Option<proc_macro2::TokenStream> {
if type_path.path.segments.len() > 1 {
let type_str = quote! { #type_path };
Some(quote! {
compile_error!(concat!(
"The type `",
#type_str,
"` in ",
#attr_name,
" function must be a simple single-segment type, ",
"e.g. `Empty` instead of `other::Empty`. ",
"Qualified paths with `::` are not allowed here."
));
})
} else {
None
}
}
/// Creates a `Node` from a dot-separated path string.
///
/// Each segment is converted to kebab-case (unless it starts with `_`).
/// Segments are joined via `.join()` calls, building a path hierarchy for
/// command matching.
///
/// # Syntax
///
/// ```rust,ignore
/// node!("subcommand")
/// node!("sub.subsub")
/// node!("") // empty → Node::default()
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::node;
///
/// // Creates a single-level node for "hello"
/// let n = node!("hello");
///
/// // Creates a two-level node for "remote control"
/// let n = node!("remote.control");
/// ```
///
/// # Internals
///
/// The generated code is equivalent to:
/// ```rust,ignore
/// Node::default().join("hello")
/// Node::default().join("remote").join("control")
/// ```
///
/// This macro is typically used internally by `dispatcher!` and should rarely
/// need to be called directly.
#[proc_macro]
pub fn node(input: TokenStream) -> TokenStream {
node::node(input)
}
/// Creates a type-safe wrapper struct around an inner type, with automatic
/// trait implementations for use in the Mingling chain/render pipeline.
///
/// The generated struct implements: `From`/`Into`, `AsRef`/`AsMut`, `Deref`/`DerefMut`,
/// `Default` (conditional on inner type), and conversion into `AnyOutput` /
/// `ChainProcess` for routing.
///
/// # Syntax
///
/// ```rust,ignore
/// // Default program name (uses `ThisProgram`):
/// pack!(TypeName = InnerType);
///
/// // Explicit program name:
/// pack!(MyProgram, TypeName = InnerType);
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::pack;
///
/// // Creates `Hello` wrapping `String`, registered under `ThisProgram`:
/// pack!(Hello = String);
///
/// // Creates `Greeting` wrapping `String`, registered under `MyApp`:
/// pack!(MyApp, Greeting = String);
/// ```
///
/// After expansion, `Hello` has:
/// - `Hello::new(String)` — constructor
/// - `Hello::to_chain()` — routes to the next chain processor
/// - `Hello::to_render()` — routes to a renderer
/// - `From<String> for Hello`, `From<Hello> for String`
/// - `Deref<Target = String>`, `DerefMut`
/// - `AsRef<String>`, `AsMut<String>`
/// - `Default` if `String: Default`
/// - `Into<AnyOutput<ThisProgram>>`, `Into<ChainProcess<ThisProgram>>`
/// - Implements `Groupped<ThisProgram>` with `member_id()` returning the enum variant
///
/// The struct is also registered via `register_type!` so that `gen_program!`
/// can include it in the program enum.
///
/// When the `general_renderer` feature is enabled, the struct also gets
/// `#[derive(serde::Serialize)]`.
#[proc_macro]
pub fn pack(input: TokenStream) -> TokenStream {
pack::pack(input)
}
/// Early-returns an error from a `Result`, converting the `Ok` branch to a
/// `ChainProcess`.
///
/// This macro is equivalent to:
/// ```rust,ignore
/// match expr {
/// Ok(r) => r,
/// Err(e) => return e,
/// }
/// ```
///
/// It is useful inside chain functions where you have a `Result<ChainProcess<G>, ChainProcess<G>>`
/// and want to propagate the error case as an early return.
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::{chain, route};
///
/// #[chain]
/// fn process(prev: SomeEntry) -> ChainProcess<ThisProgram> {
/// let value = route!(try_something().ok_or(ErrorEntry::new("failed".into()).to_render()));
/// // value is the Ok(ChainProcess) from try_something()
/// value
/// }
/// ```
#[proc_macro]
pub fn route(input: TokenStream) -> TokenStream {
let expr = parse_macro_input!(input as syn::Expr);
let expanded = quote! {
match #expr {
Ok(r) => r,
Err(e) => return e,
}
};
TokenStream::from(expanded)
}
/// Creates an empty result value wrapped in `ChainProcess` for early return
/// from a chain function.
///
/// This macro is a shorthand for constructing an `EmptyResult` and converting
/// it into a `ChainProcess`, which signals to the pipeline that there is
/// no meaningful output to continue processing.
///
/// # Syntax
///
/// ```rust,ignore
/// empty_result!()
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::{chain, empty_result};
///
/// #[chain]
/// fn maybe_skip(prev: SomeEntry) -> Next {
/// if should_skip() {
/// return empty_result!();
/// }
/// // ... continue processing
/// NextEntry::new(result).to_chain()
/// }
/// ```
///
/// # Generated code
///
/// The macro expands to:
/// ```rust,ignore
/// crate::EmptyResult::new(()).to_chain()
/// ```
///
/// This works because `EmptyResult` is automatically generated by `gen_program!`
/// and implements the necessary trait conversions into `ChainProcess`.
#[proc_macro]
pub fn empty_result(_input: TokenStream) -> TokenStream {
let expanded = quote! {
crate::EmptyResult::new(()).to_chain()
};
TokenStream::from(expanded)
}
/// Creates a `Dispatcher` implementation for a subcommand.
///
/// This is the primary way to define command-line subcommands in Mingling.
/// It generates a dispatcher struct that, when matched against user input,
/// converts the arguments into a `ChainProcess` via the specified entry type.
///
/// # Syntax
///
/// ```rust,ignore
/// // Default program name (uses `ThisProgram`):
/// dispatcher!("command.path", CommandStruct => EntryStruct);
///
/// // Explicit program name:
/// dispatcher!(MyProgram, "command.path", CommandStruct => EntryStruct);
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::dispatcher;
///
/// // "hello" subcommand → HelloCommand → HelloEntry
/// dispatcher!("hello", HelloCommand => HelloEntry);
///
/// // Nested: "remote control" → RemoteControlCommand → RemoteControlEntry
/// dispatcher!("remote.control", RemoteControlCommand => RemoteControlEntry);
///
/// // With explicit program:
/// dispatcher!(MyApp, "status", StatusCommand => StatusEntry);
/// ```
///
/// The generated `HelloCommand` implements `Dispatcher<ThisProgram>`:
/// - `node()` returns the `Node` hierarchy for "hello"
/// - `begin(args)` wraps `args` into `HelloEntry` and routes to chain
/// - `clone_dispatcher()` returns a boxed clone
///
/// The `HelloEntry` struct is a wrapper around `Vec<String>` created via
/// an implicit `pack!` call with the program name.
///
/// When the `comp` feature is enabled, the entry type also implements
/// `CompletionEntry` for providing shell completion suggestions.
#[proc_macro]
pub fn dispatcher(input: TokenStream) -> TokenStream {
dispatcher::dispatcher(input)
}
/// Prints formatted text to the current `RenderResult` buffer within a
/// `#[renderer]`(macro.renderer.html) function.
///
/// This macro requires a mutable reference to a `RenderResult` named `r`
/// to be in scope, which is automatically provided inside `#[renderer]`
/// functions.
///
/// # Syntax
///
/// Same as `format!` / `print!`:
///
/// ```rust,ignore
/// r_print!("Hello, {}!", name);
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::{renderer, r_print};
///
/// #[renderer]
/// fn show_greeting(prev: Greeting) {
/// r_print!("Hello, {}!", *prev);
/// }
/// ```
///
/// # Difference from `r_println!`
///
/// `r_print!` does **not** append a newline. Use `r_println!` for newline-terminated output.
#[proc_macro]
pub fn r_print(input: TokenStream) -> TokenStream {
render::r_print(input)
}
/// Prints formatted text followed by a newline to the current `RenderResult`
/// buffer within a `#[renderer]`(macro.renderer.html) function.
///
/// This macro requires a mutable reference to a `RenderResult` named `r`
/// to be in scope, which is automatically provided inside `#[renderer]`
/// functions.
///
/// # Syntax
///
/// Same as `println!`:
///
/// ```rust,ignore
/// r_println!("Hello, {}!", name);
/// r_println!(); // just a newline
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::{renderer, r_println};
///
/// #[renderer]
/// fn show_greeting(prev: Greeting) {
/// r_println!("Hello, {}!", *prev);
/// }
/// ```
#[proc_macro]
pub fn r_println(input: TokenStream) -> TokenStream {
render::r_println(input)
}
/// Declares a chain processing step that transforms one type into another
/// within a Mingling pipeline.
///
/// The `#[chain]` attribute converts an ordinary function (or async function
/// with the `async` feature) into a chain step by:
/// 1. Generating a hidden struct implementing the `Chain` trait.
/// 2. Registering the chain mapping in the global chain registry.
/// 3. Keeping the original function for direct calls.
///
/// # Syntax
///
/// ```rust,ignore
/// // Default program (ThisProgram):
/// #[chain]
/// fn my_step(prev: InputType) -> Next {
/// // transform `prev`...
/// OutputType::new(result)
/// }
///
/// // Explicit program name:
/// #[chain(MyProgram)]
/// fn my_step(prev: InputType) -> Next {
/// // ...
/// }
/// ```
///
/// # Resource Injection
///
/// The `#[chain]` macro supports automatic injection of global resources
/// via the 2nd to Nth parameters. You can read resources immutably with
/// `&T` or mutate them with `&mut T`.
///
/// ## Immutable Resource (`&T`)
///
/// When you write `&SomeResource` as a parameter, the macro automatically
/// resolves it from the global resource store:
///
/// ```rust,ignore
/// #[chain]
/// fn process(prev: HelloEntry, age: &Age, name: &Name) -> Next {
/// // `age` and `name` are automatically injected
/// println!("Age: {}, Name: {}", age, name);
/// NextStep::default()
/// }
/// ```
///
/// This expands to:
///
/// ```rust,ignore
/// let __age_binding = ::mingling::this::<ThisProgram>().res_or_default::<Age>();
/// let age: &Age = __age_binding.as_ref();
/// let __name_binding = ::mingling::this::<ThisProgram>().res_or_default::<Name>();
/// let name: &Name = __name_binding.as_ref();
/// ```
///
/// ## Mutable Resource (`&mut T`)
///
/// When you write `&mut SomeResource` as a parameter, the macro wraps the
/// function body in nested `__modify_res_and_return_any` calls:
///
/// ```rust,ignore
/// #[chain]
/// fn process(prev: HelloEntry, count: &mut InvocationCount, name: &Name) -> Next {
/// count.0 += 1;
/// println!("Invocation #{} for {}", count.0, name);
/// NextStep::default()
/// }
/// ```
///
/// This expands to:
///
/// ```rust,ignore
/// let __name_binding = ::mingling::this::<ThisProgram>().res_or_default::<Name>();
/// let name: &Name = __name_binding.as_ref();
///
/// ::mingling::this::<ThisProgram>().__modify_res_and_return_any(|count: &mut InvocationCount| {
/// count.0 += 1;
/// println!("Invocation #{} for {}", count.0, name);
/// NextStep::default()
/// }).into()
/// ```
///
/// Multiple `&mut` parameters are supported with proper nesting.
///
/// ## Restrictions
///
/// - The first parameter (previous type) must be taken **by move**, not by reference.
/// - Resource injection parameters **must** be references (`&T` or `&mut T`),
/// owned values are not allowed.
/// - When the `async` feature is enabled, `&mut T` cannot be used in async
/// chain functions (only `&T` is supported for async).
///
/// # Sync Example
///
/// ```rust,ignore
/// use mingling::macros::{chain, pack, gen_program};
///
/// pack!(MyOutput = String);
///
/// #[chain]
/// fn greet(prev: HelloEntry) -> Next {
/// let name = prev.first().cloned().unwrap_or_else(|| "World".to_string());
/// MyOutput::new(name)
/// }
/// ```
///
/// # Sync Example with Resource Injection
///
/// ```rust,ignore
/// use mingling::macros::{chain, pack, gen_program, r_println};
///
/// #[derive(Default, Clone)]
/// struct UserName(String);
///
/// pack!(Greeting = String);
/// pack!(DisplayCount = ());
///
/// #[chain]
/// fn greet(prev: HelloEntry, user_name: &UserName, count: &mut u64) -> Next {
/// r_println!("User: {:?}", user_name);
/// *count += 1;
/// Greeting::new(format!("Hello, {}!", user_name.0))
/// }
/// ```
///
/// # Async Example (with `async` feature)
///
/// ```rust,ignore
/// use mingling::macros::{chain, pack, gen_program};
///
/// pack!(MyOutput = String);
///
/// #[chain]
/// async fn greet(prev: HelloEntry) -> Next {
/// let name = prev.first().cloned().unwrap_or_else(|| "World".to_string());
/// some_async_fn(&name).await;
/// MyOutput::new(name)
/// }
/// ```
///
/// # Async Example with Immutable Resource Injection
///
/// ```rust,ignore
/// use mingling::macros::{chain, pack, gen_program};
///
/// pack!(MyOutput = String);
///
/// #[chain]
/// async fn greet(prev: HelloEntry, prefix: &Prefix) -> Next {
/// let name = prev.first().cloned().unwrap_or_else(|| "World".to_string());
/// some_async_fn(&name).await;
/// MyOutput::new(format!("{}{}", prefix.0, name))
/// }
/// ```
///
/// # Requirements
///
/// - The function must have at least **one** parameter (the previous type in the chain).
/// - The first parameter must be taken **by move**.
/// - The function must return `Next` (the type alias generated by `gen_program!`, which equals `ChainProcess<ProgramName>`).
/// - With the `async` feature, async functions are supported; without it, async functions are rejected.
#[proc_macro_attribute]
pub fn chain(attr: TokenStream, item: TokenStream) -> TokenStream {
chain::chain_attr(attr, item)
}
/// Declares a renderer step that renders the output of a chain to the terminal.
///
/// The `#[renderer]` attribute converts a function into a renderer by:
/// 1. Generating a hidden struct implementing the `Renderer` trait.
/// 2. Registering the renderer mapping in the global renderer registry.
/// 3. Keeping the original function for direct calls. When called directly,
/// a new `RenderResult` is created and the renderer function writes its
/// output directly to the current terminal output buffer.
///
/// Inside a `#[renderer]` function, you can use `r_print!` and `r_println!`
/// to write output to the `RenderResult` buffer.
///
/// # Syntax
///
/// ```rust,ignore
/// // Default program (ThisProgram):
/// #[renderer]
/// fn render_my_type(prev: MyType) {
/// r_println!("Output: {:?}", *prev);
/// }
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::{renderer, r_println, pack, gen_program};
///
/// pack!(Greeting = String);
///
/// #[renderer]
/// fn render_greeting(prev: Greeting) {
/// r_println!("Hello, {}!", *prev);
/// }
/// ```
///
/// # Requirements
///
/// - The function must have exactly **one** parameter (the type to render).
/// - The function must return `()` (unit).
/// - The function **cannot** be async.
///
/// # Fallback Renderers
///
/// The macros `gen_program!` automatically generates two fallback types that
/// you can provide renderers for:
/// - `RendererNotFound` — triggered when no matching renderer is found
/// - `DispatcherNotFound` — triggered when no matching dispatcher is found
///
/// ```rust,ignore
/// #[renderer]
/// fn fallback_dispatcher_not_found(prev: DispatcherNotFound) {
/// r_println!("Unknown command: {}", prev.join(", "));
/// }
///
/// #[renderer]
/// fn fallback_renderer_not_found(prev: RendererNotFound) {
/// r_println!("No renderer for `{}`", *prev);
/// }
/// ```
#[proc_macro_attribute]
pub fn renderer(_attr: TokenStream, item: TokenStream) -> TokenStream {
renderer::renderer_attr(item)
}
/// Declares a completion suggestion provider for a command entry type.
///
/// **This macro is only available with the `comp` feature.**
///
/// The `#[completion]` attribute converts a function into a completion provider by:
/// 1. Generating a hidden struct implementing the `Completion` trait.
/// 2. Registering the completion mapping for the specified entry type.
/// 3. Keeping the original function for direct calls.
///
/// # Syntax
///
/// ```rust,ignore
/// #[completion(EntryType)]
/// fn complete_my_entry(ctx: &ShellContext) -> Suggest {
/// // Return suggestions based on current input state...
/// }
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::{completion, suggest, suggest_enum};
/// use mingling::{ShellContext, Suggest};
///
/// #[completion(MyEntry)]
/// fn complete_my_command(ctx: &ShellContext) -> Suggest {
/// if ctx.filling_argument_first("--name") {
/// return suggest!();
/// }
/// if ctx.filling_argument_first("--type") {
/// return suggest_enum!(MyEnum);
/// }
/// if ctx.typing_argument() {
/// return suggest! {
/// "--name": "Provide a name",
/// "--type": "Select a type"
/// }.strip_typed_argument(ctx);
/// }
/// suggest!()
/// }
/// ```
///
/// # Requirements
///
/// - The `comp` feature must be enabled.
/// - The function must have exactly one parameter of type `&ShellContext`.
/// - The function must return `Suggest`.
/// - The function cannot be async.
#[cfg(feature = "comp")]
#[proc_macro_attribute]
pub fn completion(attr: TokenStream, item: TokenStream) -> TokenStream {
completion::completion_attr(attr, item)
}
/// Declares a program setup function that initializes the program instance
/// before execution.
///
/// The `#[program_setup]` attribute converts a function into a setup step by:
/// 1. Generating a struct implementing the `ProgramSetup` trait.
/// 2. The setup function receives a mutable reference to `&mut Program<G>`.
///
/// # Syntax
///
/// ```rust,ignore
/// // Default program (ThisProgram):
/// #[program_setup]
/// fn setup_my_program(program: &mut Program<ThisProgram>) {
/// program.stdout_setting.render_output = false;
/// }
///
/// // Explicit program name:
/// #[program_setup(MyProgram)]
/// fn setup_my_program(program: &mut Program<MyProgram>) {
/// // ...
/// }
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::program_setup;
/// use mingling::Program;
///
/// #[program_setup]
/// fn configure(program: &mut Program<ThisProgram>) {
/// program.with_setup(GeneralRendererSetup);
/// program.user_context.some_flag = true;
/// }
/// ```
///
/// # Requirements
///
/// - The function must have exactly one parameter of type `&mut Program<G>`.
/// - The function must return `()`.
/// - The function cannot be async.
#[proc_macro_attribute]
pub fn program_setup(attr: TokenStream, item: TokenStream) -> TokenStream {
program_setup::setup_attr(attr, item)
}
/// Declares a `Dispatcher` that uses `clap::Parser` for argument parsing.
///
/// **This macro is only available with the `clap` feature.**
///
/// The `#[dispatcher_clap]` attribute:
/// 1. Keeps the original struct definition (typically with `#[derive(clap::Parser)]`).
/// 2. Generates a dispatcher struct that parses arguments using clap and routes
/// to the chain pipeline.
/// 3. Optionally generates a `#[help]` block for displaying clap-generated help.
///
/// # Syntax
///
/// ```rust,ignore
/// // Default program (ThisProgram):
/// #[derive(clap::Parser)]
/// #[dispatcher_clap("command.name", DispatcherStruct)]
/// struct MyEntry { /* ... */ }
///
/// // With explicit error type and help:
/// #[derive(clap::Parser)]
/// #[dispatcher_clap("cmd", Disp, error = ParseError, help = true)]
/// struct CmdEntry { /* ... */ }
///
/// // With explicit program name:
/// #[derive(clap::Parser)]
/// #[dispatcher_clap(MyProgram, "cmd", Disp)]
/// struct CmdEntry { /* ... */ }
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use clap::Parser;
/// use mingling::macros::dispatcher_clap;
///
/// #[derive(Parser)]
/// #[dispatcher_clap("greet", GreetDispatcher, error = GreetParseError, help = true)]
/// struct GreetArgs {
/// #[arg(short, long)]
/// name: String,
/// }
/// ```
///
/// # Options
///
/// - `error = ErrorType` — Specifies an error wrapper type for clap parse failures.
/// The error message is captured and routed to the renderer.
/// - `help = true` — Generates a `#[help]` block that displays clap's help output
/// when `--help` is passed.
#[cfg(feature = "clap")]
#[proc_macro_attribute]
pub fn dispatcher_clap(attr: TokenStream, item: TokenStream) -> TokenStream {
dispatcher_clap::dispatcher_clap_attr(attr, item)
}
/// Registers a help request mapping between an entry type and a help struct.
///
/// This macro is used internally by the `#[help]`(macro.help.html) attribute
/// and is also available for manual registration if needed.
///
/// # Syntax
///
/// ```rust,ignore
/// register_help!(EntryType, HelpStruct);
/// ```
///
/// This adds an entry to the global `HELP_REQUESTS` registry, mapping the
/// enum variant for `EntryType` to the help rendering logic in `HelpStruct`.
#[proc_macro]
pub fn register_help(input: TokenStream) -> TokenStream {
help::register_help(input)
}
/// Registers a dispatcher at compile time for the `dispatch_tree` feature.
///
/// This macro is called internally by `dispatcher!`(macro.dispatcher.html) when
/// the `dispatch_tree` feature is enabled. It stores the node name into the global
/// `COMPILE_TIME_DISPATCHERS` registry and generates a static variable for the
/// dispatcher instance, which is later used by `gen_program!` to generate the
/// dispatch tree routing logic.
///
/// # Syntax
///
/// ```rust,ignore
/// register_dispatcher!("node.name", DispatcherType, EntryName);
/// ```
#[proc_macro]
pub fn register_dispatcher(input: TokenStream) -> TokenStream {
dispatcher::register_dispatcher(input)
}
/// Declares a help rendering function for an entry type.
///
/// The `#[help]` attribute converts a function into a help provider by:
/// 1. Generating a hidden struct implementing the `HelpRequest` trait.
/// 2. Registering the help mapping in the global `HELP_REQUESTS` registry.
/// 3. Keeping the original function for direct calls (with a dummy `RenderResult`).
///
/// Inside a `#[help]` function, you can use `r_print!` and `r_println!`
/// to write help text to the `RenderResult` buffer.
///
/// # Syntax
///
/// ```rust,ignore
/// #[help]
/// fn help_my_entry(prev: MyEntry) {
/// r_println!("Usage: myapp myentry [options]");
/// r_println!(" Does something useful.");
/// }
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::{help, r_println, pack, gen_program};
///
/// pack!(MyEntry = Vec<String>);
///
/// #[help]
/// fn help_my_entry(_prev: MyEntry) {
/// r_println!("Usage: myapp greet [name]");
/// r_println!("Greets the user.");
/// }
/// ```
///
/// # Requirements
///
/// - The function must have exactly one parameter (the entry type to provide help for).
/// - The function must return `()`.
/// - The function cannot be async.
#[proc_macro_attribute]
pub fn help(_attr: TokenStream, item: TokenStream) -> TokenStream {
help::help_attr(item)
}
/// Derive macro for automatically implementing the `Groupped` trait on a struct.
///
/// The `#[derive(Groupped)]` macro:
/// 1. Implements `Groupped<Group>` where the group is specified via `#[group(GroupName)]`.
/// 2. Registers the type via `register_type!` so it's included in the program enum.
/// 3. Generates `Into<AnyOutput<Group>>` and `Into<ChainProcess<Group>>` conversions.
/// 4. Adds `to_chain()` and `to_render()` methods to the struct.
///
/// # Syntax
///
/// ```rust,ignore
/// #[derive(Groupped)]
/// #[group(MyProgram)] // optional; defaults to `ThisProgram`
/// struct MyStruct {
/// field: String,
/// }
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::{Groupped, macros::{chain, gen_program, renderer, r_println}};
///
/// #[derive(Groupped)]
/// #[group(ThisProgram)]
/// struct Greeting {
/// name: String,
/// }
/// ```
///
/// This is equivalent to using `pack!` but works with custom structs that
/// have named fields. For simple wrappers, prefer `pack!`.
#[proc_macro_derive(Groupped, attributes(group))]
pub fn derive_groupped(input: TokenStream) -> TokenStream {
groupped::derive_groupped(input)
}
/// Derive macro for automatically implementing the `EnumTag` trait on an enum
/// with unit-only variants.
///
/// The `#[derive(EnumTag)]` macro generates:
/// - `enum_info(&self) -> (&'static str, &'static str)` — returns (name, description)
/// for the current variant.
/// - `build_enum(name: String) -> Option<Self>` — constructs a variant from its
/// display name (or `#[enum_rename]` value).
/// - `enums() -> &'static [(&'static str, &'static str)]` — returns all (name, description)
/// pairs.
///
/// # Attributes
///
/// - `#[enum_desc("description text")]` — Provides a description for the variant.
/// - `#[enum_rename("display name")]` — Changes the display/build name of the variant.
///
/// # Syntax
///
/// ```rust,ignore
/// #[derive(EnumTag)]
/// enum Fruit {
/// #[enum_desc("A sweet red fruit")]
/// #[enum_rename("apple")]
/// Apple,
///
/// #[enum_desc("A yellow tropical fruit")]
/// #[enum_rename("banana")]
/// Banana,
/// }
/// ```
///
/// # Requirements
///
/// - Can only be derived for **enums** (not structs or unions).
/// - All variants must be **unit variants** (no fields).
/// - Each variant is optional; variants without attributes get their Rust name as display name
/// and an empty description.
#[proc_macro_derive(EnumTag, attributes(enum_desc, enum_rename))]
pub fn derive_enum_tag(input: TokenStream) -> TokenStream {
enum_tag::derive_enum_tag(input)
}
/// Derive macro for implementing both `Groupped` and `serde::Serialize` on a struct.
///
/// **This macro is only available with the `general_renderer` feature.**
///
/// This is identical to `#[derive(Groupped)]` but also adds `#[derive(serde::Serialize)]`
/// to the struct, which is required for the general renderer to serialize output
/// to formats like JSON, YAML, TOML, or RON.
///
/// # Syntax
///
/// ```rust,ignore
/// #[derive(GrouppedSerialize)]
/// #[group(MyProgram)]
/// struct Info {
/// name: String,
/// age: i32,
/// }
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::GrouppedSerialize;
/// use serde::Serialize;
///
/// #[derive(GrouppedSerialize)]
/// struct Info {
/// name: String,
/// age: i32,
/// }
/// ```
#[cfg(feature = "general_renderer")]
#[proc_macro_derive(GrouppedSerialize, attributes(group))]
pub fn derive_groupped_serialize(input: TokenStream) -> TokenStream {
groupped::derive_groupped_serialize(input)
}
/// Generates the program enum and all collected types, chains, and renderers.
///
/// This macro **must** be called at the end of your program module to collect
/// all registered types, chains, renderers, and help requests into a single
/// program enum that implements `ProgramCollect`.
///
/// # Syntax
///
/// ```rust,ignore
/// // Default program name (uses `ThisProgram`):
/// gen_program!();
///
/// // Explicit program name:
/// gen_program!(MyProgram);
/// ```
///
/// # What it generates
///
/// The macro expands to:
/// 1. **`pub type Next = ChainProcess<ProgramName>`** — A convenience type alias
/// for use in chain function return types.
/// 2. **`program_comp_gen!(...)`** (with `comp` feature) — Generates completion infrastructure.
/// 3. **`program_fallback_gen!(...)`** — Generates `RendererNotFound` and `DispatcherNotFound` types.
/// 4. **`program_final_gen!(...)`** — Generates the program enum with:
/// - An enum with all packed types as variants
/// - `Display` implementation for the enum
/// - `ProgramCollect` implementation dispatching to all registered renderers and chains
/// - A `new()` constructor returning `Program<ProgramName>`
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::{dispatcher, chain, renderer, gen_program};
///
/// dispatcher!("hello", HelloCommand => HelloEntry);
///
/// #[chain]
/// fn process(prev: HelloEntry) -> Next {
/// // ...
/// }
///
/// #[renderer]
/// fn render(prev: /* ... */) {
/// r_println!("Done!");
/// }
///
/// // Collect everything:
/// gen_program!();
/// ```
#[proc_macro]
pub fn gen_program(input: TokenStream) -> TokenStream {
let name = read_name(&input);
#[cfg(feature = "comp")]
let comp_gen = quote! {
::mingling::macros::program_comp_gen!(#name);
};
#[cfg(not(feature = "comp"))]
let comp_gen = quote! {};
TokenStream::from(quote! {
// Shit, this feature is unstable
// TODO :: This logic will be implemented when Rust's Impl In Type Alias feature becomes stable
// pub type Next = impl Into<::mingling::ChainProcess<#name>>;
pub type Next = ::mingling::ChainProcess<#name>;
#comp_gen
::mingling::macros::program_fallback_gen!(#name);
::mingling::macros::program_final_gen!(#name);
})
}
/// Internal macro used by `gen_program!` to generate completion infrastructure.
///
/// **This macro is only available with the `comp` feature.**
///
/// This is an internal macro and should not be called directly by user code.
/// It generates a completion dispatcher, the `CompletionContext` type, and
/// the execution/render logic for shell completion.
///
/// The generated module `__completion_gen` contains:
/// - A `__comp` dispatcher that routes completion requests
/// - A `__exec_completion` chain that processes `CompletionContext` into `CompletionSuggest`
/// - A `__render_completion` renderer that outputs completion suggestions
#[proc_macro]
#[cfg(feature = "comp")]
pub fn program_comp_gen(input: TokenStream) -> TokenStream {
let name = read_name(&input);
#[cfg(feature = "async")]
let fn_exec_comp = quote! {
#[::mingling::macros::chain(#name)]
pub async fn __exec_completion(prev: CompletionContext) -> Next {
let read_ctx = ::mingling::ShellContext::try_from(prev.inner);
match read_ctx {
Ok(ctx) => {
let suggest = ::mingling::CompletionHelper::exec_completion::<#name>(&ctx);
CompletionSuggest::new((ctx, suggest)).to_render()
}
Err(_) => std::process::exit(1),
}
}
};
#[cfg(not(feature = "async"))]
let fn_exec_comp = quote! {
#[::mingling::macros::chain(#name)]
pub fn __exec_completion(prev: CompletionContext) -> Next {
let read_ctx = ::mingling::ShellContext::try_from(prev.inner);
match read_ctx {
Ok(ctx) => {
let suggest = ::mingling::CompletionHelper::exec_completion::<#name>(&ctx);
CompletionSuggest::new((ctx, suggest)).to_render()
}
Err(_) => std::process::exit(1),
}
}
};
let comp_dispatcher = quote! {
::mingling::macros::dispatcher!(#name, "__comp", CompletionDispatcher => CompletionContext);
::mingling::macros::pack!(
#name,
CompletionSuggest = (::mingling::ShellContext, ::mingling::Suggest)
);
#fn_exec_comp
::mingling::macros::register_type!(CompletionContext);
#[allow(unused)]
#[::mingling::macros::renderer(#name)]
pub fn __render_completion(prev: CompletionSuggest) {
let (ctx, suggest) = prev.inner;
::mingling::CompletionHelper::render_suggest::<#name>(ctx, suggest);
}
};
TokenStream::from(comp_dispatcher)
}
/// Registers a type into the global packed types registry for inclusion in
/// the program enum generated by `gen_program!`.
///
/// This macro is called internally by `pack!` and `#[derive(Groupped)]`(macro.derive_groupped.html)
/// and is generally not needed in user code. However, it can be used for manual
/// registration if you are implementing custom type registration outside of
/// the standard macros.
///
/// # Syntax
///
/// ```rust,ignore
/// register_type!(MyType);
/// ```
///
/// Each call inserts the type's name into the `PACKED_TYPES` global set, which
/// is later consumed by `program_final_gen!` to generate enum variants.
#[proc_macro]
pub fn register_type(input: TokenStream) -> TokenStream {
let type_ident = parse_macro_input!(input as syn::Ident);
let entry_str = type_ident.to_string();
PACKED_TYPES.lock().unwrap().insert(entry_str);
TokenStream::new()
}
/// Registers a chain mapping from a previous type to a chain struct.
///
/// This macro is called internally by `#[chain]`(macro.chain.html) and is
/// generally not needed in user code. It inserts entries into the global
/// `CHAINS` and `CHAINS_EXIST` registries.
///
/// # Syntax
///
/// ```rust,ignore
/// register_chain!(PreviousType, ChainStruct);
/// ```
///
/// The `PreviousType` is the input type of the chain step, and `ChainStruct`
/// is the generated struct that implements the `Chain` trait.
#[proc_macro]
pub fn register_chain(input: TokenStream) -> TokenStream {
chain::register_chain(input)
}
/// Registers a renderer mapping from a type to a renderer struct.
///
/// This macro is called internally by `#[renderer]`(macro.renderer.html) and is
/// generally not needed in user code. It inserts entries into the global
/// `RENDERERS`, `RENDERERS_EXIST` and (with `general_renderer` feature)
/// `GENERAL_RENDERERS` registries.
///
/// # Syntax
///
/// ```rust,ignore
/// register_renderer!(PreviousType, RendererStruct);
/// ```
///
/// The `PreviousType` is the input type of the renderer, and `RendererStruct`
/// is the generated struct that implements the `Renderer` trait.
#[proc_macro]
pub fn register_renderer(input: TokenStream) -> TokenStream {
renderer::register_renderer(input)
}
/// Internal macro used by `gen_program!` to generate fallback types.
///
/// This macro generates the fallback wrapper types that are essential
/// for error handling in the Mingling pipeline:
///
/// - **`RendererNotFound`** — Wraps a `String` (the name of the missing renderer).
/// Used when no matching renderer is found for a given output type.
/// - **`DispatcherNotFound`** — Wraps `Vec<String>` (the unrecognized command args).
/// Used when no matching dispatcher is found for user input.
/// - **`EmptyResult`** — Wraps `()` (the unit type).
/// Used when the chain returns an empty result.
///
/// Users can (and should) write `#[renderer]` functions for these types
/// to provide meaningful error messages.
///
/// This macro is called automatically by `gen_program!` and should not
/// be called directly by user code.
///
/// # Syntax
///
/// ```rust,ignore
/// // Called internally by gen_program!:
/// program_fallback_gen!(ThisProgram);
/// program_fallback_gen!(MyProgram);
/// ```
///
/// # Generated code equivalent
///
/// ```rust,ignore
/// pack!(ProgramName, RendererNotFound = String);
/// pack!(ProgramName, DispatcherNotFound = Vec<String>);
/// pack!(ProgramName, EmptyResult = ());
/// ```
#[proc_macro]
pub fn program_fallback_gen(input: TokenStream) -> TokenStream {
let name = read_name(&input);
let expanded = quote! {
::mingling::macros::pack!(#name, RendererNotFound = String);
::mingling::macros::pack!(#name, DispatcherNotFound = Vec<String>);
::mingling::macros::pack!(#name, EmptyResult = ());
};
TokenStream::from(expanded)
}
/// Internal macro used by `gen_program!` to generate the final program enum
/// and its `ProgramCollect` implementation.
///
/// This is the core code generation macro that:
/// 1. Collects all registered types (from `pack!`, `#[derive(Groupped)]`, etc.) and
/// creates an enum with each type as a variant.
/// 2. Generates the `Display` implementation for the enum.
/// 3. Generates the `ProgramCollect` implementation that dispatches to all
/// registered renderers, chains, help handlers, completions, and general renderers.
/// 4. Adds a `new()` constructor on the enum returning `Program<EnumName>`.
///
/// The generated enum's representation type (`#[repr(u8)]`, `#[repr(u16)]`, etc.)
/// is automatically chosen based on the number of variants.
///
/// This macro is called automatically by `gen_program!` and should not
/// be called directly by user code.
///
/// # Syntax
///
/// ```rust,ignore
/// program_final_gen!(ThisProgram);
/// program_final_gen!(MyProgram);
/// ```
///
/// # Generated code structure
///
/// ```rust,ignore
/// #[repr(u8)]
/// pub enum MyProgram {
/// TypeA,
/// TypeB,
/// // ...
/// }
///
/// impl ProgramCollect for MyProgram {
/// type Enum = MyProgram;
/// type EmptyResult = EmptyResult;
/// fn render(any, r) { /* dispatches to all registered renderers */ }
/// fn do_chain(any) -> ChainProcess { /* dispatches to all registered chain steps */ }
/// fn render_help(any, r) { /* dispatches to all registered help handlers */ }
/// fn has_renderer(any) -> bool { /* checks renderer registry */ }
/// fn has_chain(any) -> bool { /* checks chain registry */ }
/// // (with comp feature) fn do_comp(...)
/// // (with general_renderer feature) fn general_render(...)
/// }
///
/// impl MyProgram {
/// pub fn new() -> Program<MyProgram> { Program::new() }
/// }
/// ```
#[proc_macro]
pub fn program_final_gen(input: TokenStream) -> TokenStream {
let name = read_name(&input);
let packed_types = PACKED_TYPES.lock().unwrap().clone();
let renderers = RENDERERS.lock().unwrap().clone();
let chains = CHAINS.lock().unwrap().clone();
let renderer_exist = RENDERERS_EXIST.lock().unwrap().clone();
let chain_exist = CHAINS_EXIST.lock().unwrap().clone();
#[cfg(feature = "general_renderer")]
let general_renderers = GENERAL_RENDERERS.lock().unwrap().clone();
#[cfg(feature = "comp")]
let completions = COMPLETIONS.lock().unwrap().clone();
let packed_types: Vec<proc_macro2::TokenStream> = packed_types
.iter()
.map(|s| syn::parse_str::<proc_macro2::TokenStream>(s).unwrap())
.collect();
let renderer_tokens: Vec<proc_macro2::TokenStream> = renderers
.iter()
.map(|s| syn::parse_str::<proc_macro2::TokenStream>(s).unwrap())
.collect();
let chain_tokens: Vec<proc_macro2::TokenStream> = chains
.iter()
.map(|s| syn::parse_str::<proc_macro2::TokenStream>(s).unwrap())
.collect();
let renderer_exist_tokens: Vec<proc_macro2::TokenStream> = renderer_exist
.iter()
.map(|s| syn::parse_str::<proc_macro2::TokenStream>(s).unwrap())
.collect();
let chain_exist_tokens: Vec<proc_macro2::TokenStream> = chain_exist
.iter()
.map(|s| syn::parse_str::<proc_macro2::TokenStream>(s).unwrap())
.collect();
#[cfg(feature = "general_renderer")]
let general_renderer_tokens: Vec<proc_macro2::TokenStream> = general_renderers
.iter()
.map(|s| syn::parse_str::<proc_macro2::TokenStream>(s).unwrap())
.collect();
#[cfg(feature = "general_renderer")]
let general_render = quote! {
fn general_render(
any: ::mingling::AnyOutput<Self::Enum>,
setting: &::mingling::GeneralRendererSetting,
) -> Result<::mingling::RenderResult, ::mingling::error::GeneralRendererSerializeError> {
match any.member_id {
#(#general_renderer_tokens)*
_ => Ok(::mingling::RenderResult::default()),
}
}
};
#[cfg(not(feature = "general_renderer"))]
let general_render = quote! {};
#[cfg(feature = "dispatch_tree")]
let compile_time_dispatchers: Vec<String> = COMPILE_TIME_DISPATCHERS
.lock()
.unwrap()
.clone()
.iter()
.cloned()
.collect();
#[cfg(feature = "dispatch_tree")]
let dispatch_tree_nodes = {
let entries: Vec<(String, String, String)> = compile_time_dispatchers
.iter()
.filter_map(|entry| {
let parts: Vec<&str> = entry.split(':').collect();
if parts.len() == 3 {
Some((
parts[0].to_string(),
parts[1].to_string(),
parts[2].to_string(),
))
} else {
None
}
})
.collect();
let get_nodes_fn = dispatch_tree_gen::gen_get_nodes(&entries);
let dispatch_trie_fn = dispatch_tree_gen::gen_dispatch_args_trie(&entries);
quote! {
#get_nodes_fn
#dispatch_trie_fn
}
};
#[cfg(not(feature = "dispatch_tree"))]
let dispatch_tree_nodes = quote! {};
#[cfg(feature = "comp")]
let completion_tokens: Vec<proc_macro2::TokenStream> = completions
.iter()
.map(|s| syn::parse_str::<proc_macro2::TokenStream>(s).unwrap())
.collect();
#[cfg(feature = "comp")]
let comp = quote! {
fn do_comp(any: &::mingling::AnyOutput<Self::Enum>, ctx: &::mingling::ShellContext) -> ::mingling::Suggest {
match any.member_id {
#(#completion_tokens)*
_ => ::mingling::Suggest::FileCompletion,
}
}
};
#[cfg(not(feature = "comp"))]
let comp = quote! {};
let help_tokens: Vec<proc_macro2::TokenStream> = HELP_REQUESTS
.lock()
.unwrap()
.clone()
.iter()
.map(|s| syn::parse_str::<proc_macro2::TokenStream>(s).unwrap())
.collect();
let num_variants = packed_types.len();
let repr_type = if num_variants <= u8::MAX as usize {
quote! { u8 }
} else if num_variants <= u16::MAX as usize {
quote! { u16 }
} else if num_variants <= u32::MAX as usize {
quote! { u32 }
} else {
quote! { u128 }
};
let expanded = quote! {
#[derive(Debug, PartialEq, Eq, Clone)]
#[repr(#repr_type)]
pub enum #name {
#(#packed_types),*
}
impl ::std::fmt::Display for #name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
match self {
#(#name::#packed_types => write!(f, stringify!(#packed_types)),)*
}
}
}
impl ::mingling::ProgramCollect for #name {
type Enum = #name;
type DispatcherNotFound = DispatcherNotFound;
type RendererNotFound = RendererNotFound;
type EmptyResult = EmptyResult;
fn build_renderer_not_found(member_id: Self::Enum) -> ::mingling::AnyOutput<Self::Enum> {
::mingling::AnyOutput::new(RendererNotFound::new(member_id.to_string()))
}
fn build_dispatcher_not_found(args: Vec<String>) -> ::mingling::AnyOutput<Self::Enum> {
::mingling::AnyOutput::new(DispatcherNotFound::new(args))
}
fn build_empty_result() -> ::mingling::AnyOutput<Self::Enum> {
::mingling::AnyOutput::new(EmptyResult::new(()))
}
::mingling::__dispatch_program_renderers!(
#(#renderer_tokens)*
);
::mingling::__dispatch_program_chains!(
#(#chain_tokens)*
);
fn render_help(any: ::mingling::AnyOutput<Self::Enum>, r: &mut ::mingling::RenderResult) {
match any.member_id {
#(#help_tokens)*
_ => (),
}
}
fn has_renderer(any: &::mingling::AnyOutput<Self::Enum>) -> bool {
match any.member_id {
#(#renderer_exist_tokens)*
_ => false
}
}
fn has_chain(any: &::mingling::AnyOutput<Self::Enum>) -> bool {
match any.member_id {
#(#chain_exist_tokens)*
_ => false
}
}
#dispatch_tree_nodes
#general_render
#comp
}
impl #name {
/// Creates a new `Program<#name>` instance with default configuration.
pub fn new() -> ::mingling::Program<#name> {
::mingling::Program::new()
}
/// Returns a static reference to the global `Program<#name>` singleton.
pub fn this() -> &'static ::mingling::Program<#name> {
&::mingling::this::<#name>()
}
}
};
TokenStream::from(expanded)
}
/// Builds a `Suggest` instance with inline suggestion items.
///
/// **This macro is only available with the `comp` feature.**
///
/// The `suggest!` macro provides a concise syntax for creating shell completion
/// suggestions. Each item can be either a simple flag or a flag with a description.
///
/// # Syntax
///
/// ```rust,ignore
/// // Empty suggestions:
/// suggest!()
///
/// // Simple flags (no description):
/// suggest! { "--flag1", "--flag2" }
///
/// // Flags with descriptions:
/// suggest! {
/// "--name": "User's name",
/// "--age": "User's age"
/// }
///
/// // Mixed:
/// suggest! {
/// "--name": "User's name",
/// "--verbose"
/// }
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::{completion, suggest};
/// use mingling::{ShellContext, Suggest};
///
/// #[completion(MyEntry)]
/// fn complete(ctx: &ShellContext) -> Suggest {
/// if ctx.typing_argument() {
/// return suggest! {
/// "--name": "Provide a name",
/// "--type": "Select a type"
/// }.strip_typed_argument(ctx);
/// }
/// suggest!()
/// }
/// ```
///
/// # Related
///
/// - `suggest_enum!`(macro.suggest_enum.html) — Build suggestions from an `EnumTag` enum.
#[cfg(feature = "comp")]
#[proc_macro]
pub fn suggest(input: TokenStream) -> TokenStream {
suggest::suggest(input)
}
/// Builds a `Suggest` instance from an `EnumTag` enum's variants.
///
/// **This macro is only available with the `comp` feature.**
///
/// The `suggest_enum!` macro iterates over all variants of an `EnumTag`-derived
/// enum and creates suggestion items using each variant's display name
/// (from `#[enum_rename]`) and description (from `#[enum_desc]`).
///
/// # Syntax
///
/// ```rust,ignore
/// suggest_enum!(MyEnumType);
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use mingling::macros::{completion, suggest_enum};
/// use mingling::{ShellContext, Suggest, EnumTag};
///
/// #[derive(EnumTag)]
/// enum Fruit {
/// #[enum_desc("A sweet red fruit")]
/// #[enum_rename("apple")]
/// Apple,
/// #[enum_desc("A yellow tropical fruit")]
/// #[enum_rename("banana")]
/// Banana,
/// }
///
/// #[completion(MyEntry)]
/// fn complete(ctx: &ShellContext) -> Suggest {
/// if ctx.filling_argument_first("--fruit") {
/// return suggest_enum!(Fruit);
/// }
/// suggest!()
/// }
/// ```
///
/// # Generated code equivalent
///
/// ```rust,ignore
/// {
/// let mut enum_suggest = Suggest::new();
/// for (name, desc) in <Fruit>::enums() {
/// if desc.is_empty() {
/// enum_suggest.insert(SuggestItem::new(name.to_string()));
/// } else {
/// enum_suggest.insert(SuggestItem::new_with_desc(name.to_string(), desc.to_string()));
/// }
/// }
/// enum_suggest
/// }
/// ```
///
/// # Related
///
/// - `suggest!`(macro.suggest.html) — Build suggestions with inline syntax.
/// - `EnumTag`(derive.EnumTag.html) — The derive macro required for the enum type.
#[cfg(feature = "comp")]
#[proc_macro]
pub fn suggest_enum(input: TokenStream) -> TokenStream {
suggest::suggest_enum(input)
}
fn read_name(input: &TokenStream) -> Ident {
if input.is_empty() {
Ident::new(DEFAULT_PROGRAM_NAME, proc_macro2::Span::call_site())
} else {
syn::parse(input.clone()).unwrap()
}
}
|