referrerpolicy=no-referrer-when-downgrade

frame_support_procedural/construct_runtime/expand/
composite_helper.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::construct_runtime::parse::PalletPath;
19use proc_macro2::{Ident, TokenStream};
20use quote::quote;
21
22pub(crate) fn expand_conversion_fn(
23	composite_name: &str,
24	path: &PalletPath,
25	instance: Option<&Ident>,
26	variant_name: &Ident,
27) -> TokenStream {
28	let composite_name = quote::format_ident!("{}", composite_name);
29	let runtime_composite_name = quote::format_ident!("Runtime{}", composite_name);
30
31	if let Some(inst) = instance {
32		quote! {
33			impl From<#path::#composite_name<#path::#inst>> for #runtime_composite_name {
34				fn from(hr: #path::#composite_name<#path::#inst>) -> Self {
35					#runtime_composite_name::#variant_name(hr)
36				}
37			}
38		}
39	} else {
40		quote! {
41			impl From<#path::#composite_name> for #runtime_composite_name {
42				fn from(hr: #path::#composite_name) -> Self {
43					#runtime_composite_name::#variant_name(hr)
44				}
45			}
46		}
47	}
48}
49
50pub(crate) fn expand_variant(
51	composite_name: &str,
52	index: u8,
53	path: &PalletPath,
54	instance: Option<&Ident>,
55	variant_name: &Ident,
56) -> TokenStream {
57	let composite_name = quote::format_ident!("{}", composite_name);
58
59	if let Some(inst) = instance {
60		quote! {
61			#[codec(index = #index)]
62			#variant_name(#path::#composite_name<#path::#inst>),
63		}
64	} else {
65		quote! {
66			#[codec(index = #index)]
67			#variant_name(#path::#composite_name),
68		}
69	}
70}
71
72pub(crate) fn expand_variant_count(
73	composite_name: &str,
74	path: &PalletPath,
75	instance: Option<&Ident>,
76) -> TokenStream {
77	let composite_name = quote::format_ident!("{}", composite_name);
78
79	if let Some(inst) = instance {
80		quote! {
81			#path::#composite_name::<#path::#inst>::VARIANT_COUNT
82		}
83	} else {
84		// Wrapped `<`..`>` means: use default type parameter for enum.
85		//
86		// This is used for pallets without instance support or pallets with instance support when
87		// we don't specify instance:
88		//
89		// ```
90		// pub struct Pallet<T, I = ()>{..}
91		//
92		// #[pallet::composite_enum]
93		// pub enum HoldReason<I: 'static = ()> {..}
94		//
95		// Pallet1: pallet_x,  // <- default type parameter
96		// ```
97		quote! {
98			<#path::#composite_name>::VARIANT_COUNT
99		}
100	}
101}