xcm_procedural/enum_variants.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
17//! Simple derive macro for getting the number of variants in an enum.
18
19use proc_macro2::TokenStream as TokenStream2;
20use quote::{format_ident, quote};
21use syn::{Data, DeriveInput, Error, Result};
22
23pub fn derive(input: DeriveInput) -> Result<TokenStream2> {
24 let data_enum = match &input.data {
25 Data::Enum(data_enum) => data_enum,
26 _ => return Err(Error::new_spanned(&input, "Expected an enum.")),
27 };
28 let ident = format_ident!("{}NumVariants", input.ident);
29 let number_of_variants: usize = data_enum.variants.iter().count();
30 Ok(quote! {
31 pub struct #ident;
32 impl ::frame_support::traits::Get<u32> for #ident {
33 fn get() -> u32 {
34 #number_of_variants as u32
35 }
36 }
37 })
38}