nextjson-derive 0.1.3

Dependency-free derive macros for NextJson serialization, decoding, and schemas.
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
//! Zero-dependency derive macros for `nextjson`.
//!
//! Implemented entirely with the standard `proc_macro` API: no `syn`, no
//! `quote`, no `proc-macro2`. The input `TokenStream` is parsed by a
//! hand-written recursive-descent parser into a small AST, and the output is
//! generated as text and re-parsed.
//!
//! ## Forward compatibility contract
//!
//! A derive macro only ever receives a single item's tokens, and this parser
//! interprets a deliberately **stable grammar subset**: the item header
//! (`struct` / `enum` + name), the generic parameter list, the `where`
//! clause, and the field / variant structure, plus `#[njson]` /
//! `#[nextjson]` / `#[serde]` attributes. Everything inside a field or
//! generic *type position* is carried through verbatim as an opaque token
//! sequence, so new Rust syntax that appears in type positions (new
//! literals, `impl Trait` forms, associated-type paths, ...) needs no parser
//! change — it is round-tripped unchanged.
//!
//! The risk of future item-level grammar changes is handled defensively:
//! `parse_input` requires that **every** input token be consumed. If a future
//! Rust release extends item syntax in a way this parser does not understand,
//! the macro fails with a loud `compile_error!` naming the leftover tokens
//! instead of silently generating impls from a mis-parsed subset.

#![deny(unsafe_code)]
#![deny(missing_docs)]
#![doc(html_root_url = "https://docs.rs/nextjson-derive")]

extern crate proc_macro;

use proc_macro::{Delimiter, Spacing, TokenStream, TokenTree};
use std::str::FromStr;

mod attr;
mod case;
mod de;
mod schema;
mod ser;

pub(crate) use attr::{ContainerAttrs, FieldAttrs, Meta, VariantAttrs};

/// Parse a string into a TokenStream.
pub(crate) fn ts(s: &str) -> TokenStream {
    TokenStream::from_str(s)
        .unwrap_or_else(|e| panic!("nextjson-derive: invalid generated tokens: {e:?}"))
}

/// Build a `compile_error!` expansion from a message.
pub(crate) fn err(msg: &str) -> TokenStream {
    ts(&format!("::core::compile_error!({:?})", msg))
}

/// Error string returned by codegen helpers.
pub(crate) fn err_str(msg: &str) -> String {
    msg.to_string()
}

/// Token cursor over a slice of TokenTrees.
pub(crate) struct P<'a> {
    pub toks: &'a [TokenTree],
    pub i: usize,
}

impl<'a> P<'a> {
    pub fn peek(&self) -> Option<&TokenTree> {
        self.toks.get(self.i)
    }
    pub fn next(&mut self) -> Option<TokenTree> {
        let t = self.toks.get(self.i).cloned();
        if t.is_some() {
            self.i += 1;
        }
        t
    }
    pub fn is_ident(&self, s: &str) -> bool {
        matches!(self.peek(), Some(TokenTree::Ident(id)) if id.to_string() == s)
    }
    pub fn is_punct(&self, ch: char) -> bool {
        matches!(self.peek(), Some(TokenTree::Punct(p)) if p.as_char() == ch)
    }
    pub fn eat_ident(&mut self, s: &str) -> bool {
        if self.is_ident(s) {
            self.i += 1;
            true
        } else {
            false
        }
    }
    pub fn eat_punct(&mut self, ch: char) -> bool {
        if self.is_punct(ch) {
            self.i += 1;
            true
        } else {
            false
        }
    }
    pub fn expect_ident(&mut self) -> Option<String> {
        match self.next() {
            Some(TokenTree::Ident(id)) => Some(id.to_string()),
            _ => None,
        }
    }
}

/// Join tokens into a re-parseable string, preserving `Joint` spacing so
/// that punctuation sequences (`::`, `'a`, `->`, `>>`) stay adjacent.
pub(crate) fn join(toks: &[TokenTree]) -> String {
    let mut s = String::new();
    let mut no_space = false;
    for t in toks {
        if !s.is_empty() && !no_space {
            s.push(' ');
        }
        no_space = false;
        match t {
            TokenTree::Punct(p) => {
                s.push_str(&p.to_string());
                no_space = p.spacing() == Spacing::Joint;
            }
            _ => s.push_str(&t.to_string()),
        }
    }
    s
}

/// Split tokens at a top-level separator.
///
/// Angle brackets are tracked so that generic types such as
/// `BTreeMap<String, i32>` stay on a single side of the split.
pub(crate) fn split_top(toks: &[TokenTree], sep: char) -> Vec<Vec<TokenTree>> {
    let mut out: Vec<Vec<TokenTree>> = Vec::new();
    let mut cur: Vec<TokenTree> = Vec::new();
    let mut angle: usize = 0;
    for tt in toks {
        match tt {
            TokenTree::Group(_) => cur.push(tt.clone()),
            TokenTree::Punct(p) if p.as_char() == '<' => {
                angle += 1;
                cur.push(tt.clone());
            }
            TokenTree::Punct(p) if p.as_char() == '>' => {
                angle = angle.saturating_sub(1);
                cur.push(tt.clone());
            }
            TokenTree::Punct(p) if angle == 0 && p.as_char() == sep => {
                out.push(std::mem::take(&mut cur));
            }
            _ => cur.push(tt.clone()),
        }
    }
    if !cur.is_empty() {
        out.push(cur);
    }
    if out.is_empty() {
        out.push(Vec::new());
    }
    out
}

