frame_support_procedural/pallet/expand/composite.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use crate::pallet::Def;
19use proc_macro2::TokenStream;
20
21/// Expands `composite_enum` and adds the `VariantCount` implementation for it.
22pub fn expand_composites(def: &mut Def) -> TokenStream {
23 let mut expand = quote::quote!();
24 let frame_support = &def.frame_support;
25
26 for composite in &def.composites {
27 let name = &composite.ident;
28 let (impl_generics, ty_generics, where_clause) = composite.generics.split_for_impl();
29 let variants_count = composite.variant_count;
30
31 // add `VariantCount` implementation for `composite_enum`
32 expand.extend(quote::quote_spanned!(composite.attr_span =>
33 impl #impl_generics #frame_support::traits::VariantCount for #name #ty_generics #where_clause {
34 const VARIANT_COUNT: u32 = #variants_count;
35 }
36 ));
37 }
38
39 expand
40}