facet-macros-impl 0.46.1

Implementation of facet derive macros (parsing and code generation)
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
//! Plugin system for facet derive macro.
//!
//! This module implements the plugin chain pattern that allows external crates
//! to hook into `#[derive(Facet)]` and generate additional trait implementations.
//!
//! ## How it works
//!
//! 1. User writes `#[derive(Facet)]` with `#[facet(derive(Error))]`
//! 2. `facet_macros` detects the `derive(...)` attribute
//! 3. It chains to the first plugin: `::facet_error::__facet_derive!`
//! 4. Each plugin adds itself to the chain and forwards to the next (or finalize)
//! 5. `__facet_finalize!` parses ONCE and generates all code
//!
//! ## Plugin naming convention
//!
//! `#[facet(derive(Foo))]` maps to `::facet_foo::__facet_derive!`
//! (lowercase the trait name, prefix with `facet_`)

use crate::{Attribute, AttributeInner, FacetInner, IParse, Ident, ToTokenIter, TokenStream};
use quote::quote;

/// A plugin reference - either a simple name or a full path.
///
/// - `Error` → convention-based lookup (`::facet_error`)
/// - `some_crate::SomeTrait` → explicit path (`::some_crate`)
#[derive(Debug, Clone)]
pub enum PluginRef {
    /// Simple name like `Error` - uses convention `::facet_{snake_case}`
    Simple(String),
    /// Explicit path like `some_crate::SomeTrait` - uses the crate part directly
    Path {
        /// The crate name (e.g., `some_crate`)
        crate_name: String,
        /// The plugin/trait name (e.g., `SomeTrait`)
        plugin_name: String,
    },
}

