referrerpolicy=no-referrer-when-downgrade

xcm_procedural/
weight_info.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17use 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								// If the type is already a reference, do nothing
38								quote::quote!(#r)
39							},
40							t => {
41								// Otherwise, make it a reference
42								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}