| 26 | } |
| 27 | |
| 28 | pub fn generate(defs: Def, ast: DeriveInput) -> TokenStream { |
| 29 | let has_a_lifetime = ast.generics.lifetimes().any(|l| l.lifetime.ident == "a"); |
| 30 | if !has_a_lifetime && defs.maybe_unsized() { |
| 31 | return Error::new(ast.ident.span(), "this object needs the <'a> lifetime but it didn't have it. Add it") |
| 32 | .into_compile_error(); |
| 33 | } |
| 34 | let vis = &ast.vis; |
| 35 | let attrs = &ast.attrs; |
| 36 | let ident = &ast.ident; |
| 37 | match &ast.data { |
| 38 | Data::Enum(DataEnum { variants, .. }) => { |
| 39 | if !variants.is_empty() { |
| 40 | return Error::new(ident.span(), "enum must be empty").into_compile_error(); |
| 41 | } |
| 42 | if !defs.suggested_data_type().is_enum() { |
| 43 | return Error::new(ident.span(), "wrong structure for this syntax, please redefine as a Struct") |
| 44 | .into_compile_error(); |
| 45 | } |
| 46 | } |
| 47 | Data::Struct(DataStruct { fields, .. }) => { |
| 48 | if !fields.is_empty() { |
| 49 | return Error::new(ident.span(), "struct must be empty").into_compile_error(); |
| 50 | } |
| 51 | if !defs.suggested_data_type().is_struct() { |
| 52 | return Error::new(ident.span(), "wrong structure for this syntax, please redefine as an Enum") |
| 53 | .into_compile_error(); |
| 54 | } |
| 55 | } |
| 56 | Data::Union(_) => { |
| 57 | return Error::new(ident.span(), "cannot create from_syntax on Union").into_compile_error(); |
| 58 | } |
| 59 | } |
| 60 | let derives_visitable = has_derive_of(attrs, "Visitable"); |
| 61 | let derives_parse = has_derive_of(attrs, "Parse"); |
| 62 | let additonal_defs = defs.generate_additional_types(vis, ident, &ast.generics); |
| 63 | let def = defs.generate_definition(vis, ident, &ast.generics, derives_parse, derives_visitable); |
| 64 | quote! { |
| 65 | #additonal_defs |
| 66 | |
| 67 | #(#attrs)* |
| 68 | #def |
| 69 | } |
| 70 | } |