MCPcopy Create free account
hub / github.com/csskit/csskit / generate

Function generate

crates/csskit_proc_macro/src/syntax.rs:28–70  ·  view source on GitHub ↗
(defs: Def, ast: DeriveInput)

Source from the content-addressed store, hash-verified

26}
27
28pub 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}

Callers 2

syntaxFunction · 0.70

Calls 9

has_derive_ofFunction · 0.85
maybe_unsizedMethod · 0.80
is_enumMethod · 0.80
suggested_data_typeMethod · 0.80
is_structMethod · 0.80
generate_definitionMethod · 0.80
spanMethod · 0.45
is_emptyMethod · 0.45

Tested by 1