1use proc_macro2::{Span, TokenStream, TokenTree};
2use syn::parse::{Error, Parse, ParseStream, Result};
3use syn::{parenthesized, token, Attribute, Data, DeriveInput, Expr, Fields, Ident, Meta, Token};
4
5pub struct Input {
6 pub ident: Ident,
7 pub repr: Ident,
8 pub variants: Vec<Variant>,
9 pub default_variant: Option<Variant>,
10}
11
12#[derive(Clone)]
13pub struct Variant {
14 pub ident: Ident,
15 pub attrs: VariantAttrs,
16}
17
18#[derive(Clone)]
19pub struct VariantAttrs {
20 pub is_default: bool,
21}
22
23fn parse_serde_attr(attrs: &mut VariantAttrs, attr: &Attribute) {
24 if let Meta::List(_) = attr.meta {
25 let _ = attr.parse_nested_meta(|meta: ParseNestedMeta<'_>| {
26 if meta.input.peek(Token![=]) {
27 let _value: Expr = meta.value()?.parse()?;
28 } else if meta.input.peek(token:token::Paren) {
29 let _group: TokenTree = meta.input.parse()?;
30 } else if meta.path.is_ident("other") {
31 attrs.is_default = true;
32 }
33 Ok(())
34 });
35 }
36}
37
38fn parse_attrs(variant: &syn::Variant) -> VariantAttrs {
39 let mut attrs: VariantAttrs = VariantAttrs { is_default: false };
40 for attr: &Attribute in &variant.attrs {
41 if attr.path().is_ident("serde") {
42 parse_serde_attr(&mut attrs, attr);
43 }
44 }
45 attrs
46}
47
48impl Parse for Input {
49 fn parse(input: ParseStream) -> Result<Self> {
50 let call_site = Span::call_site();
51 let derive_input = DeriveInput::parse(input)?;
52
53 let data = match derive_input.data {
54 Data::Enum(data) => data,
55 _ => {
56 return Err(Error::new(call_site, "input must be an enum"));
57 }
58 };
59
60 let variants = data
61 .variants
62 .into_iter()
63 .map(|variant| match variant.fields {
64 Fields::Unit => {
65 let attrs = parse_attrs(&variant);
66 Ok(Variant {
67 ident: variant.ident,
68 attrs,
69 })
70 }
71 Fields::Named(_) | Fields::Unnamed(_) => {
72 Err(Error::new(variant.ident.span(), "must be a unit variant to use serde_repr derive"))
73 }
74 })
75 .collect::<Result<Vec<Variant>>>()?;
76
77 if variants.is_empty() {
78 return Err(Error::new(call_site, "there must be at least one variant"));
79 }
80
81 let generics = derive_input.generics;
82 if !generics.params.is_empty() || generics.where_clause.is_some() {
83 return Err(Error::new(call_site, "generic enum is not supported"));
84 }
85
86 let mut repr = None;
87 for attr in derive_input.attrs {
88 if attr.path().is_ident("repr") {
89 if let Meta::List(meta) = &attr.meta {
90 meta.parse_nested_meta(|meta| {
91 const RECOGNIZED: &[&str] = &[
92 "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64",
93 "i128", "isize",
94 ];
95 if RECOGNIZED.iter().any(|int| meta.path.is_ident(int)) {
96 repr = Some(meta.path.get_ident().unwrap().clone());
97 return Ok(());
98 }
99 if meta.path.is_ident("align") || meta.path.is_ident("packed") {
100 if meta.input.peek(token::Paren) {
101 let arg;
102 parenthesized!(arg in meta.input);
103 let _ = arg.parse::<TokenStream>()?;
104 }
105 return Ok(());
106 }
107 Err(meta.error("unsupported repr for serde_repr enum"))
108 })?;
109 }
110 }
111 }
112 let repr = repr.ok_or_else(|| Error::new(call_site, "missing #[repr(...)] attribute"))?;
113
114 let mut default_variants = variants.iter().filter(|x| x.attrs.is_default);
115 let default_variant = default_variants.next().cloned();
116 if default_variants.next().is_some() {
117 return Err(Error::new(
118 call_site,
119 "only one variant can be #[serde(other)]",
120 ));
121 }
122
123 Ok(Input {
124 ident: derive_input.ident,
125 repr,
126 variants,
127 default_variant,
128 })
129 }
130}
131