/// Read a `<...>` group. proc_macro does not group angle brackets, so this
/// scans for the matching `>` while ignoring `->` arrow tokens.
pub(crate) fn read_angle(p: &mut P) -> Option<Vec<TokenTree>> {
    if !p.eat_punct('<') {
        return None;
    }
    let mut depth = 1usize;
    let mut out = Vec::new();
    while let Some(tt) = p.next() {
        match &tt {
            TokenTree::Punct(c) if c.as_char() == '<' => {
                depth += 1;
                out.push(tt);
            }
            TokenTree::Punct(c)
                if c.as_char() == '-'
                    && matches!(p.peek(), Some(TokenTree::Punct(n)) if n.as_char() == '>') =>
            {
                out.push(tt);
                out.push(p.next().unwrap());
            }
            TokenTree::Punct(c) if c.as_char() == '>' => {
                if depth == 1 {
                    return Some(out);
                }
                depth -= 1;
                out.push(tt);
            }
            _ => out.push(tt),
        }
    }
    None
}

// ---------------------------------------------------------------------------
// AST
// ---------------------------------------------------------------------------

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum ParamKind {
    Lifetime,
    Type,
    Const,
}

#[derive(Clone)]
pub(crate) struct GenericParam {
    pub kind: ParamKind,
    pub full: String,
    pub name: String,
}

#[derive(Clone, Default)]
pub(crate) struct Generics {
    pub params: Vec<GenericParam>,
    pub where_preds: Vec<String>,
}

#[derive(Clone)]
pub(crate) struct Field {
    pub ident: Option<String>,
    pub ty: String,
    pub attrs: Vec<attr::Meta>,
}

#[derive(Clone)]
pub(crate) enum Fields {
    Unit,
    Named(Vec<Field>),
    Unnamed(Vec<Field>),
}

impl Fields {
    pub fn iter(&self) -> core::slice::Iter<'_, Field> {
        match self {
            Fields::Unit => [].iter(),
            Fields::Named(fields) | Fields::Unnamed(fields) => fields.iter(),
        }
    }
}

#[derive(Clone)]
pub(crate) struct Variant {
    pub ident: String,
    pub fields: Fields,
    pub attrs: Vec<attr::Meta>,
}

#[derive(Clone)]
pub(crate) enum Data {
    Struct(Fields),
    Enum(Vec<Variant>),
}

#[derive(Clone)]
pub(crate) struct Input {
    pub ident: String,
    pub generics: Generics,
    pub data: Data,
    pub cattr: ContainerAttrs,
}

// ---------------------------------------------------------------------------
// Attribute collection
// ---------------------------------------------------------------------------

/// Collect leading `#[...]` attribute groups.
fn parse_attrs(p: &mut P) -> Vec<Vec<TokenTree>> {
    let mut out = Vec::new();
    while p.is_punct('#') {
        p.next();
        if let Some(TokenTree::Group(g)) = p.next() {
            if g.delimiter() == Delimiter::Bracket {
                out.push(g.stream().into_iter().collect());
            }
        }
    }
    out
}

/// Extract `njson` / `nextjson` metas from a set of attribute groups.
fn collect_metas(groups: &[Vec<TokenTree>]) -> Vec<attr::Meta> {
    let mut out = Vec::new();
    for g in groups {
        out.extend(attr::metas_from_attr(g));
    }
    out
}

// ---------------------------------------------------------------------------
// Top-level parse
// ---------------------------------------------------------------------------

pub(crate) fn parse_input(input: TokenStream) -> Result<Input, String> {
    let toks: Vec<TokenTree> = input.into_iter().collect();
    let mut p = P { toks: &toks, i: 0 };

    let attrs = parse_attrs(&mut p);
    let cattr = ContainerAttrs::from_metas(&collect_metas(&attrs));
    eat_visibility(&mut p);

    let is_enum = if p.eat_ident("struct") {
        false
    } else if p.eat_ident("enum") {
        true
    } else {
        return Err("nextjson: expected `struct` or `enum`".into());
    };

    let ident = p
        .expect_ident()
        .ok_or_else(|| "nextjson: expected type name".to_string())?;

    let mut generics = Generics::default();
    if let Some(inner) = read_angle(&mut p) {
        generics = parse_generics(&inner);
    }

    let data = if !is_enum
        && matches!(p.peek(), Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Parenthesis)
    {
        let Some(TokenTree::Group(body)) = p.next() else {
            return Err("nextjson: expected a tuple struct body".into());
        };
        if p.eat_ident("where") {
            parse_where_clause(&mut p, &mut generics, false);
        }
        // Tuple structs terminate with `;`; consume it so the trailing-input
        // check below sees a fully parsed item.
        p.eat_punct(';');
        let inner: Vec<TokenTree> = body.stream().into_iter().collect();
        Data::Struct(Fields::Unnamed(parse_unnamed_fields(&inner)))
    } else {
        if p.eat_ident("where") {
            parse_where_clause(&mut p, &mut generics, true);
        }
        if !is_enum {
            match p.next() {
                Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => {
                    let inner: Vec<TokenTree> = g.stream().into_iter().collect();
                    Data::Struct(Fields::Named(parse_named_fields(&inner)))
                }
                Some(TokenTree::Punct(pc)) if pc.as_char() == ';' => Data::Struct(Fields::Unit),
                _ => return Err("nextjson: expected a struct body".into()),
            }
        } else {
            match p.next() {
                Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => {
                    let inner: Vec<TokenTree> = g.stream().into_iter().collect();
                    Data::Enum(parse_variants(&inner))
                }
                _ => return Err("nextjson: expected an enum body".into()),
            }
        }
    };

    // The derive input must be a single item. Any tokens left over mean the
    // hand-written parser did not understand part of the declaration; refuse
    // to generate code from a silently mis-parsed subset (this is the
    // forward-compatibility guard: if a future Rust release extends item
    // syntax, the macro fails loudly instead of emitting wrong impls).
    if p.i != toks.len() {
        return Err(format!(
            "nextjson: cannot parse trailing tokens: {}",
            join(&toks[p.i..])
        ));
    }

    Ok(Input {
        ident,
        generics,
        data,
        cattr,
    })
}

