nextjson-derive 0.1.1

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
//! 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.

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

extern crate proc_macro;

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

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

pub(crate) use attr::{ContainerAttrs, FieldAttrs, 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);
        }
        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()),
            }
        }
    };

    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,
        }
    }
    match colon {
        Some(c) => Field {
            ident: Some(join(&piece[p.i..c]).trim().to_string()),
            ty: join(&piece[c + 1..]).trim().to_string(),
            attrs: collect_metas(&attrs),
        },
        None => Field {
            ident: None,
            ty: join(&piece[p.i..]).trim().to_string(),
            attrs: collect_metas(&attrs),
        },
    }
}

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 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(", "))
    };

    let mut preds: Vec<String> = if let Some(bound) = &c.bound {
        let cleaned = bound.trim().trim_matches('"');
        if cleaned.is_empty() {
            Vec::new()
        } else {
            cleaned
                .split(',')
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect()
        }
    } else {
        let mut v: Vec<String> = g.where_preds.clone();
        for p in g.params.iter() {
            if p.kind != ParamKind::Type {
                continue;
            }
            if de && has_flatten {
                v.push(format!(
                    "{0}: for<'__n> {1}::NsonDeserialize<'__n>",
                    p.name, cp
                ));
            } else if de {
                v.push(format!("{}: {}::NsonDeserialize<'de>", p.name, cp));
            } else {
                v.push(format!("{}: {}::NsonSerialize", p.name, cp));
            }
        }
        v
    };

    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)
}

/// Emit the `NsonSchema` + `NsonSerialize` impls.
pub(crate) fn generate_impls(input: &Input) -> TokenStream {
    let cp = input.cattr.crate_path.clone();
    let name = input.ident.clone();
    let (ig, tg, wc) = build_generics(input, &cp, false, false, false);
    let schema_expr = schema::schema_expr(input, &cp);
    let body = 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 {name}{tg}{wc} {{\n\
         \x20   const SCHEMA: {cp}::TypeSchema = {schema_expr};\n\
         }}\n\
         #[automatically_derived]\n\
         impl {ig} {cp}::NsonSerialize for {name}{tg}{wc} {{\n\
         \x20   fn nextencode<__E: {cp}::FormatEncoder>(&self, __e: &mut __E) -> {cp}::Result<()> {{\n\
         {body}\n\
         \x20   }}\n\
         }}"
    );
    ts(&out)
}

/// Emit the `NsonDeserialize` impl.
pub(crate) fn generate_de_impl(input: &Input) -> TokenStream {
    let cp = input.cattr.crate_path.clone();
    let name = input.ident.clone();
    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`");
    }
    let (ig, tg, wc) = build_generics(input, &cp, true, has_flatten, has_borrow);
    let body = 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 {name}{tg}{wc} {{\n\
         \x20   fn nextdecode_into<__D: {cp}::FormatDecoder<'de>>(\n\
         \x20       __d: &mut __D,\n\
         \x20       __out: &mut {cp}::DecodeSlot<Self>,\n\
         \x20   ) -> {cp}::Result<()> {{\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)))),
    }
}

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

/// Build a `proc_macro::Ident` (kept for API symmetry).
#[allow(dead_code)]
pub(crate) fn ident(name: &str) -> Ident {
    Ident::new(name, proc_macro::Span::call_site())
}

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

#[proc_macro_derive(NsonSerialize, attributes(njson, nextjson))]
/// Derive NextJson's native serialization contract and compile-time schema.
///
/// Configuration is accepted through `#[njson(...)]`. 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))]
/// Derive NextJson's native decoding contract.
///
/// Configuration is accepted through `#[njson(...)]`. 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),
    }
}