xcm_procedural/
weight_info.rs1use inflector::Inflector;
18use quote::format_ident;
19
20pub fn derive(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
21 let input: syn::DeriveInput = match syn::parse(item) {
22 Ok(input) => input,
23 Err(e) => return e.into_compile_error().into(),
24 };
25
26 let syn::DeriveInput { generics, data, .. } = input;
27
28 match data {
29 syn::Data::Enum(syn::DataEnum { variants, .. }) => {
30 let methods = variants.into_iter().map(|syn::Variant { ident, fields, .. }| {
31 let snake_cased_ident = format_ident!("{}", ident.to_string().to_snake_case());
32 let ref_fields =
33 fields.into_iter().enumerate().map(|(idx, syn::Field { ident, ty, .. })| {
34 let field_name = ident.unwrap_or_else(|| format_ident!("_{}", idx));
35 let field_ty = match ty {
36 syn::Type::Reference(r) => {
37 quote::quote!(#r)
39 },
40 t => {
41 quote::quote!(&#t)
43 },
44 };
45
46 quote::quote!(#field_name: #field_ty,)
47 });
48 quote::quote!(fn #snake_cased_ident( #(#ref_fields)* ) -> Weight;)
49 });
50
51 let res = quote::quote! {
52 pub trait XcmWeightInfo #generics {
53 #(#methods)*
54 }
55 };
56 res.into()
57 },
58 syn::Data::Struct(syn::DataStruct { struct_token, .. }) => {
59 let msg = "structs are not supported by 'derive(XcmWeightInfo)'";
60 syn::Error::new(struct_token.span, msg).into_compile_error().into()
61 },
62 syn::Data::Union(syn::DataUnion { union_token, .. }) => {
63 let msg = "unions are not supported by 'derive(XcmWeightInfo)'";
64 syn::Error::new(union_token.span, msg).into_compile_error().into()
65 },
66 }
67}