fn parse_where_clause(p: &mut P<'_>, generics: &mut Generics, has_braced_body: bool) {
    let mut tokens = Vec::new();
    while let Some(token) = p.peek() {
        let is_body = has_braced_body
            && p.i + 1 == p.toks.len()
            && matches!(token, TokenTree::Group(g) if g.delimiter() == Delimiter::Brace);
        if is_body || matches!(token, TokenTree::Punct(punct) if punct.as_char() == ';') {
            break;
        }
        if let Some(token) = p.next() {
            tokens.push(token);
        }
    }
    for piece in split_top(&tokens, ',') {
        let predicate = join(&piece).trim().to_string();
        if !predicate.is_empty() {
            generics.where_preds.push(predicate);
        }
    }
}

fn parse_generics(inner: &[TokenTree]) -> Generics {
    let mut g = Generics::default();
    for item in split_top(inner, ',') {
        if item.is_empty() {
            continue;
        }
        let declaration = strip_generic_default(&item);
        let mut p = P {
            toks: &declaration,
            i: 0,
        };
        if p.is_punct('\'') {
            p.next();
            let name = p.expect_ident().unwrap_or_default();
            g.params.push(GenericParam {
                kind: ParamKind::Lifetime,
                full: join(&declaration),
                name: format!("'{name}"),
            });
        } else if p.eat_ident("const") {
            let name = p.expect_ident().unwrap_or_default();
            g.params.push(GenericParam {
                kind: ParamKind::Const,
                full: join(&declaration),
                name,
            });
        } else {
            let name = p.expect_ident().unwrap_or_default();
            g.params.push(GenericParam {
                kind: ParamKind::Type,
                full: join(&declaration),
                name,
            });
        }
    }
    g
}

fn strip_generic_default(tokens: &[TokenTree]) -> Vec<TokenTree> {
    let mut angle_depth = 0usize;
    for (index, token) in tokens.iter().enumerate() {
        match token {
            TokenTree::Punct(punct) if punct.as_char() == '<' => angle_depth += 1,
            TokenTree::Punct(punct) if punct.as_char() == '>' => {
                angle_depth = angle_depth.saturating_sub(1);
            }
            TokenTree::Punct(punct) if punct.as_char() == '=' && angle_depth == 0 => {
                return tokens[..index].to_vec();
            }
            _ => {}
        }
    }
    tokens.to_vec()
}

fn parse_named_fields(inner: &[TokenTree]) -> Vec<Field> {
    split_top(inner, ',')
        .iter()
        .filter(|s| !s.is_empty())
        .map(|piece| parse_named_field(piece))
        .collect()
}

/// Consume an optional `pub` visibility specifier (`pub`, `pub(crate)`,
/// `pub(super)`, `pub(in path)`). In the proc-macro token stream the
/// parenthesized part arrives as a `Group` with `Parenthesis` delimiter, not
/// as a `Punct('(')`, so it must be matched as a group.
pub(crate) fn eat_visibility(p: &mut P<'_>) {
    if !p.eat_ident("pub") {
        return;
    }
    if matches!(p.peek(), Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Parenthesis) {
        p.next();
    }
}

fn parse_named_field(piece: &[TokenTree]) -> Field {
    let mut p = P { toks: piece, i: 0 };
    let attrs = parse_attrs(&mut p);
    eat_visibility(&mut p);
    // Find the field separator ':' at top level, excluding '::'.
    let mut colon = None;
    let mut j = p.i;
    while j < piece.len() {
        match &piece[j] {
            TokenTree::Punct(c) if c.as_char() == ':' => {
                if matches!(piece.get(j + 1), Some(TokenTree::Punct(n)) if n.as_char() == ':') {
                    j += 2;
                    continue;
                }
                colon = Some(j);
                break;
            }
            _ => j += 1,
        }
    }
    let (ident, ty) = match colon {
        Some(c) => (
            Some(join(&piece[p.i..c]).trim().to_string()),
            join(&piece[c + 1..]).trim().to_string(),
        ),
        None => (None, join(&piece[p.i..]).trim().to_string()),
    };
    let mut field_metas = collect_metas(&attrs);
    // `PhantomData` fields are not part of the data model (serde semantics):
    // skip them on serialize and default them on deserialize. Normalizing at
    // parse time keeps every codegen path (ser / de / schema) consistent.
    if crate::schema::is_phantom_data(&ty) {
        field_metas.push(Meta::Flag("skip".to_string()));
    }
    Field {
        ident,
        ty,
        attrs: field_metas,
    }
}