impl PluginRef {
    /// Get the crate path for this plugin reference.
    pub fn crate_path(&self) -> TokenStream {
        match self {
            PluginRef::Simple(name) => {
                let snake_case = to_snake_case(name);
                let crate_name = format!("facet_{snake_case}");
                let crate_ident = quote::format_ident!("{}", crate_name);
                quote! { ::#crate_ident }
            }
            PluginRef::Path { crate_name, .. } => {
                let crate_ident = quote::format_ident!("{}", crate_name);
                quote! { ::#crate_ident }
            }
        }
    }
}

/// Extract plugin references from `#[facet(derive(Plugin1, Plugin2, ...))]` attributes.
///
/// Supports both simple names and explicit paths:
/// - `#[facet(derive(Error))]` → `PluginRef::Simple("Error")`
/// - `#[facet(derive(some_crate::SomeTrait))]` → `PluginRef::Path { crate_name: "some_crate", plugin_name: "SomeTrait" }`
pub fn extract_derive_plugins(attrs: &[Attribute]) -> Vec<PluginRef> {
    let mut plugins = Vec::new();

    for attr in attrs {
        if let AttributeInner::Facet(facet_attr) = &attr.body.content {
            for inner in facet_attr.inner.content.iter().map(|d| &d.value) {
                if let FacetInner::Simple(simple) = inner
                    && simple.key == "derive"
                {
                    // Parse the args to get plugin names
                    if let Some(ref args) = simple.args {
                        match args {
                            crate::AttrArgs::Parens(parens) => {
                                // Parse comma-separated items (either idents or paths)
                                plugins.extend(parse_plugin_list(&parens.content));
                            }
                            crate::AttrArgs::Equals(_) => {
                                // derive = Something syntax (unusual but handle it)
                            }
                        }
                    }
                }
            }
        }
    }

    plugins
}

/// Parse a comma-separated list of plugin references (idents or paths).
fn parse_plugin_list(tokens: &[crate::TokenTree]) -> Vec<PluginRef> {
    let mut plugins = Vec::new();
    let mut iter = tokens.iter().cloned().peekable();

    while iter.peek().is_some() {
        // Collect tokens until comma or end
        let mut item_tokens = Vec::new();
        while let Some(tt) = iter.peek() {
            if let proc_macro2::TokenTree::Punct(p) = tt
                && p.as_char() == ','
            {
                iter.next(); // consume comma
                break;
            }
            item_tokens.push(iter.next().unwrap());
        }

        // Parse the collected tokens as either a simple ident or a path
        if let Some(plugin_ref) = parse_plugin_ref(&item_tokens) {
            plugins.push(plugin_ref);
        }
    }

    plugins
}

/// Parse a single plugin reference from a sequence of tokens.
fn parse_plugin_ref(tokens: &[proc_macro2::TokenTree]) -> Option<PluginRef> {
    if tokens.is_empty() {
        return None;
    }

    // Check if it's a path (contains ::)
    let has_path_sep = tokens.windows(2).any(|w| {
        matches!((&w[0], &w[1]),
            (proc_macro2::TokenTree::Punct(p1), proc_macro2::TokenTree::Punct(p2))
            if p1.as_char() == ':' && p2.as_char() == ':')
    });

    if has_path_sep {
        // Parse as path: crate_name::PluginName
        // For now, just support single-segment crate name
        let mut iter = tokens.iter();

        // First ident is crate name
        let crate_name = match iter.next() {
            Some(proc_macro2::TokenTree::Ident(id)) => id.to_string(),
            _ => return None,
        };

        // Skip ::
        match (iter.next(), iter.next()) {
            (Some(proc_macro2::TokenTree::Punct(p1)), Some(proc_macro2::TokenTree::Punct(p2)))
                if p1.as_char() == ':' && p2.as_char() == ':' => {}
            _ => return None,
        }

        // Last ident is plugin name
        let plugin_name = match iter.next() {
            Some(proc_macro2::TokenTree::Ident(id)) => id.to_string(),
            _ => return None,
        };

        Some(PluginRef::Path {
            crate_name,
            plugin_name,
        })
    } else {
        // Simple ident
        match tokens.first() {
            Some(proc_macro2::TokenTree::Ident(id)) => Some(PluginRef::Simple(id.to_string())),
            _ => None,
        }
    }
}

/// Convert a plugin name to its crate path.
///
/// `Error` → `::facet_error`
/// `Display` → `::facet_display`
pub fn plugin_to_crate_path(plugin_name: &str) -> TokenStream {
    // Convert PascalCase to snake_case and prefix with facet_
    let snake_case = to_snake_case(plugin_name);
    let crate_name = format!("facet_{snake_case}");
    let crate_ident = quote::format_ident!("{}", crate_name);
    quote! { ::#crate_ident }
}

/// Convert PascalCase to snake_case.
fn to_snake_case(s: &str) -> String {
    let mut result = String::new();
    for (i, c) in s.chars().enumerate() {
        if c.is_uppercase() {
            if i > 0 {
                result.push('_');
            }
            result.push(c.to_ascii_lowercase());
        } else {
            result.push(c);
        }
    }
    result
}

/// Strip `#[facet(derive(...))]` and plugin-specific attributes from a token stream.
///
/// This filters out the plugin-system-specific attributes before passing
/// the tokens to the normal Facet processing, which would otherwise reject
/// "derive" as an unknown attribute.
///
/// Currently strips:
/// - `derive(...)` - plugin registration
/// - `error::from` - facet-error plugin attribute
/// - `error::source` - facet-error plugin attribute
/// - Any `namespace::key` pattern (for future plugins)
///
/// Handles combined attributes like `#[facet(rename_all = "...", derive(Default))]`
/// by removing only the plugin-specific parts and keeping other attributes.
fn strip_derive_attrs(tokens: TokenStream) -> TokenStream {
    let mut result = TokenStream::new();
    let mut iter = tokens.into_iter().peekable();

    while let Some(tt) = iter.next() {
        // Check for # followed by [...]
        if let proc_macro2::TokenTree::Punct(p) = &tt
            && p.as_char() == '#'
            && let Some(proc_macro2::TokenTree::Group(g)) = iter.peek()
            && g.delimiter() == proc_macro2::Delimiter::Bracket
        {
            // This is an attribute - check if it's a facet attribute
            let inner = g.stream();
            if let Some(filtered) = strip_plugin_items_from_facet_attr(&inner) {
                if filtered.is_empty() {
                    // All items were stripped - skip the entire attribute
                    iter.next(); // consume the group
                    continue;
                } else {
                    // Some items remain - emit the filtered attribute
                    result.extend(std::iter::once(tt));
                    iter.next(); // consume the original group
                    result.extend(std::iter::once(proc_macro2::TokenTree::Group(
                        proc_macro2::Group::new(proc_macro2::Delimiter::Bracket, filtered),
                    )));
                    continue;
                }
            }
        }
        result.extend(std::iter::once(tt));
    }

    result
}

/// Strip plugin-specific items from inside a facet attribute.
///
/// Returns Some(filtered_tokens) if this is a facet attribute, None otherwise.
/// The filtered_tokens will have plugin items removed (derive, namespace::key).
/// If all items are plugin items, returns Some(empty stream).
fn strip_plugin_items_from_facet_attr(inner: &TokenStream) -> Option<TokenStream> {
    let mut iter = inner.clone().into_iter().peekable();

    // Check for "facet" identifier
    let facet_ident = match iter.next() {
        Some(proc_macro2::TokenTree::Ident(id)) if id == "facet" => id,
        _ => return None,
    };

    // Check for (...) group
    let group = match iter.next() {
        Some(proc_macro2::TokenTree::Group(g))
            if g.delimiter() == proc_macro2::Delimiter::Parenthesis =>
        {
            g
        }
        _ => return None,
    };

    // Parse and filter the items inside facet(...)
    let filtered_content = strip_plugin_items_from_content(group.stream());

    // Reconstruct the attribute
    let mut result = TokenStream::new();
    result.extend(std::iter::once(proc_macro2::TokenTree::Ident(facet_ident)));
    result.extend(std::iter::once(proc_macro2::TokenTree::Group(
        proc_macro2::Group::new(proc_macro2::Delimiter::Parenthesis, filtered_content),
    )));

    Some(result)
}

/// Strip plugin-specific items from the content of a facet(...) attribute.
///
/// Items are comma-separated. Plugin items are:
/// - `derive(...)` - plugin registration
/// - `namespace::key` patterns (e.g., error::from, error::source)
fn strip_plugin_items_from_content(content: TokenStream) -> TokenStream {
    let mut items: Vec<TokenStream> = Vec::new();

    // Parse comma-separated items
    let mut current_item = TokenStream::new();
    let tokens: Vec<proc_macro2::TokenTree> = content.into_iter().collect();

    for tt in &tokens {
        // Check for comma separator
        if let proc_macro2::TokenTree::Punct(p) = tt
            && p.as_char() == ','
        {
            // End of current item
            if !current_item.is_empty() && !is_plugin_item(&current_item) {
                items.push(current_item);
            }
            current_item = TokenStream::new();
            continue;
        }

        current_item.extend(std::iter::once(tt.clone()));
    }

    // Don't forget the last item
    if !current_item.is_empty() && !is_plugin_item(&current_item) {
        items.push(current_item);
    }

    // Reconstruct with commas
    let mut result = TokenStream::new();
    for (idx, item) in items.iter().enumerate() {
        if idx > 0 {
            result.extend(std::iter::once(proc_macro2::TokenTree::Punct(
                proc_macro2::Punct::new(',', proc_macro2::Spacing::Alone),
            )));
        }
        result.extend(item.clone());
    }

    result
}

/// Check if an item within facet(...) is a plugin-specific item.
///
/// Returns true for:
/// - `derive(...)` - plugin registration
///
/// NOTE: We intentionally do NOT strip `namespace::key` patterns here.
/// Extension attributes like `args::positional`, `dibs::table`, `error::from`
/// must be preserved so they can be processed by the Facet derive and stored
/// in the Shape's attributes field.
fn is_plugin_item(item: &TokenStream) -> bool {
    let mut iter = item.clone().into_iter();

    if let Some(proc_macro2::TokenTree::Ident(id)) = iter.next() {
        let name = id.to_string();

        // Check for derive(...)
        if name == "derive" {
            return true;
        }
    }

    false
}

/// Generate the plugin chain invocation.
///
/// If there are plugins, emits a chain starting with the first plugin.
/// If no plugins, returns None (caller should proceed with normal codegen).
pub fn generate_plugin_chain(
    input_tokens: &TokenStream,
    plugins: &[PluginRef],
    facet_crate: &TokenStream,
) -> Option<TokenStream> {
    if plugins.is_empty() {
        return None;
    }

    // Build the chain from right to left
    // First plugin gets called with remaining plugins
    let plugin_paths: Vec<TokenStream> = plugins
        .iter()
        .map(|p| {
            let crate_path = p.crate_path();
            quote! { #crate_path::__facet_invoke }
        })
        .collect();

    let first = &plugin_paths[0];
    let rest: Vec<_> = plugin_paths[1..].iter().collect();

    let remaining = if rest.is_empty() {
        quote! {}
    } else {
        quote! { #(#rest),* }
    };

    Some(quote! {
        #first! {
            @tokens { #input_tokens }
            @remaining { #remaining }
            @plugins { }
            @facet_crate { #facet_crate }
        }
    })
}

/// Implementation of `__facet_finalize!` proc macro.
///
/// This is called at the end of the plugin chain. It:
/// 1. Parses the type definition ONCE
/// 2. Generates the base Facet impl
/// 3. Evaluates each plugin's template against the parsed type
pub fn facet_finalize(input: TokenStream) -> TokenStream {
    // Parse the finalize invocation format:
    // @tokens { ... }
    // @plugins { @plugin { @name {...} @template {...} } ... }
    // @facet_crate { ::facet }

    let mut iter = input.to_token_iter();

    let mut tokens: Option<TokenStream> = None;
    let mut plugins_section: Option<TokenStream> = None;
    let mut facet_crate: Option<TokenStream> = None;

    // Parse sections
    while let Ok(section) = iter.parse::<FinalizeSection>() {
        match section.marker.name.to_string().as_str() {
            "tokens" => {
                tokens = Some(section.content.content);
            }
            "plugins" => {
                plugins_section = Some(section.content.content);
            }
            "facet_crate" => {
                facet_crate = Some(section.content.content);
            }
            other => {
                let msg = format!("unknown section in __facet_finalize: @{other}");
                return quote! { compile_error!(#msg); };
            }
        }
    }

    let tokens = match tokens {
        Some(t) => t,
        None => {
            return quote! { compile_error!("__facet_finalize: missing @tokens section"); };
        }
    };

    let facet_crate = facet_crate.unwrap_or_else(|| quote! { ::facet });

    // Strip #[facet(derive(...))] attributes before processing
    let filtered_tokens = strip_derive_attrs(tokens.clone());

    // Parse the type and generate Facet impl
    let mut type_iter = filtered_tokens.clone().to_token_iter();
    let facet_impl = match type_iter.parse::<crate::Cons<crate::AdtDecl, crate::EndOfStream>>() {
        Ok(it) => match it.first {
            crate::AdtDecl::Struct(parsed) => crate::process_struct::process_struct(parsed),
            crate::AdtDecl::Enum(parsed) => crate::process_enum::process_enum(parsed),
        },
        Err(err) => {
            let msg = format!("__facet_finalize: could not parse type: {err}");
            return quote! { compile_error!(#msg); };
        }
    };

    // Extract and evaluate plugin templates
    let plugin_impls = if let Some(plugins_tokens) = plugins_section {
        // For now, just extract the templates - evaluation will come next
        extract_plugin_templates(plugins_tokens, &filtered_tokens, &facet_crate)
    } else {
        vec![]
    };

    quote! {
        #facet_impl
        #(#plugin_impls)*
    }
}

/// Represents a parsed plugin with its template
struct PluginTemplate {
    #[allow(dead_code)] // Will be used for debugging/diagnostics
    name: String,
    template: TokenStream,
}

/// Extract plugin templates from the @plugins section
fn extract_plugin_templates(
    plugins_tokens: TokenStream,
    type_tokens: &TokenStream,
    facet_crate: &TokenStream,
) -> Vec<TokenStream> {
    // Parse plugin sections
    let plugins = parse_plugin_sections(plugins_tokens);

    // Parse the type once for all plugins
    let parsed_type = match facet_macro_parse::parse_type(type_tokens.clone()) {
        Ok(ty) => ty,
        Err(e) => {
            let msg = format!("failed to parse type for plugin templates: {e}");
            return vec![quote! { compile_error!(#msg); }];
        }
    };

    // Evaluate each plugin's template
    plugins
        .into_iter()
        .map(|plugin| evaluate_template(plugin.template, &parsed_type, facet_crate))
        .collect()
}

/// Parse @plugin { @name {...} @template {...} } sections
fn parse_plugin_sections(tokens: TokenStream) -> Vec<PluginTemplate> {
    let mut plugins = Vec::new();
    let mut iter = tokens.into_iter().peekable();

    while let Some(tt) = iter.next() {
        // Look for @plugin marker
        if let proc_macro2::TokenTree::Punct(p) = &tt
            && p.as_char() == '@'
        {
            // Next should be 'plugin' identifier
            if let Some(proc_macro2::TokenTree::Ident(id)) = iter.peek()
                && *id == "plugin"
            {
                iter.next(); // consume 'plugin'

                // Next should be { ... } containing @name and @template
                if let Some(proc_macro2::TokenTree::Group(g)) = iter.next()
                    && g.delimiter() == proc_macro2::Delimiter::Brace
                    && let Some(plugin) = parse_plugin_content(g.stream())
                {
                    plugins.push(plugin);
                }
            }
        }
    }

    plugins
}

/// Parse the content of a @plugin { ... } section
fn parse_plugin_content(tokens: TokenStream) -> Option<PluginTemplate> {
    let mut name: Option<String> = None;
    let mut template: Option<TokenStream> = None;
    let mut iter = tokens.into_iter().peekable();

    while let Some(tt) = iter.next() {
        if let proc_macro2::TokenTree::Punct(p) = &tt
            && p.as_char() == '@'
            && let Some(proc_macro2::TokenTree::Ident(id)) = iter.peek()
        {
            let key = id.to_string();
            iter.next(); // consume identifier

            // Next should be { ... }
            if let Some(proc_macro2::TokenTree::Group(g)) = iter.next()
                && g.delimiter() == proc_macro2::Delimiter::Brace
            {
                match key.as_str() {
                    "name" => {
                        // Extract string literal from group
                        let content = g.stream().into_iter().collect::<Vec<_>>();
                        if let Some(proc_macro2::TokenTree::Literal(lit)) = content.first() {
                            let s = lit.to_string();
                            name = Some(s.trim_matches('"').to_string());
                        }
                    }
                    "template" => {
                        template = Some(g.stream());
                    }
                    _ => {}
                }
            }
        }
    }

    match (name, template) {
        (Some(n), Some(t)) => Some(PluginTemplate {
            name: n,
            template: t,
        }),
        _ => None,
    }
}

/// Evaluate a template against a parsed type
fn evaluate_template(
    template: TokenStream,
    parsed_type: &facet_macro_parse::PType,
    _facet_crate: &TokenStream,
) -> TokenStream {
    let mut ctx = EvalContext::new(parsed_type);
    evaluate_with_context(template, &mut ctx)
}

// ============================================================================
// CONTEXT STACK
// ============================================================================

/// The evaluation context - a stack of nested scopes.
///
/// As we enter `@for_variant`, `@for_field`, `@if_attr`, etc., we push
/// context frames. Directives like `@field_name` look up the stack to find
/// the relevant context.
struct EvalContext<'a> {
    /// The parsed type we're generating code for
    parsed_type: &'a facet_macro_parse::PType,

    /// Stack of context frames (innermost last)
    stack: Vec<ContextFrame<'a>>,
}

/// A single frame in the context stack
enum ContextFrame<'a> {
    /// We're inside a `@for_variant { ... }` loop
    Variant {
        variant: &'a facet_macro_parse::PVariant,
    },

    /// We're inside a `@for_field { ... }` loop
    Field {
        field: &'a facet_macro_parse::PStructField,
        /// Index of this field (for tuple patterns like `__v0`, `__v1`)
        index: usize,
    },

    /// We're inside a `@if_attr(ns::key) { ... }` block
    Attr {
        /// The matched attribute
        attr: &'a facet_macro_parse::PFacetAttr,
    },
}

impl<'a> EvalContext<'a> {
    const fn new(parsed_type: &'a facet_macro_parse::PType) -> Self {
        Self {
            parsed_type,
            stack: Vec::new(),
        }
    }

    fn push(&mut self, frame: ContextFrame<'a>) {
        self.stack.push(frame);
    }

    fn pop(&mut self) {
        self.stack.pop();
    }

    /// Find the current variant context (if any)
    fn current_variant(&self) -> Option<&'a facet_macro_parse::PVariant> {
        self.stack.iter().rev().find_map(|f| match f {
            ContextFrame::Variant { variant } => Some(*variant),
            _ => None,
        })
    }

    /// Find the current field context (if any)
    fn current_field(&self) -> Option<(&'a facet_macro_parse::PStructField, usize)> {
        self.stack.iter().rev().find_map(|f| match f {
            ContextFrame::Field { field, index } => Some((*field, *index)),
            _ => None,
        })
    }

    /// Find the current attr context (if any)
    fn current_attr(&self) -> Option<&'a facet_macro_parse::PFacetAttr> {
        self.stack.iter().rev().find_map(|f| match f {
            ContextFrame::Attr { attr } => Some(*attr),
            _ => None,
        })
    }

    /// Get the fields of the current context (variant's fields or struct's fields)
    fn current_fields(&self) -> Option<&'a [facet_macro_parse::PStructField]> {
        // First check if we're in a variant
        if let Some(variant) = self.current_variant() {
            return match &variant.kind {
                facet_macro_parse::PVariantKind::Tuple { fields } => Some(fields),
                facet_macro_parse::PVariantKind::Struct { fields } => Some(fields),
                facet_macro_parse::PVariantKind::Unit => None,
            };
        }

        // Otherwise, check if we're in a struct
        if let facet_macro_parse::PType::Struct(s) = self.parsed_type {
            return match &s.kind {
                facet_macro_parse::PStructKind::Struct { fields } => Some(fields),
                facet_macro_parse::PStructKind::TupleStruct { fields } => Some(fields),
                facet_macro_parse::PStructKind::UnitStruct => None,
            };
        }

        None
    }

    /// Get the attrs of the current context (field, variant, or container)
    fn current_attrs(&self) -> &'a facet_macro_parse::PAttrs {
        // Check field first (most specific)
        if let Some((field, _)) = self.current_field() {
            return &field.attrs;
        }

        // Then variant
        if let Some(variant) = self.current_variant() {
            return &variant.attrs;
        }

        // Finally container
        match self.parsed_type {
            facet_macro_parse::PType::Struct(s) => &s.container.attrs,
            facet_macro_parse::PType::Enum(e) => &e.container.attrs,
        }
    }
}

// ============================================================================
// ATTRIBUTE QUERY
// ============================================================================

/// Parsed attribute query like `error::source` or `diagnostic::label`
struct AttrQuery {
    ns: String,
    key: String,
}

impl AttrQuery {
    /// Parse from tokens like `error::source` or `diagnostic::label`
    fn parse(tokens: TokenStream) -> Option<Self> {
        let mut iter = tokens.into_iter();

        // First: namespace ident
        let ns = match iter.next() {
            Some(proc_macro2::TokenTree::Ident(id)) => id.to_string(),
            _ => return None,
        };

        // Then: ::
        match (iter.next(), iter.next()) {
            (Some(proc_macro2::TokenTree::Punct(p1)), Some(proc_macro2::TokenTree::Punct(p2)))
                if p1.as_char() == ':' && p2.as_char() == ':' => {}
            _ => return None,
        }

        // Then: key ident
        let key = match iter.next() {
            Some(proc_macro2::TokenTree::Ident(id)) => id.to_string(),
            _ => return None,
        };

        Some(AttrQuery { ns, key })
    }

    /// Check if an attribute matches this query
    fn matches(&self, attr: &facet_macro_parse::PFacetAttr) -> bool {
        if let Some(ref ns) = attr.ns {
            *ns == self.ns && attr.key == self.key
        } else {
            false
        }
    }

    /// Find matching attribute in a list
    fn find_in<'a>(
        &self,
        attrs: &'a [facet_macro_parse::PFacetAttr],
    ) -> Option<&'a facet_macro_parse::PFacetAttr> {
        attrs.iter().find(|a| self.matches(a))
    }
}

// ============================================================================
// TEMPLATE EVALUATION
// ============================================================================

/// Evaluate a template with the given context
fn evaluate_with_context(template: TokenStream, ctx: &mut EvalContext<'_>) -> TokenStream {
    let mut output = TokenStream::new();
    let mut iter = template.into_iter().peekable();

    while let Some(tt) = iter.next() {
        match &tt {
            proc_macro2::TokenTree::Punct(p) if p.as_char() == '@' => {
                handle_directive(&mut iter, ctx, &mut output);
            }
            proc_macro2::TokenTree::Group(g) => {
                // Recursively evaluate groups
                let inner = evaluate_with_context(g.stream(), ctx);
                let new_group = proc_macro2::Group::new(g.delimiter(), inner);
                output.extend(std::iter::once(proc_macro2::TokenTree::Group(new_group)));
            }
            _ => {
                output.extend(std::iter::once(tt));
            }
        }
    }

    output
}

/// Handle a directive after seeing `@`
fn handle_directive(
    iter: &mut std::iter::Peekable<proc_macro2::token_stream::IntoIter>,
    ctx: &mut EvalContext<'_>,
    output: &mut TokenStream,
) {
    let Some(next) = iter.next() else {
        // @ at end of stream - emit it
        output.extend(quote! { @ });
        return;
    };

    let proc_macro2::TokenTree::Ident(directive_ident) = &next else {
        // Not an ident - emit @ and the token
        output.extend(quote! { @ });
        output.extend(std::iter::once(next));
        return;
    };

    let directive = directive_ident.to_string();

    match directive.as_str() {
        // === Type-level directives ===
        "Self" => emit_self_type(ctx, output),

        // === Looping directives ===
        "for_variant" => handle_for_variant(iter, ctx, output),
        "for_field" => handle_for_field(iter, ctx, output),

        // === Conditional directives ===
        "if_attr" => handle_if_attr(iter, ctx, output),
        "if_field_attr" => handle_if_field_attr(iter, ctx, output),
        "if_any_field_attr" => handle_if_any_field_attr(iter, ctx, output),
        "if_struct" => handle_if_struct(iter, ctx, output),
        "if_enum" => handle_if_enum(iter, ctx, output),
        "if_unit_variant" => handle_if_unit_variant(iter, ctx, output),
        "if_tuple_variant" => handle_if_tuple_variant(iter, ctx, output),
        "if_struct_variant" => handle_if_struct_variant(iter, ctx, output),

        // === Context accessors ===
        "variant_name" => emit_variant_name(ctx, output),
        "variant_pattern" => emit_variant_pattern(ctx, output),
        "variant_pattern_only" => handle_variant_pattern_only(iter, ctx, output),
        "field_name" => emit_field_name(ctx, output),
        "field_type" => emit_field_type(ctx, output),
        "field_expr" => emit_field_expr(ctx, output),
        "attr_args" => emit_attr_args(ctx, output),
        "doc" => emit_doc(ctx, output),

        // === Default-related directives ===
        "field_default_expr" => emit_field_default_expr(ctx, output),
        "variant_default_construction" => emit_variant_default_construction(ctx, output),

        // === Display-related directives ===
        "format_doc_comment" => emit_format_doc_comment(ctx, output),

        // === Unknown directive ===
        _ => {
            // Emit as-is (might be user code with @ symbol)
            output.extend(quote! { @ });
            output.extend(std::iter::once(next.clone()));
        }
    }
}

// ============================================================================
// TYPE-LEVEL DIRECTIVES
// ============================================================================

fn emit_self_type(ctx: &EvalContext<'_>, output: &mut TokenStream) {
    let name = ctx.parsed_type.name();
    output.extend(quote! { #name });
}

// ============================================================================
// LOOPING DIRECTIVES
// ============================================================================

/// `@for_variant { ... }` - loop over enum variants
fn handle_for_variant(
    iter: &mut std::iter::Peekable<proc_macro2::token_stream::IntoIter>,
    ctx: &mut EvalContext<'_>,
    output: &mut TokenStream,
) {
    let Some(proc_macro2::TokenTree::Group(body_group)) = iter.next() else {
        return; // Malformed - no body
    };

    let body = body_group.stream();

    // Only works for enums
    let facet_macro_parse::PType::Enum(e) = ctx.parsed_type else {
        return;
    };

    for variant in &e.variants {
        ctx.push(ContextFrame::Variant { variant });
        let expanded = evaluate_with_context(body.clone(), ctx);
        output.extend(expanded);
        ctx.pop();
    }
}

/// `@for_field { ... }` - loop over fields of current context
fn handle_for_field(
    iter: &mut std::iter::Peekable<proc_macro2::token_stream::IntoIter>,
    ctx: &mut EvalContext<'_>,
    output: &mut TokenStream,
) {
    let Some(proc_macro2::TokenTree::Group(body_group)) = iter.next() else {
        return;
    };

    let body = body_group.stream();

    let Some(fields) = ctx.current_fields() else {
        return;
    };

    // Need to collect to avoid borrow issues
    let fields: Vec<_> = fields.iter().enumerate().collect();

    for (index, field) in fields {
        ctx.push(ContextFrame::Field { field, index });
        let expanded = evaluate_with_context(body.clone(), ctx);
        output.extend(expanded);
        ctx.pop();
    }
}

// ============================================================================
// CONDITIONAL DIRECTIVES
// ============================================================================

/// `@if_attr(ns::key) { ... }` - conditional on current context having attr
fn handle_if_attr(
    iter: &mut std::iter::Peekable<proc_macro2::token_stream::IntoIter>,
    ctx: &mut EvalContext<'_>,
    output: &mut TokenStream,
) {
    // Parse (ns::key)
    let Some(proc_macro2::TokenTree::Group(query_group)) = iter.next() else {
        return;
    };

    // Parse { body }
    let Some(proc_macro2::TokenTree::Group(body_group)) = iter.next() else {
        return;
    };

    let Some(query) = AttrQuery::parse(query_group.stream()) else {
        return;
    };

    let attrs = ctx.current_attrs();

    if let Some(matched_attr) = query.find_in(&attrs.facet) {
        ctx.push(ContextFrame::Attr { attr: matched_attr });
        let expanded = evaluate_with_context(body_group.stream(), ctx);
        output.extend(expanded);
        ctx.pop();
    }
}

/// `@if_field_attr(ns::key) { ... }` - find field with attr, bind field context
///
/// This is a combined "find + bind" - it searches all fields in current context
/// for one with the given attribute, and if found, enters both field and attr context.
fn handle_if_field_attr(
    iter: &mut std::iter::Peekable<proc_macro2::token_stream::IntoIter>,
    ctx: &mut EvalContext<'_>,
    output: &mut TokenStream,
) {
    let Some(proc_macro2::TokenTree::Group(query_group)) = iter.next() else {
        return;
    };

    let Some(proc_macro2::TokenTree::Group(body_group)) = iter.next() else {
        return;
    };

    let Some(query) = AttrQuery::parse(query_group.stream()) else {
        return;
    };

    let Some(fields) = ctx.current_fields() else {
        return;
    };

    // Need to collect to avoid borrow issues
    let fields: Vec<_> = fields.iter().enumerate().collect();

    // Find first field with matching attr
    for (index, field) in fields {
        if let Some(matched_attr) = query.find_in(&field.attrs.facet) {
            ctx.push(ContextFrame::Field { field, index });
            ctx.push(ContextFrame::Attr { attr: matched_attr });
            let expanded = evaluate_with_context(body_group.stream(), ctx);
            output.extend(expanded);
            ctx.pop(); // attr
            ctx.pop(); // field
            return; // Only emit once for first match
        }
    }
}

/// `@if_any_field_attr(ns::key) { ... }` - conditional if ANY field has attr
///
/// Unlike `@if_field_attr`, this doesn't bind a specific field - it just checks
/// if any field has the attribute. Useful for wrapping a `@for_field` that will
/// check each field individually.
fn handle_if_any_field_attr(
    iter: &mut std::iter::Peekable<proc_macro2::token_stream::IntoIter>,
    ctx: &mut EvalContext<'_>,
    output: &mut TokenStream,
) {
    let Some(proc_macro2::TokenTree::Group(query_group)) = iter.next() else {
        return;
    };

    let Some(proc_macro2::TokenTree::Group(body_group)) = iter.next() else {
        return;
    };

    let Some(query) = AttrQuery::parse(query_group.stream()) else {
        return;
    };

    let Some(fields) = ctx.current_fields() else {
        return;
    };

    // Check if any field has the attr
    let has_any = fields
        .iter()
        .any(|f| query.find_in(&f.attrs.facet).is_some());

    if has_any {
        let expanded = evaluate_with_context(body_group.stream(), ctx);
        output.extend(expanded);
    }
}

/// `@if_struct { ... }` - emit body only for struct types
fn handle_if_struct(
    iter: &mut std::iter::Peekable<proc_macro2::token_stream::IntoIter>,
    ctx: &mut EvalContext<'_>,
    output: &mut TokenStream,
) {
    let Some(proc_macro2::TokenTree::Group(body_group)) = iter.next() else {
        return;
    };

    if matches!(ctx.parsed_type, facet_macro_parse::PType::Struct(_)) {
        let expanded = evaluate_with_context(body_group.stream(), ctx);
        output.extend(expanded);
    }
}

/// `@if_enum { ... }` - emit body only for enum types
fn handle_if_enum(
    iter: &mut std::iter::Peekable<proc_macro2::token_stream::IntoIter>,
    ctx: &mut EvalContext<'_>,
    output: &mut TokenStream,
) {
    let Some(proc_macro2::TokenTree::Group(body_group)) = iter.next() else {
        return;
    };

    if matches!(ctx.parsed_type, facet_macro_parse::PType::Enum(_)) {
        let expanded = evaluate_with_context(body_group.stream(), ctx);
        output.extend(expanded);
    }
}

/// `@if_unit_variant { ... }` - conditional on current variant being a unit variant
fn handle_if_unit_variant(
    iter: &mut std::iter::Peekable<proc_macro2::token_stream::IntoIter>,
    ctx: &mut EvalContext<'_>,
    output: &mut TokenStream,
) {
    let Some(proc_macro2::TokenTree::Group(body_group)) = iter.next() else {
        return;
    };

    let Some(variant) = ctx.current_variant() else {
        return;
    };

    if matches!(variant.kind, facet_macro_parse::PVariantKind::Unit) {
        let expanded = evaluate_with_context(body_group.stream(), ctx);
        output.extend(expanded);
    }
}

/// `@if_tuple_variant { ... }` - conditional on current variant being a tuple variant
fn handle_if_tuple_variant(
    iter: &mut std::iter::Peekable<proc_macro2::token_stream::IntoIter>,
    ctx: &mut EvalContext<'_>,
    output: &mut TokenStream,
) {
    let Some(proc_macro2::TokenTree::Group(body_group)) = iter.next() else {
        return;
    };

    let Some(variant) = ctx.current_variant() else {
        return;
    };

    if matches!(variant.kind, facet_macro_parse::PVariantKind::Tuple { .. }) {
        let expanded = evaluate_with_context(body_group.stream(), ctx);
        output.extend(expanded);
    }
}

/// `@if_struct_variant { ... }` - conditional on current variant being a struct variant
fn handle_if_struct_variant(
    iter: &mut std::iter::Peekable<proc_macro2::token_stream::IntoIter>,
    ctx: &mut EvalContext<'_>,
    output: &mut TokenStream,
) {
    let Some(proc_macro2::TokenTree::Group(body_group)) = iter.next() else {
        return;
    };

    let Some(variant) = ctx.current_variant() else {
        return;
    };

    if matches!(variant.kind, facet_macro_parse::PVariantKind::Struct { .. }) {
        let expanded = evaluate_with_context(body_group.stream(), ctx);
        output.extend(expanded);
    }
}

// ============================================================================
// CONTEXT ACCESSORS
// ============================================================================

fn emit_variant_name(ctx: &EvalContext<'_>, output: &mut TokenStream) {
    if let Some(variant) = ctx.current_variant()
        && let facet_macro_parse::IdentOrLiteral::Ident(name) = &variant.name.raw
    {
        output.extend(quote! { #name });
    }
}

fn emit_variant_pattern(ctx: &EvalContext<'_>, output: &mut TokenStream) {
    let Some(variant) = ctx.current_variant() else {
        return;
    };

    use facet_macro_parse::{IdentOrLiteral, PVariantKind};

    match &variant.kind {
        PVariantKind::Unit => {
            // No pattern needed for unit variants
        }
        PVariantKind::Tuple { fields } => {
            // Use v0, v1, etc. for legacy compatibility with @format_doc_comment
            // (which uses {0}, {1} placeholders that expect v0, v1, etc.)
            let names: Vec<_> = (0..fields.len())
                .map(|i| quote::format_ident!("v{}", i))
                .collect();
            output.extend(quote! { ( #(#names),* ) });
        }
        PVariantKind::Struct { fields } => {
            let names: Vec<_> = fields
                .iter()
                .filter_map(|f| {
                    if let IdentOrLiteral::Ident(id) = &f.name.raw {
                        Some(id.clone())
                    } else {
                        None
                    }
                })
                .collect();
            output.extend(quote! { { #(#names),* } });
        }
    }
}

/// `@variant_pattern_only(ns::key) { ... }` - generate pattern binding only fields with attribute
fn handle_variant_pattern_only(
    iter: &mut std::iter::Peekable<proc_macro2::token_stream::IntoIter>,
    ctx: &EvalContext<'_>,
    output: &mut TokenStream,
) {
    let Some(proc_macro2::TokenTree::Group(query_group)) = iter.next() else {
        return;
    };

    let Some(query) = AttrQuery::parse(query_group.stream()) else {
        return;
    };

    let Some(variant) = ctx.current_variant() else {
        return;
    };

    use facet_macro_parse::{IdentOrLiteral, PVariantKind};

    match &variant.kind {
        PVariantKind::Unit => {
            // No pattern needed for unit variants
        }
        PVariantKind::Tuple { fields } => {
            let patterns: Vec<_> = fields
                .iter()
                .enumerate()
                .map(|(i, f)| {
                    if query.find_in(&f.attrs.facet).is_some() {
                        let name = quote::format_ident!("v{}", i);
                        quote! { #name }
                    } else {
                        quote! { _ }
                    }
                })
                .collect();
            output.extend(quote! { ( #(#patterns),* ) });
        }
        PVariantKind::Struct { fields } => {
            let bindings: Vec<_> = fields
                .iter()
                .filter_map(|f| {
                    if let IdentOrLiteral::Ident(id) = &f.name.raw {
                        if query.find_in(&f.attrs.facet).is_some() {
                            Some(quote! { #id })
                        } else {
                            Some(quote! { #id: _ })
                        }
                    } else {
                        None
                    }
                })
                .collect();
            output.extend(quote! { { #(#bindings),* } });
        }
    }
}

fn emit_field_name(ctx: &EvalContext<'_>, output: &mut TokenStream) {
    let Some((field, index)) = ctx.current_field() else {
        return;
    };

    use facet_macro_parse::IdentOrLiteral;

    match &field.name.raw {
        IdentOrLiteral::Ident(id) => {
            output.extend(quote! { #id });
        }
        IdentOrLiteral::Literal(_) => {
            // Tuple field - use generated name matching @variant_pattern (v0, v1, etc.)
            let name = quote::format_ident!("v{}", index);
            output.extend(quote! { #name });
        }
    }
}

fn emit_field_type(ctx: &EvalContext<'_>, output: &mut TokenStream) {
    if let Some((field, _)) = ctx.current_field() {
        let ty = &field.ty;
        output.extend(quote! { #ty });
    }
}

fn emit_field_expr(ctx: &EvalContext<'_>, output: &mut TokenStream) {
    // Same as field_name for now, but could be different for self.field access
    emit_field_name(ctx, output);
}

fn emit_attr_args(ctx: &EvalContext<'_>, output: &mut TokenStream) {
    if let Some(attr) = ctx.current_attr() {
        let args = &attr.args;
        output.extend(args.clone());
    }
}

fn emit_doc(ctx: &EvalContext<'_>, output: &mut TokenStream) {
    let attrs = ctx.current_attrs();
    let doc = attrs.doc.join(" ").trim().to_string();
    if !doc.is_empty() {
        output.extend(quote! { #doc });
    }
}

// ============================================================================
// DEFAULT-RELATED DIRECTIVES
// ============================================================================

/// `@field_default_expr` - emit the default expression for the current field
///
/// Checks for:
/// - `#[facet(default = literal)]` (builtin) → `literal` (direct value)
/// - `#[facet(default)]` (builtin, no value) → `::core::default::Default::default()`
/// - `#[facet(default::value = literal)]` → `literal.into()`
/// - `#[facet(default::func = "path")]` → `path()`
/// - No attribute → `::core::default::Default::default()`
fn emit_field_default_expr(ctx: &EvalContext<'_>, output: &mut TokenStream) {
    let Some((field, _)) = ctx.current_field() else {
        return;
    };

    // Check for builtin #[facet(default = ...)] attribute (ns is None, key is "default")
    if let Some(attr) = field
        .attrs
        .facet
        .iter()
        .find(|a| a.ns.is_none() && a.key == "default")
    {
        let args = &attr.args;
        if args.is_empty() {
            // #[facet(default)] without value - use Default::default()
            output.extend(quote! { ::core::default::Default::default() });
        } else {
            // #[facet(default = value)] - emit the value directly
            output.extend(quote! { #args });
        }
        return;
    }

    // No default attribute - use Default::default()
    output.extend(quote! { ::core::default::Default::default() });
}

/// `@variant_default_construction` - emit the construction for a default variant
///
/// - Unit variant → nothing
/// - Tuple variant → (Default::default(), ...)
/// - Struct variant → { field: Default::default(), ... }
fn emit_variant_default_construction(ctx: &EvalContext<'_>, output: &mut TokenStream) {
    use facet_macro_parse::{IdentOrLiteral, PVariantKind};

    let Some(variant) = ctx.current_variant() else {
        return;
    };

    match &variant.kind {
        PVariantKind::Unit => {
            // Nothing to emit
        }
        PVariantKind::Tuple { fields } => {
            let defaults: Vec<_> = fields.iter().map(field_default_tokens).collect();
            output.extend(quote! { ( #(#defaults),* ) });
        }
        PVariantKind::Struct { fields } => {
            let field_inits: Vec<_> = fields
                .iter()
                .filter_map(|f| {
                    if let IdentOrLiteral::Ident(name) = &f.name.raw {
                        let default_expr = field_default_tokens(f);
                        Some(quote! { #name: #default_expr })
                    } else {
                        None
                    }
                })
                .collect();
            output.extend(quote! { { #(#field_inits),* } });
        }
    }
}

/// Helper to generate the default expression tokens for a field
fn field_default_tokens(field: &facet_macro_parse::PStructField) -> TokenStream {
    // Check for builtin #[facet(default = ...)] attribute (ns is None, key is "default")
    if let Some(attr) = field
        .attrs
        .facet
        .iter()
        .find(|a| a.ns.is_none() && a.key == "default")
    {
        let args = &attr.args;
        if args.is_empty() {
            // #[facet(default)] without value - use Default::default()
            return quote! { ::core::default::Default::default() };
        } else {
            // #[facet(default = value)] - emit the value directly
            return quote! { #args };
        }
    }

    // No default attribute - use Default::default()
    quote! { ::core::default::Default::default() }
}

// ============================================================================
// LEGACY DIRECTIVES (for backwards compatibility with existing facet-error)
// ============================================================================

/// Legacy `@format_doc_comment` - emits doc comment as format string with args
fn emit_format_doc_comment(ctx: &EvalContext<'_>, output: &mut TokenStream) {
    use facet_macro_parse::PVariantKind;

    let Some(variant) = ctx.current_variant() else {
        return;
    };

    let doc = variant.attrs.doc.join(" ").trim().to_string();
    let format_str = if doc.is_empty() {
        variant.name.original.clone()
    } else {
        doc
    };

    // Check if format string uses positional args like {0}
    match &variant.kind {
        PVariantKind::Unit => {
            output.extend(quote! { #format_str });
        }
        PVariantKind::Tuple { fields } => {
            if format_str.contains("{0}") {
                // Use v0, v1, etc. to match legacy @variant_pattern
                let field_names: Vec<_> = (0..fields.len())
                    .map(|i| quote::format_ident!("v{}", i))
                    .collect();
                output.extend(quote! { #format_str, #(#field_names),* });
            } else {
                output.extend(quote! { #format_str });
            }
        }
        PVariantKind::Struct { fields: _ } => {
            // For struct variants, Rust will resolve field names in format string
            output.extend(quote! { #format_str });
        }
    }
}

// Grammar for parsing finalize sections
crate::unsynn! {
    /// Section marker like `@tokens`, `@plugins`
    struct FinalizeSectionMarker {
        _at: crate::At,
        name: Ident,
    }

    /// A braced section like `@tokens { ... }`
    struct FinalizeSection {
        marker: FinalizeSectionMarker,
        content: crate::BraceGroupContaining<TokenStream>,
    }
}

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

    #[test]
    fn test_to_snake_case() {
        assert_eq!(to_snake_case("Error"), "error");
        assert_eq!(to_snake_case("Display"), "display");
        assert_eq!(to_snake_case("PartialEq"), "partial_eq");
        assert_eq!(to_snake_case("FromStr"), "from_str");
    }

    #[test]
    fn test_extract_derive_plugins() {
        let input = quote! {
            #[derive(Facet, Debug)]
            #[facet(derive(Error))]
            #[repr(u8)]
            pub enum MyError {
                Disconnect(u32),
            }
        };

        let mut iter = input.to_token_iter();
        let parsed = iter.parse::<crate::Enum>().expect("Failed to parse enum");

        let plugins = extract_derive_plugins(&parsed.attributes);
        assert_eq!(plugins.len(), 1);
        assert!(matches!(&plugins[0], PluginRef::Simple(name) if name == "Error"));
    }

    #[test]
    fn test_extract_multiple_plugins() {
        let input = quote! {
            #[facet(derive(Error, Display))]
            pub enum MyError {
                Unknown,
            }
        };

        let mut iter = input.to_token_iter();
        let parsed = iter.parse::<crate::Enum>().expect("Failed to parse enum");

        let plugins = extract_derive_plugins(&parsed.attributes);
        assert_eq!(plugins.len(), 2);
        assert!(matches!(&plugins[0], PluginRef::Simple(name) if name == "Error"));
        assert!(matches!(&plugins[1], PluginRef::Simple(name) if name == "Display"));
    }

    #[test]
    fn test_extract_path_plugins() {
        let input = quote! {
            #[facet(derive(Error, facet_default::Default))]
            pub enum MyError {
                Unknown,
            }
        };

        let mut iter = input.to_token_iter();
        let parsed = iter.parse::<crate::Enum>().expect("Failed to parse enum");

        let plugins = extract_derive_plugins(&parsed.attributes);
        assert_eq!(plugins.len(), 2);
        assert!(matches!(&plugins[0], PluginRef::Simple(name) if name == "Error"));
        assert!(
            matches!(&plugins[1], PluginRef::Path { crate_name, plugin_name } if crate_name == "facet_default" && plugin_name == "Default")
        );
    }

    #[test]
    fn test_plugin_ref_crate_path() {
        let simple = PluginRef::Simple("Error".to_string());
        assert_eq!(simple.crate_path().to_string(), ":: facet_error");

        let path = PluginRef::Path {
            crate_name: "facet_default".to_string(),
            plugin_name: "Default".to_string(),
        };
        assert_eq!(path.crate_path().to_string(), ":: facet_default");
    }

    /// Test for issue #1679: derive(Default) combined with other attributes on the same line
    #[test]
    fn test_extract_derive_plugins_combined_attrs() {
        // This is the failing case from the issue: derive(Default) combined with rename_all
        let input = quote! {
            #[derive(Debug, Facet)]
            #[facet(rename_all = "kebab-case", derive(Default))]
            struct PreCommitConfig {
                generate_readmes: bool,
            }
        };

        let mut iter = input.to_token_iter();
        let parsed = iter
            .parse::<crate::Struct>()
            .expect("Failed to parse struct");

        let plugins = extract_derive_plugins(&parsed.attributes);
        assert_eq!(
            plugins.len(),
            1,
            "should extract derive(Default) even when combined with other attrs"
        );
        assert!(matches!(&plugins[0], PluginRef::Simple(name) if name == "Default"));
    }

    /// Test for issue #1679: strip_derive_attrs should strip only derive part, keeping other attrs
    #[test]
    fn test_strip_derive_attrs_combined() {
        // Input with derive(Default) combined with rename_all
        let input = quote! {
            #[derive(Debug, Facet)]
            #[facet(rename_all = "kebab-case", derive(Default))]
            struct PreCommitConfig {
                generate_readmes: bool,
            }
        };

        let stripped = strip_derive_attrs(input);
        let stripped_str = stripped.to_string();

        // Should keep #[derive(Debug, Facet)]
        assert!(
            stripped_str.contains("derive"),
            "should keep #[derive(Debug, Facet)]"
        );

        // Should keep rename_all in the facet attribute
        assert!(
            stripped_str.contains("rename_all"),
            "should keep rename_all attribute"
        );

        // Should NOT contain derive(Default) in facet attribute
        // The original has facet(rename_all = "kebab-case", derive(Default))
        // After stripping, it should have facet(rename_all = "kebab-case")
        assert!(
            !stripped_str.contains("facet (rename_all = \"kebab-case\" , derive (Default))"),
            "should strip derive(Default) from combined attribute"
        );
    }

    /// Test strip_derive_attrs with only derive in facet attribute
    #[test]
    fn test_strip_derive_attrs_only_derive() {
        let input = quote! {
            #[facet(derive(Default))]
            struct Foo {}
        };

        let stripped = strip_derive_attrs(input);
        let stripped_str = stripped.to_string();

        // The entire facet attribute should be stripped (or result in empty facet())
        // Since the facet attribute only contains derive(Default)
        assert!(
            !stripped_str.contains("derive (Default)"),
            "derive(Default) should be stripped"
        );
    }
}