Skip to main content

nextjson_derive/
lib.rs

1//! Zero-dependency derive macros for `nextjson`.
2//!
3//! Implemented entirely with the standard `proc_macro` API: no `syn`, no
4//! `quote`, no `proc-macro2`. The input `TokenStream` is parsed by a
5//! hand-written recursive-descent parser into a small AST, and the output is
6//! generated as text and re-parsed.
7
8#![deny(unsafe_code)]
9#![deny(missing_docs)]
10#![doc(html_root_url = "https://docs.rs/nextjson-derive")]
11
12extern crate proc_macro;
13
14use proc_macro::{Delimiter, Ident, Spacing, TokenStream, TokenTree};
15use std::str::FromStr;
16
17mod attr;
18mod case;
19mod de;
20mod schema;
21mod ser;
22
23pub(crate) use attr::{ContainerAttrs, FieldAttrs, VariantAttrs};
24
25/// Parse a string into a TokenStream.
26pub(crate) fn ts(s: &str) -> TokenStream {
27    TokenStream::from_str(s)
28        .unwrap_or_else(|e| panic!("nextjson-derive: invalid generated tokens: {e:?}"))
29}
30
31/// Build a `compile_error!` expansion from a message.
32pub(crate) fn err(msg: &str) -> TokenStream {
33    ts(&format!("::core::compile_error!({:?})", msg))
34}
35
36/// Error string returned by codegen helpers.
37pub(crate) fn err_str(msg: &str) -> String {
38    msg.to_string()
39}
40
41/// Token cursor over a slice of TokenTrees.
42pub(crate) struct P<'a> {
43    pub toks: &'a [TokenTree],
44    pub i: usize,
45}
46
47impl<'a> P<'a> {
48    pub fn peek(&self) -> Option<&TokenTree> {
49        self.toks.get(self.i)
50    }
51    pub fn next(&mut self) -> Option<TokenTree> {
52        let t = self.toks.get(self.i).cloned();
53        if t.is_some() {
54            self.i += 1;
55        }
56        t
57    }
58    pub fn is_ident(&self, s: &str) -> bool {
59        matches!(self.peek(), Some(TokenTree::Ident(id)) if id.to_string() == s)
60    }
61    pub fn is_punct(&self, ch: char) -> bool {
62        matches!(self.peek(), Some(TokenTree::Punct(p)) if p.as_char() == ch)
63    }
64    pub fn eat_ident(&mut self, s: &str) -> bool {
65        if self.is_ident(s) {
66            self.i += 1;
67            true
68        } else {
69            false
70        }
71    }
72    pub fn eat_punct(&mut self, ch: char) -> bool {
73        if self.is_punct(ch) {
74            self.i += 1;
75            true
76        } else {
77            false
78        }
79    }
80    pub fn expect_ident(&mut self) -> Option<String> {
81        match self.next() {
82            Some(TokenTree::Ident(id)) => Some(id.to_string()),
83            _ => None,
84        }
85    }
86}
87
88/// Join tokens into a re-parseable string, preserving `Joint` spacing so
89/// that punctuation sequences (`::`, `'a`, `->`, `>>`) stay adjacent.
90pub(crate) fn join(toks: &[TokenTree]) -> String {
91    let mut s = String::new();
92    let mut no_space = false;
93    for t in toks {
94        if !s.is_empty() && !no_space {
95            s.push(' ');
96        }
97        no_space = false;
98        match t {
99            TokenTree::Punct(p) => {
100                s.push_str(&p.to_string());
101                no_space = p.spacing() == Spacing::Joint;
102            }
103            _ => s.push_str(&t.to_string()),
104        }
105    }
106    s
107}
108
109/// Split tokens at a top-level separator.
110///
111/// Angle brackets are tracked so that generic types such as
112/// `BTreeMap<String, i32>` stay on a single side of the split.
113pub(crate) fn split_top(toks: &[TokenTree], sep: char) -> Vec<Vec<TokenTree>> {
114    let mut out: Vec<Vec<TokenTree>> = Vec::new();
115    let mut cur: Vec<TokenTree> = Vec::new();
116    let mut angle: usize = 0;
117    for tt in toks {
118        match tt {
119            TokenTree::Group(_) => cur.push(tt.clone()),
120            TokenTree::Punct(p) if p.as_char() == '<' => {
121                angle += 1;
122                cur.push(tt.clone());
123            }
124            TokenTree::Punct(p) if p.as_char() == '>' => {
125                angle = angle.saturating_sub(1);
126                cur.push(tt.clone());
127            }
128            TokenTree::Punct(p) if angle == 0 && p.as_char() == sep => {
129                out.push(std::mem::take(&mut cur));
130            }
131            _ => cur.push(tt.clone()),
132        }
133    }
134    if !cur.is_empty() {
135        out.push(cur);
136    }
137    if out.is_empty() {
138        out.push(Vec::new());
139    }
140    out
141}
142
143/// Read a `<...>` group. proc_macro does not group angle brackets, so this
144/// scans for the matching `>` while ignoring `->` arrow tokens.
145pub(crate) fn read_angle(p: &mut P) -> Option<Vec<TokenTree>> {
146    if !p.eat_punct('<') {
147        return None;
148    }
149    let mut depth = 1usize;
150    let mut out = Vec::new();
151    while let Some(tt) = p.next() {
152        match &tt {
153            TokenTree::Punct(c) if c.as_char() == '<' => {
154                depth += 1;
155                out.push(tt);
156            }
157            TokenTree::Punct(c)
158                if c.as_char() == '-'
159                    && matches!(p.peek(), Some(TokenTree::Punct(n)) if n.as_char() == '>') =>
160            {
161                out.push(tt);
162                out.push(p.next().unwrap());
163            }
164            TokenTree::Punct(c) if c.as_char() == '>' => {
165                if depth == 1 {
166                    return Some(out);
167                }
168                depth -= 1;
169                out.push(tt);
170            }
171            _ => out.push(tt),
172        }
173    }
174    None
175}
176
177// ---------------------------------------------------------------------------
178// AST
179// ---------------------------------------------------------------------------
180
181#[derive(Clone, Copy, PartialEq, Eq)]
182pub(crate) enum ParamKind {
183    Lifetime,
184    Type,
185    Const,
186}
187
188#[derive(Clone)]
189pub(crate) struct GenericParam {
190    pub kind: ParamKind,
191    pub full: String,
192    pub name: String,
193}
194
195#[derive(Clone, Default)]
196pub(crate) struct Generics {
197    pub params: Vec<GenericParam>,
198    pub where_preds: Vec<String>,
199}
200
201#[derive(Clone)]
202pub(crate) struct Field {
203    pub ident: Option<String>,
204    pub ty: String,
205    pub attrs: Vec<attr::Meta>,
206}
207
208#[derive(Clone)]
209pub(crate) enum Fields {
210    Unit,
211    Named(Vec<Field>),
212    Unnamed(Vec<Field>),
213}
214
215impl Fields {
216    pub fn iter(&self) -> Vec<&Field> {
217        match self {
218            Fields::Unit => Vec::new(),
219            Fields::Named(f) | Fields::Unnamed(f) => f.iter().collect(),
220        }
221    }
222}
223
224#[derive(Clone)]
225pub(crate) struct Variant {
226    pub ident: String,
227    pub fields: Fields,
228    pub attrs: Vec<attr::Meta>,
229}
230
231#[derive(Clone)]
232pub(crate) enum Data {
233    Struct(Fields),
234    Enum(Vec<Variant>),
235}
236
237#[derive(Clone)]
238pub(crate) struct Input {
239    pub ident: String,
240    pub generics: Generics,
241    pub data: Data,
242    pub cattr: ContainerAttrs,
243}
244
245// ---------------------------------------------------------------------------
246// Attribute collection
247// ---------------------------------------------------------------------------
248
249/// Collect leading `#[...]` attribute groups.
250fn parse_attrs(p: &mut P) -> Vec<Vec<TokenTree>> {
251    let mut out = Vec::new();
252    while p.is_punct('#') {
253        p.next();
254        if let Some(TokenTree::Group(g)) = p.next() {
255            if g.delimiter() == Delimiter::Bracket {
256                out.push(g.stream().into_iter().collect());
257            }
258        }
259    }
260    out
261}
262
263/// Extract `njson` / `nextjson` metas from a set of attribute groups.
264fn collect_metas(groups: &[Vec<TokenTree>]) -> Vec<attr::Meta> {
265    let mut out = Vec::new();
266    for g in groups {
267        out.extend(attr::metas_from_attr(g));
268    }
269    out
270}
271
272// ---------------------------------------------------------------------------
273// Top-level parse
274// ---------------------------------------------------------------------------
275
276pub(crate) fn parse_input(input: TokenStream) -> Result<Input, String> {
277    let toks: Vec<TokenTree> = input.into_iter().collect();
278    let mut p = P { toks: &toks, i: 0 };
279
280    let attrs = parse_attrs(&mut p);
281    let cattr = ContainerAttrs::from_metas(&collect_metas(&attrs));
282
283    let is_enum = if p.eat_ident("struct") {
284        false
285    } else if p.eat_ident("enum") {
286        true
287    } else {
288        return Err("nextjson: expected `struct` or `enum`".into());
289    };
290
291    let ident = p
292        .expect_ident()
293        .ok_or_else(|| "nextjson: expected type name".to_string())?;
294
295    let mut generics = Generics::default();
296    if let Some(inner) = read_angle(&mut p) {
297        generics = parse_generics(&inner);
298    }
299
300    if p.eat_ident("where") {
301        let mut cur = Vec::new();
302        loop {
303            match p.peek() {
304                Some(TokenTree::Group(g))
305                    if matches!(g.delimiter(), Delimiter::Brace | Delimiter::Parenthesis) =>
306                {
307                    break
308                }
309                Some(_) => cur.push(p.next().unwrap()),
310                None => break,
311            }
312        }
313        for piece in split_top(&cur, ',') {
314            let s = join(&piece).trim().to_string();
315            if !s.is_empty() && s != ";" {
316                generics.where_preds.push(s);
317            }
318        }
319    }
320
321    let data = if !is_enum {
322        match p.next() {
323            Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => {
324                let inner: Vec<TokenTree> = g.stream().into_iter().collect();
325                Data::Struct(Fields::Named(parse_named_fields(&inner)))
326            }
327            Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Parenthesis => {
328                let inner: Vec<TokenTree> = g.stream().into_iter().collect();
329                Data::Struct(Fields::Unnamed(parse_unnamed_fields(&inner)))
330            }
331            Some(TokenTree::Punct(pc)) if pc.as_char() == ';' => Data::Struct(Fields::Unit),
332            _ => return Err("nextjson: expected a struct body".into()),
333        }
334    } else {
335        match p.next() {
336            Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => {
337                let inner: Vec<TokenTree> = g.stream().into_iter().collect();
338                Data::Enum(parse_variants(&inner))
339            }
340            _ => return Err("nextjson: expected an enum body".into()),
341        }
342    };
343
344    Ok(Input {
345        ident,
346        generics,
347        data,
348        cattr,
349    })
350}
351
352fn parse_generics(inner: &[TokenTree]) -> Generics {
353    let mut g = Generics::default();
354    for item in split_top(inner, ',') {
355        if item.is_empty() {
356            continue;
357        }
358        let mut p = P { toks: &item, i: 0 };
359        if p.is_punct('\'') {
360            p.next();
361            let name = p.expect_ident().unwrap_or_default();
362            g.params.push(GenericParam {
363                kind: ParamKind::Lifetime,
364                full: join(&item),
365                name: format!("'{name}"),
366            });
367        } else if p.eat_ident("const") {
368            let name = p.expect_ident().unwrap_or_default();
369            g.params.push(GenericParam {
370                kind: ParamKind::Const,
371                full: join(&item),
372                name,
373            });
374        } else {
375            let name = p.expect_ident().unwrap_or_default();
376            g.params.push(GenericParam {
377                kind: ParamKind::Type,
378                full: join(&item),
379                name,
380            });
381        }
382    }
383    g
384}
385
386fn parse_named_fields(inner: &[TokenTree]) -> Vec<Field> {
387    split_top(inner, ',')
388        .iter()
389        .filter(|s| !s.is_empty())
390        .map(|piece| parse_named_field(piece))
391        .collect()
392}
393
394fn parse_named_field(piece: &[TokenTree]) -> Field {
395    let mut p = P { toks: piece, i: 0 };
396    let attrs = parse_attrs(&mut p);
397    if p.eat_ident("pub") && p.is_punct('(') {
398        p.next();
399        p.next();
400    }
401    // Find the field separator ':' at top level, excluding '::'.
402    let mut colon = None;
403    let mut j = p.i;
404    while j < piece.len() {
405        match &piece[j] {
406            TokenTree::Punct(c) if c.as_char() == ':' => {
407                if matches!(piece.get(j + 1), Some(TokenTree::Punct(n)) if n.as_char() == ':') {
408                    j += 2;
409                    continue;
410                }
411                colon = Some(j);
412                break;
413            }
414            _ => j += 1,
415        }
416    }
417    match colon {
418        Some(c) => Field {
419            ident: Some(join(&piece[p.i..c]).trim().to_string()),
420            ty: join(&piece[c + 1..]).trim().to_string(),
421            attrs: collect_metas(&attrs),
422        },
423        None => Field {
424            ident: None,
425            ty: join(&piece[p.i..]).trim().to_string(),
426            attrs: collect_metas(&attrs),
427        },
428    }
429}
430
431fn parse_unnamed_fields(inner: &[TokenTree]) -> Vec<Field> {
432    split_top(inner, ',')
433        .iter()
434        .filter(|s| !s.is_empty())
435        .map(|piece| {
436            let mut p = P { toks: piece, i: 0 };
437            let attrs = parse_attrs(&mut p);
438            if p.eat_ident("pub") && p.is_punct('(') {
439                p.next();
440                p.next();
441            }
442            Field {
443                ident: None,
444                ty: join(&piece[p.i..]).trim().to_string(),
445                attrs: collect_metas(&attrs),
446            }
447        })
448        .collect()
449}
450
451fn parse_variants(inner: &[TokenTree]) -> Vec<Variant> {
452    split_top(inner, ',')
453        .iter()
454        .filter(|s| !s.is_empty())
455        .map(|piece| {
456            let mut p = P { toks: piece, i: 0 };
457            let attrs = parse_attrs(&mut p);
458            let ident = p.expect_ident().unwrap_or_default();
459            let fields = match p.next() {
460                Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => {
461                    let inner2: Vec<TokenTree> = g.stream().into_iter().collect();
462                    Fields::Named(parse_named_fields(&inner2))
463                }
464                Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Parenthesis => {
465                    let inner2: Vec<TokenTree> = g.stream().into_iter().collect();
466                    Fields::Unnamed(parse_unnamed_fields(&inner2))
467                }
468                _ => Fields::Unit,
469            };
470            Variant {
471                ident,
472                fields,
473                attrs: collect_metas(&attrs),
474            }
475        })
476        .collect()
477}
478
479// ---------------------------------------------------------------------------
480// Generic helpers for code generation
481// ---------------------------------------------------------------------------
482
483/// Build `(impl_generics, ty_generics, where_clause)` for the impl header.
484pub(crate) fn build_generics(
485    input: &Input,
486    cp: &str,
487    de: bool,
488    has_flatten: bool,
489    has_borrow: bool,
490) -> (String, String, String) {
491    let g = &input.generics;
492    let c = &input.cattr;
493
494    let mut impl_params: Vec<String> = g.params.iter().map(|p| p.full.clone()).collect();
495    if de {
496        impl_params.insert(0, "'de".to_string());
497    }
498    let impl_generics = if impl_params.is_empty() {
499        String::new()
500    } else {
501        format!("<{}>", impl_params.join(", "))
502    };
503
504    let names: Vec<String> = g.params.iter().map(|p| p.name.clone()).collect();
505    let ty_generics = if names.is_empty() {
506        String::new()
507    } else {
508        format!("<{}>", names.join(", "))
509    };
510
511    let mut preds: Vec<String> = if let Some(bound) = &c.bound {
512        let cleaned = bound.trim().trim_matches('"');
513        if cleaned.is_empty() {
514            Vec::new()
515        } else {
516            cleaned
517                .split(',')
518                .map(|s| s.trim().to_string())
519                .filter(|s| !s.is_empty())
520                .collect()
521        }
522    } else {
523        let mut v: Vec<String> = g.where_preds.clone();
524        for p in g.params.iter() {
525            if p.kind != ParamKind::Type {
526                continue;
527            }
528            if de && has_flatten {
529                v.push(format!(
530                    "{0}: for<'__n> {1}::NsonDeserialize<'__n>",
531                    p.name, cp
532                ));
533            } else if de {
534                v.push(format!("{}: {}::NsonDeserialize<'de>", p.name, cp));
535            } else {
536                v.push(format!("{}: {}::NsonSerialize", p.name, cp));
537            }
538        }
539        v
540    };
541
542    if de && has_borrow {
543        for p in g.params.iter() {
544            if p.kind == ParamKind::Lifetime {
545                preds.push(format!("'de: {}", p.name));
546            }
547        }
548    }
549
550    let where_clause = if preds.is_empty() {
551        String::new()
552    } else {
553        format!(" where {}", preds.join(", "))
554    };
555
556    (impl_generics, ty_generics, where_clause)
557}
558
559/// Emit the `NsonSchema` + `NsonSerialize` impls.
560pub(crate) fn generate_impls(input: &Input) -> TokenStream {
561    let cp = input.cattr.crate_path.clone();
562    let name = input.ident.clone();
563    let (ig, tg, wc) = build_generics(input, &cp, false, false, false);
564    let schema_expr = schema::schema_expr(input, &cp);
565    let body = match &input.data {
566        Data::Struct(f) => ser::serialize_struct(&name, f, input, &cp),
567        Data::Enum(v) => ser::serialize_enum(&name, v, input, &cp),
568    };
569    let out = format!(
570        "#[automatically_derived]\n\
571         impl {ig} {cp}::NsonSchema for {name}{tg}{wc} {{\n\
572         \x20   const SCHEMA: {cp}::TypeSchema = {schema_expr};\n\
573         }}\n\
574         #[automatically_derived]\n\
575         impl {ig} {cp}::NsonSerialize for {name}{tg}{wc} {{\n\
576         \x20   fn nextencode<__W: {cp}::Write>(&self, __e: &mut {cp}::Encoder<__W>) -> {cp}::Result<()> {{\n\
577         {body}\n\
578         \x20   }}\n\
579         }}"
580    );
581    ts(&out)
582}
583
584/// Emit the `NsonDeserialize` impl.
585pub(crate) fn generate_de_impl(input: &Input) -> TokenStream {
586    let cp = input.cattr.crate_path.clone();
587    let name = input.ident.clone();
588    let has_flatten = type_has_flag(input, |fa| fa.flatten);
589    let has_borrow = type_has_flag(input, |fa| fa.borrow);
590    if has_flatten && type_has_with(input) {
591        return err("nextjson: `flatten` cannot be combined with `with` / `deserialize_with`");
592    }
593    let (ig, tg, wc) = build_generics(input, &cp, true, has_flatten, has_borrow);
594    let body = match &input.data {
595        Data::Struct(f) => de::deserialize_struct(&name, f, input, &cp, has_flatten),
596        Data::Enum(v) => de::deserialize_enum(&name, v, input, &cp, has_flatten),
597    };
598    let out = format!(
599        "#[automatically_derived]\n\
600         impl {ig} {cp}::NsonDeserialize<'de> for {name}{tg}{wc} {{\n\
601         \x20   fn nextdecode_into(\n\
602         \x20       __d: &mut {cp}::Decoder<'de>,\n\
603         \x20       __out: &mut {cp}::DecodeSlot<Self>,\n\
604         \x20   ) -> {cp}::Result<()> {{\n\
605         {body}\n\
606         \x20   }}\n\
607         }}"
608    );
609    ts(&out)
610}
611
612fn type_has_flag<F: Fn(&FieldAttrs) -> bool>(input: &Input, f: F) -> bool {
613    match &input.data {
614        Data::Struct(fields) => fields
615            .iter()
616            .iter()
617            .any(|fld| f(&attr::field_attrs(&fld.attrs))),
618        Data::Enum(variants) => variants.iter().any(|v| {
619            v.fields
620                .iter()
621                .iter()
622                .any(|fld| f(&attr::field_attrs(&fld.attrs)))
623        }),
624    }
625}
626
627fn type_has_with(input: &Input) -> bool {
628    type_has_flag(input, |fa| {
629        fa.with.is_some() || fa.deserialize_with.is_some()
630    })
631}
632
633/// Build a `proc_macro::Ident` (kept for API symmetry).
634#[allow(dead_code)]
635pub(crate) fn ident(name: &str) -> Ident {
636    Ident::new(name, proc_macro::Span::call_site())
637}
638
639// ---------------------------------------------------------------------------
640// Entry points
641// ---------------------------------------------------------------------------
642
643#[proc_macro_derive(NsonSerialize, attributes(njson, nextjson))]
644/// Derive NextJson's native serialization contract and compile-time schema.
645///
646/// Configuration is accepted through `#[njson(...)]`. The generated
647/// implementation writes directly through `NsonSerialize::nextencode` and
648/// exposes `NsonSchema::SCHEMA` without depending on another macro framework.
649pub fn derive_serialize(input: TokenStream) -> TokenStream {
650    match parse_input(input) {
651        Ok(ast) => generate_impls(&ast),
652        Err(e) => err(&e),
653    }
654}
655
656#[proc_macro_derive(NsonDeserialize, attributes(njson, nextjson))]
657/// Derive NextJson's native decoding contract.
658///
659/// Configuration is accepted through `#[njson(...)]`. The generated
660/// implementation decodes through checked `DecodeSlot` state and uses normal
661/// Rust drop semantics for partially initialized fields.
662pub fn derive_deserialize(input: TokenStream) -> TokenStream {
663    match parse_input(input) {
664        Ok(ast) => generate_de_impl(&ast),
665        Err(e) => err(&e),
666    }
667}