fn parse_unnamed_fields(inner: &[TokenTree]) -> Vec<Field> {
    split_top(inner, ',')
        .iter()
        .filter(|s| !s.is_empty())
        .map(|piece| {
            let mut p = P { toks: piece, i: 0 };
            let attrs = parse_attrs(&mut p);
            eat_visibility(&mut p);
            Field {
                ident: None,
                ty: join(&piece[p.i..]).trim().to_string(),
                attrs: collect_metas(&attrs),
            }
        })
        .collect()
}

fn parse_variants(inner: &[TokenTree]) -> Vec<Variant> {
    split_top(inner, ',')
        .iter()
        .filter(|s| !s.is_empty())
        .map(|piece| {
            let mut p = P { toks: piece, i: 0 };
            let attrs = parse_attrs(&mut p);
            let ident = p.expect_ident().unwrap_or_default();
            let fields = match p.next() {
                Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => {
                    let inner2: Vec<TokenTree> = g.stream().into_iter().collect();
                    Fields::Named(parse_named_fields(&inner2))
                }
                Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Parenthesis => {
                    let inner2: Vec<TokenTree> = g.stream().into_iter().collect();
                    Fields::Unnamed(parse_unnamed_fields(&inner2))
                }
                _ => Fields::Unit,
            };
            Variant {
                ident,
                fields,
                attrs: collect_metas(&attrs),
            }
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Generic helpers for code generation
// ---------------------------------------------------------------------------

/// Build `(impl_generics, ty_generics, where_clause)` for the impl header.
pub(crate) fn build_generics(
    input: &Input,
    cp: &str,
    de: bool,
    has_flatten: bool,
    has_borrow: bool,
) -> (String, String, String) {
    let g = &input.generics;
    let c = &input.cattr;
    let name = input.ident.clone();
    // `remote` implements the traits for an external type; conversion bounds
    // that mention `Self` must refer to that type instead of the mirror. The
    // remote path already carries its generic arguments, while a local type
    // must be written with the mirror's type parameters applied.
    let (self_ty, remote_typed) = match &c.remote {
        Some(r) => (r.clone(), true),
        None => (name.clone(), false),
    };

    let mut impl_params: Vec<String> = g.params.iter().map(|p| p.full.clone()).collect();
    if de {
        impl_params.insert(0, "'de".to_string());
    }
    let impl_generics = if impl_params.is_empty() {
        String::new()
    } else {
        format!("<{}>", impl_params.join(", "))
    };

    let names: Vec<String> = g.params.iter().map(|p| p.name.clone()).collect();
    let ty_generics = if names.is_empty() {
        String::new()
    } else {
        format!("<{}>", names.join(", "))
    };
    // The fully-instantiated `Self` for conversion bounds: for local types the
    // type parameters must be applied (`Dst<T>`), for remote types the path
    // already names them (`external::Foreign<T>`).
    let self_ty_inst = if remote_typed {
        self_ty.clone()
    } else {
        format!("{self_ty}{ty_generics}")
    };

    // The type's own where-clause predicates are ALWAYS required to name the
    // type, so they are kept unconditionally. The `bound` attribute only
    // replaces the *auto-generated per-type-parameter* bounds; serde behaves
    // the same way.
    let mut preds: Vec<String> = g.where_preds.clone();

    let directional = if de {
        c.bound_de.as_ref()
    } else {
        c.bound_ser.as_ref()
    };
    let bound = directional.or(c.bound.as_ref());
    let auto_bound = |p: &GenericParam| -> Option<String> {
        if p.kind != ParamKind::Type {
            return None;
        }
        if de && has_flatten {
            Some(format!(
                "{0}: for<'__n> {1}::NsonDeserialize<'__n>",
                p.name, cp
            ))
        } else if de {
            Some(format!("{}: {}::NsonDeserialize<'de>", p.name, cp))
        } else {
            Some(format!("{}: {}::NsonSerialize", p.name, cp))
        }
    };
    if let Some(bound) = bound {
        let cleaned = bound.trim().trim_matches('"');
        if !cleaned.is_empty() {
            for s in cleaned.split(',') {
                let s = s.trim();
                if !s.is_empty() {
                    preds.push(s.to_string());
                }
            }
        }
    } else {
        for p in g.params.iter() {
            if let Some(b) = auto_bound(p) {
                preds.push(b);
            }
        }
    }

    // Missing-field fallbacks that call `Default::default()` must be able to
    // name the type parameters they fall back on, so every type parameter
    // receives a `Default` bound when any fallback can fire for a generic
    // field. This mirrors serde, which adds `T: Default` for exactly the same
    // attribute combinations.
    if de && de_uses_type_param_default(input) {
        for p in g.params.iter() {
            if p.kind == ParamKind::Type {
                preds.push(format!("{}: ::core::default::Default", p.name));
            }
        }
    }

    // Conversion attributes add their own bounds.
    if de {
        if let Some(from) = &c.from {
            preds.push(format!(
                "{from}: {cp}::NsonDeserialize<'de> + ::core::convert::Into<{self_ty_inst}>"
            ));
        }
        if let Some(from) = &c.try_from {
            preds.push(format!(
                "{from}: {cp}::NsonDeserialize<'de> + ::core::convert::TryInto<{self_ty_inst}>"
            ));
            preds.push(format!(
                "<{from} as ::core::convert::TryInto<{self_ty_inst}>>::Error: ::core::fmt::Display"
            ));
        }
    } else {
        if let Some(into) = &c.into {
            preds.push(format!("{self_ty_inst}: ::core::clone::Clone"));
            preds.push(format!("{self_ty_inst}: ::core::convert::Into<{into}>"));
            preds.push(format!("{into}: {cp}::NsonSerialize"));
            preds.push(format!("{into}: {cp}::NsonSchema"));
        }
    }

    if de && has_borrow {
        for p in g.params.iter() {
            if p.kind == ParamKind::Lifetime {
                preds.push(format!("'de: {}", p.name));
            }
        }
    }

    let where_clause = if preds.is_empty() {
        String::new()
    } else {
        format!(" where {}", preds.join(", "))
    };

    (impl_generics, ty_generics, where_clause)
}

/// Validate attribute combinations that serde rejects at compile time.
///
/// Returns an error message when the combination is invalid. Called by both
/// derive entry points so the rejection is identical regardless of which
/// macro is expanded first.
fn validate_input(input: &Input) -> Option<String> {
    // `transparent` is only meaningful on single-field structs.
    if input.cattr.transparent {
        match &input.data {
            Data::Enum(_) => {
                return Some("nextjson: `transparent` is not supported on enums".to_string());
            }
            Data::Struct(Fields::Named(f)) if f.len() != 1 => {
                return Some("nextjson: `transparent` requires exactly one field".to_string());
            }
            Data::Struct(Fields::Unnamed(f)) if f.len() != 1 => {
                return Some("nextjson: `transparent` requires exactly one field".to_string());
            }
            _ => {}
        }
    }
    // `flatten` splices a nested map into the parent object, which is
    // impossible for positional (unnamed) shapes.
    if type_has_flag_on(input, |f, fa| fa.flatten && f.ident.is_none()) {
        return Some(
            "nextjson: `flatten` is not allowed on tuple structs or tuple variants".to_string(),
        );
    }
    // `flatten` + `skip_serializing_if` conflicts: the decision to skip would
    // depend on the flattened value, which serde rejects up front.
    if type_has_flag_on(input, |_f, fa| {
        fa.flatten && fa.skip_serializing_if.is_some()
    }) {
        return Some(
            "nextjson: `flatten` cannot be combined with `skip_serializing_if`".to_string(),
        );
    }
    None
}

/// Like [`type_has_flag`] but the predicate also sees the field (needed to
/// tell named from unnamed fields).
fn type_has_flag_on<F: Fn(&Field, &FieldAttrs) -> bool>(input: &Input, f: F) -> bool {
    let check = |fld: &Field| f(fld, &attr::field_attrs(&fld.attrs));
    match &input.data {
        Data::Struct(fields) => fields.iter().any(check),
        Data::Enum(variants) => variants.iter().any(|v| v.fields.iter().any(check)),
    }
}

/// Emit the `NsonSchema` + `NsonSerialize` impls.
pub(crate) fn generate_impls(input: &Input) -> TokenStream {
    if let Some(msg) = validate_input(input) {
        return err(&msg);
    }
    let cp = input.cattr.crate_path.clone();
    let name = input.ident.clone();
    // `remote` implements the traits for the external type itself. The remote
    // path already names its generic arguments, so the mirror's type-generics
    // are not appended a second time (`Foreign<T><T>` would not parse).
    let (target, use_tg) = match &input.cattr.remote {
        Some(r) => (r.clone(), false),
        None => (name.clone(), true),
    };
    let (ig, tg, wc) = build_generics(input, &cp, false, false, false);
    let tg_part = if use_tg { tg.as_str() } else { "" };
    let body = if let Some(into) = &input.cattr.into {
        // `into = "T"`: serialize by converting `self` to `T` first.
        format!(
            "let __v: {into} = ::core::convert::Into::into(self.clone());\n\
             <{into} as {cp}::NsonSerialize>::nextencode(&__v, __e)"
        )
    } else {
        match &input.data {
            Data::Struct(f) => ser::serialize_struct(&name, f, input, &cp),
            Data::Enum(v) => ser::serialize_enum(&name, v, input, &cp),
        }
    };
    let out = format!(
        "#[automatically_derived]\n\
         impl {ig} {cp}::NsonSchema for {target}{tg_part}{wc} {{\n\
         \x20   const SCHEMA: {cp}::TypeSchema = {schema_expr};\n\
         }}\n\
         #[automatically_derived]\n\
         impl {ig} {cp}::NsonSerialize for {target}{tg_part}{wc} {{\n\
         \x20   fn nextencode<__E: {cp}::FormatEncoder>(&self, __e: &mut __E) -> ::core::result::Result<(), __E::Error> {{\n\
         {body}\n\
         \x20   }}\n\
         }}",
        schema_expr = schema::schema_expr(input, &cp)
    );
    ts(&out)
}

/// Emit the `NsonDeserialize` impl.
pub(crate) fn generate_de_impl(input: &Input) -> TokenStream {
    if let Some(msg) = validate_input(input) {
        return err(&msg);
    }
    let cp = input.cattr.crate_path.clone();
    let name = input.ident.clone();
    // Container-level `default` supplies missing-field values from a `Self`
    // instance, which only makes sense for structs (serde rejects it on
    // enums as well).
    if matches!(&input.data, Data::Enum(_)) && input.cattr.has_default() {
        return err("nextjson: `default` is not supported on enums");
    }
    let has_flatten = type_has_flag(input, |fa| fa.flatten);
    let has_borrow = type_has_flag(input, |fa| fa.borrow);
    if has_flatten && type_has_with(input) {
        return err("nextjson: `flatten` cannot be combined with `with` / `deserialize_with`");
    }
    if has_flatten && input.cattr.deny_unknown_fields {
        // flatten consumes every remaining key, so unknown-field rejection is
        // silently impossible; serde rejects this combination at compile time.
        return err("nextjson: `deny_unknown_fields` cannot be combined with `flatten`");
    }
    let (target, use_tg) = match &input.cattr.remote {
        Some(r) => (r.clone(), false),
        None => (name.clone(), true),
    };
    let (ig, tg, wc) = build_generics(input, &cp, true, has_flatten, has_borrow);
    let tg_part = if use_tg { tg.as_str() } else { "" };
    // `expecting = "..."` overrides the default `type_name`-based description
    // used in type-mismatch and length-mismatch error messages.
    let expecting = match &input.cattr.expecting {
        Some(e) => format!("\n     fn expecting() -> &'static str {{ {:?} }}\n", e),
        None => String::new(),
    };
    let body = if let Some(from) = &input.cattr.from {
        // `from = "T"`: deserialize a `T` then convert into `Self`.
        format!(
            "let __v: {from} = <{from} as {cp}::NsonDeserialize<'de>>::nextdecode(__d)?;\n\
             __out.write(::core::convert::Into::into(__v));\n\
             ::core::result::Result::Ok(())"
        )
    } else if let Some(from) = &input.cattr.try_from {
        // `try_from = "T"`: deserialize a `T` then fallibly convert.
        format!(
            "let __v: {from} = <{from} as {cp}::NsonDeserialize<'de>>::nextdecode(__d)?;\n\
             let __c: Self = ::core::convert::TryInto::try_into(__v).map_err(|__e| {{\n\
             \x20   {cp}::FormatError::custom({cp}::__private::ToString::to_string(&__e))\n\
             }})?;\n\
             __out.write(__c);\n\
             ::core::result::Result::Ok(())"
        )
    } else {
        match &input.data {
            Data::Struct(f) => de::deserialize_struct(&name, f, input, &cp, has_flatten),
            Data::Enum(v) => de::deserialize_enum(&name, v, input, &cp, has_flatten),
        }
    };
    let out = format!(
        "#[automatically_derived]\n\
         impl {ig} {cp}::NsonDeserialize<'de> for {target}{tg_part}{wc} {{\n\
         {expecting}\
         \x20   fn nextdecode_into<__D: {cp}::FormatDecoder<'de>>(\n\
         \x20       __d: &mut __D,\n\
         \x20       __out: &mut {cp}::DecodeSlot<Self>,\n\
         \x20   ) -> ::core::result::Result<(), __D::Error> {{\n\
         \x20       __d.set_expecting(Self::expecting());\n\
         {body}\n\
         \x20   }}\n\
         }}"
    );
    ts(&out)
}

fn type_has_flag<F: Fn(&FieldAttrs) -> bool>(input: &Input, f: F) -> bool {
    match &input.data {
        Data::Struct(fields) => fields.iter().any(|fld| f(&attr::field_attrs(&fld.attrs))),
        Data::Enum(variants) => variants
            .iter()
            .any(|v| v.fields.iter().any(|fld| f(&attr::field_attrs(&fld.attrs)))),
    }
}

/// Whether the generated deserializer can fall back to `Default::default()`
/// for a field whose type is a generic parameter.
///
/// True when the container has a default, when any field has a bare
/// `default` attribute, or when any field is `skip_deserializing` without an
/// explicit `default = "path"` (which would supply its own value). Field-level
/// `default = "path"` does not need the bound. `PhantomData` fields are
/// excluded: `PhantomData<T>: Default` holds for every `T` with no bound.
fn de_uses_type_param_default(input: &Input) -> bool {
    if input.cattr.has_default() {
        return true;
    }
    let mut found = false;
    let mut scan = |f: &Field| {
        if crate::schema::is_phantom_data(&f.ty) {
            return;
        }
        let fa = attr::field_attrs(&f.attrs);
        let bare_default = fa.default == Some(String::new());
        let skip_without_path =
            fa.skip_deserializing && !matches!(&fa.default, Some(d) if !d.is_empty());
        if bare_default || skip_without_path {
            found = true;
        }
    };
    match &input.data {
        Data::Struct(fields) => {
            for f in fields.iter() {
                scan(f);
            }
        }
        Data::Enum(variants) => {
            for v in variants {
                for f in v.fields.iter() {
                    scan(f);
                }
            }
        }
    }
    found
}

fn type_has_with(input: &Input) -> bool {
    type_has_flag(input, |fa| {
        fa.with.is_some() || fa.deserialize_with.is_some()
    })
}

// ---------------------------------------------------------------------------
// Entry points
// ---------------------------------------------------------------------------

#[proc_macro_derive(NsonSerialize, attributes(njson, nextjson, serde))]
/// Derive NextJson's native serialization contract and compile-time schema.
///
/// Configuration is accepted through `#[njson(...)]` (and, for migration
/// convenience, `#[serde(...)]`). The generated implementation writes
/// directly through `NsonSerialize::nextencode` and exposes
/// `NsonSchema::SCHEMA` without depending on another macro framework.
pub fn derive_serialize(input: TokenStream) -> TokenStream {
    match parse_input(input) {
        Ok(ast) => generate_impls(&ast),
        Err(e) => err(&e),
    }
}

#[proc_macro_derive(NsonDeserialize, attributes(njson, nextjson, serde))]
/// Derive NextJson's native decoding contract.
///
/// Configuration is accepted through `#[njson(...)]` (and, for migration
/// convenience, `#[serde(...)]`). The generated implementation decodes
/// through checked `DecodeSlot` state and uses normal Rust drop semantics for
/// partially initialized fields.
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
    match parse_input(input) {
        Ok(ast) => generate_de_impl(&ast),
        Err(e) => err(&e),
    }
}

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

    // The `proc_macro` API cannot be used in unit tests (it panics outside a
    // macro expansion), so these tests build the small AST by hand and cover
    // the pure logic: attribute parsing, validation, and impl-generics
    // construction. Parser token-level behavior is exercised by the workspace
    // integration tests, which compile real derives.

    fn meta_flag(name: &str) -> Meta {
        Meta::Flag(name.to_string())
    }
    fn meta_named(name: &str, value: &str) -> Meta {
        Meta::Named(name.to_string(), value.to_string())
    }
    fn named_field(ident: &str, ty: &str, metas: Vec<Meta>) -> Field {
        Field {
            ident: Some(ident.to_string()),
            ty: ty.to_string(),
            attrs: metas,
        }
    }
    fn unnamed_field(ty: &str, metas: Vec<Meta>) -> Field {
        Field {
            ident: None,
            ty: ty.to_string(),
            attrs: metas,
        }
    }
    fn cattr(metas: &[Meta]) -> ContainerAttrs {
        ContainerAttrs::from_metas(metas)
    }
    fn struct_named(cattr: ContainerAttrs, fields: Vec<Field>) -> Input {
        Input {
            ident: "S".into(),
            generics: Generics::default(),
            data: Data::Struct(Fields::Named(fields)),
            cattr,
        }
    }
    fn generic_input(
        ident: &str,
        params: Vec<GenericParam>,
        where_preds: Vec<String>,
        data: Data,
        cattr: ContainerAttrs,
    ) -> Input {
        Input {
            ident: ident.to_string(),
            generics: Generics {
                params,
                where_preds,
            },
            data,
            cattr,
        }
    }

    // -- validate_input ------------------------------------------------------

    #[test]
    fn validate_rejects_transparent_enum() {
        let input = Input {
            ident: "E".into(),
            generics: Generics::default(),
            data: Data::Enum(vec![Variant {
                ident: "A".into(),
                fields: Fields::Unit,
                attrs: vec![],
            }]),
            cattr: cattr(&[meta_flag("transparent")]),
        };
        let msg = validate_input(&input).expect("must reject transparent enum");
        assert!(msg.contains("transparent"));
    }

    #[test]
    fn validate_rejects_transparent_multi_field() {
        let input = struct_named(
            cattr(&[meta_flag("transparent")]),
            vec![
                named_field("a", "i32", vec![]),
                named_field("b", "i32", vec![]),
            ],
        );
        assert!(validate_input(&input).is_some());
    }

    #[test]
    fn validate_rejects_flatten_on_tuple() {
        let input = Input {
            ident: "T".into(),
            generics: Generics::default(),
            data: Data::Struct(Fields::Unnamed(vec![unnamed_field(
                "std::collections::BTreeMap<String, i32>",
                vec![meta_flag("flatten")],
            )])),
            cattr: cattr(&[]),
        };
        let msg = validate_input(&input).expect("must reject flatten on tuple field");
        assert!(msg.contains("flatten"), "unexpected error: {msg}");
    }

    #[test]
    fn validate_rejects_flatten_with_skip_if() {
        let input = struct_named(
            cattr(&[]),
            vec![named_field(
                "m",
                "std::collections::BTreeMap<String, i32>",
                vec![
                    meta_flag("flatten"),
                    meta_named("skip_serializing_if", "Option::is_none"),
                ],
            )],
        );
        let msg = validate_input(&input).expect("must reject flatten + skip_serializing_if");
        assert!(msg.contains("flatten"), "unexpected error: {msg}");
    }

    #[test]
    fn validate_accepts_valid_combinations() {
        let ok = struct_named(
            cattr(&[]),
            vec![
                named_field("a", "i32", vec![]),
                named_field(
                    "m",
                    "std::collections::BTreeMap<String, i32>",
                    vec![meta_flag("flatten")],
                ),
            ],
        );
        assert!(validate_input(&ok).is_none());

        let w = Input {
            ident: "W".into(),
            generics: Generics::default(),
            data: Data::Struct(Fields::Unnamed(vec![unnamed_field("i32", vec![])])),
            cattr: cattr(&[meta_flag("transparent")]),
        };
        assert!(validate_input(&w).is_none());
    }

    // -- build_generics ------------------------------------------------------

    #[test]
    fn build_generics_keeps_where_clause_with_bound() {
        // `bound` must replace only the auto bounds; the struct's own where
        // clause must always survive.
        let input = generic_input(
            "S",
            vec![GenericParam {
                kind: ParamKind::Type,
                full: "T".into(),
                name: "T".into(),
            }],
            vec!["T: core::fmt::Debug".into()],
            Data::Struct(Fields::Named(vec![named_field("v", "T", vec![])])),
            cattr(&[meta_named("bound", "\"T: Clone\"")]),
        );
        let (_, _, wc) = build_generics(&input, "::nextjson", false, false, false);
        assert!(wc.contains("Clone"), "missing user bound: {wc}");
        assert!(wc.contains("Debug"), "missing struct where clause: {wc}");
    }

    #[test]
    fn build_generics_instantiates_conversion_self_type() {
        // Conversion bounds must name `Dst<T>`, never bare `Dst`.
        let input = generic_input(
            "Dst",
            vec![GenericParam {
                kind: ParamKind::Type,
                full: "T".into(),
                name: "T".into(),
            }],
            vec![],
            Data::Struct(Fields::Named(vec![named_field("x", "T", vec![])])),
            cattr(&[meta_named("from", "\"Src<T>\"")]),
        );
        let (_, _, wc) = build_generics(&input, "::nextjson", true, false, false);
        assert!(
            wc.contains("Into<Dst<T>>"),
            "missing instantiated self: {wc}"
        );
        assert!(!wc.contains("Into<Dst>"), "bare self type: {wc}");
    }

    #[test]
    fn build_generics_adds_default_bound_for_generic_default() {
        // Container `default` on a generic struct must add `T: Default`.
        let input = generic_input(
            "S",
            vec![GenericParam {
                kind: ParamKind::Type,
                full: "T".into(),
                name: "T".into(),
            }],
            vec![],
            Data::Struct(Fields::Named(vec![named_field("v", "T", vec![])])),
            cattr(&[meta_flag("default")]),
        );
        let (_, _, wc) = build_generics(&input, "::nextjson", true, false, false);
        assert!(
            wc.contains("T: ::core::default::Default"),
            "missing Default bound: {wc}"
        );
    }

    #[test]
    fn build_generics_remote_never_appends_generics_twice() {
        // The impl target for `remote` carries its own generic arguments.
        let input = generic_input(
            "Mirror",
            vec![GenericParam {
                kind: ParamKind::Type,
                full: "T".into(),
                name: "T".into(),
            }],
            vec![],
            Data::Struct(Fields::Named(vec![named_field("x", "T", vec![])])),
            cattr(&[meta_named("remote", "external::Foreign<T>")]),
        );
        let (_, _, wc) = build_generics(&input, "::nextjson", false, false, false);
        assert!(
            wc.contains("T: ::nextjson::NsonSerialize"),
            "missing auto bound: {wc}"
        );
    }

    // -- default-bound detection ---------------------------------------------

    #[test]
    fn default_bound_not_needed_for_phantom_data() {
        // A `PhantomData` field must not force `T: Default`.
        let input = struct_named(
            cattr(&[]),
            vec![
                named_field("_m", "core::marker::PhantomData<T>", vec![]),
                named_field("n", "i32", vec![]),
            ],
        );
        assert!(!de_uses_type_param_default(&input));

        // With a container default the bound is required again.
        let input2 = struct_named(
            cattr(&[meta_flag("default")]),
            vec![named_field("v", "T", vec![])],
        );
        assert!(de_uses_type_param_default(&input2));
    }

    // -- PhantomData detection ----------------------------------------------

    #[test]
    fn phantom_detection_spellings() {
        assert!(crate::schema::is_phantom_data("PhantomData<T>"));
        assert!(crate::schema::is_phantom_data(
            "core::marker::PhantomData<T>"
        ));
        assert!(crate::schema::is_phantom_data(
            "::core::marker::PhantomData<T>"
        ));
        assert!(crate::schema::is_phantom_data(
            "std::marker::PhantomData<T>"
        ));
        assert!(!crate::schema::is_phantom_data("Vec<T>"));
        assert!(!crate::schema::is_phantom_data("Option<PhantomData<T>>"));
        assert!(!crate::schema::is_phantom_data("Phantom"));
    }
}