Skip to main content

serde_implicit_proc/
lib.rs

1use proc_macro::TokenStream as TS1;
2use syn::{DeriveInput, parse_macro_input};
3
4mod ast;
5mod expand;
6mod tuple_enum;
7
8/// Derive macro for implicitly tagged enum deserialization.
9///
10/// Annotate one field per variant with `#[serde_implicit(tag)]` to mark it as
11/// the discriminant. When that key appears in the input, the deserializer
12/// commits to the corresponding variant and produces targeted error messages
13/// instead of serde's generic "data did not match any variant" error.
14///
15/// **Tag fields should be non-optional.** During deserialization, keys whose
16/// value is `null` are ignored when searching for the implicit tag. If a tag
17/// field is `Option<T>` and the input contains `"field": null`, that variant
18/// will not be selected.
19// todo: shadow serde completely?
20#[proc_macro_derive(Deserialize, attributes(serde_implicit))]
21pub fn derive_serialize(input: TS1) -> TS1 {
22    let input = parse_macro_input!(input as DeriveInput);
23
24    let ts = expand::expand_derive_serialize(input)
25        .unwrap_or_else(syn::Error::into_compile_error)
26        .into();
27
28    ts
29}