referrerpolicy=no-referrer-when-downgrade

frame_support_procedural/pallet/expand/
constants.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::{deprecation::extract_or_return_allow_attrs, pallet::Def};
19
20struct ConstDef {
21	/// Name of the associated type.
22	pub ident: syn::Ident,
23	/// The type in Get, e.g. `u32` in `type Foo: Get<u32>;`, but `Self` is replaced by `T`
24	pub type_: syn::Type,
25	/// The doc associated
26	pub doc: Vec<syn::Expr>,
27	/// default_byte implementation
28	pub default_byte_impl: proc_macro2::TokenStream,
29	/// Constant name for Metadata (optional)
30	pub metadata_name: Option<syn::Ident>,
31	/// Deprecation_info:
32	pub deprecation_info: proc_macro2::TokenStream,
33}
34
35/// Implement the `pallet_constants_metadata` function for the pallet.
36pub fn expand_constants(def: &mut Def) -> proc_macro2::TokenStream {
37	let frame_support = &def.frame_support;
38	let type_impl_gen = &def.type_impl_generics(proc_macro2::Span::call_site());
39	let type_use_gen = &def.type_use_generics(proc_macro2::Span::call_site());
40	let pallet_ident = &def.pallet_struct.pallet;
41	let trait_use_gen = &def.trait_use_generics(proc_macro2::Span::call_site());
42
43	let mut where_clauses = vec![&def.config.where_clause];
44	where_clauses.extend(def.extra_constants.iter().map(|d| &d.where_clause));
45	let completed_where_clause = super::merge_where_clauses(&where_clauses);
46
47	let mut config_consts = vec![];
48	for const_ in def.config.consts_metadata.iter() {
49		let ident = &const_.ident;
50		let const_type = &const_.type_;
51		let deprecation_info = match crate::deprecation::get_deprecation(
52			&quote::quote! { #frame_support },
53			&const_.attrs,
54		) {
55			Ok(deprecation) => deprecation,
56			Err(e) => return e.into_compile_error(),
57		};
58
59		// Extracts #[allow] attributes, necessary so that we don't run into compiler warnings
60		let maybe_allow_attrs = extract_or_return_allow_attrs(&const_.attrs);
61
62		config_consts.push(ConstDef {
63			ident: const_.ident.clone(),
64			type_: const_.type_.clone(),
65			doc: const_.doc.clone(),
66			default_byte_impl: quote::quote!(
67				#(#maybe_allow_attrs)*
68				let value = <<T as Config #trait_use_gen>::#ident as
69					#frame_support::traits::Get<#const_type>>::get();
70				#frame_support::__private::codec::Encode::encode(&value)
71			),
72			metadata_name: None,
73			deprecation_info,
74		})
75	}
76
77	let mut extra_consts = vec![];
78	for const_ in def.extra_constants.iter().flat_map(|d| &d.extra_constants) {
79		let ident = &const_.ident;
80		let deprecation_info = match crate::deprecation::get_deprecation(
81			&quote::quote! { #frame_support },
82			&const_.attrs,
83		) {
84			Ok(deprecation) => deprecation,
85			Err(e) => return e.into_compile_error(),
86		};
87		// Extracts #[allow] attributes, necessary so that we don't run into compiler warnings
88		let maybe_allow_attrs = extract_or_return_allow_attrs(&const_.attrs);
89
90		extra_consts.push(ConstDef {
91			ident: const_.ident.clone(),
92			type_: const_.type_.clone(),
93			doc: const_.doc.clone(),
94			default_byte_impl: quote::quote!(
95				#(#maybe_allow_attrs)*
96				let value = <Pallet<#type_use_gen>>::#ident();
97				#frame_support::__private::codec::Encode::encode(&value)
98			),
99			metadata_name: const_.metadata_name.clone(),
100			deprecation_info,
101		})
102	}
103
104	let consts = config_consts.into_iter().chain(extra_consts.into_iter()).map(|const_| {
105		let const_type = &const_.type_;
106		let ident_str = format!("{}", const_.metadata_name.unwrap_or(const_.ident));
107
108		let no_docs = vec![];
109		let doc = if cfg!(feature = "no-metadata-docs") { &no_docs } else { &const_.doc };
110
111		let default_byte_impl = &const_.default_byte_impl;
112		let deprecation_info = &const_.deprecation_info;
113		quote::quote!({
114			#frame_support::__private::metadata_ir::PalletConstantMetadataIR {
115				name: #ident_str,
116				ty: #frame_support::__private::scale_info::meta_type::<#const_type>(),
117				value: { #default_byte_impl },
118				docs: #frame_support::__private::vec![ #( #doc ),* ],
119				deprecation_info: #deprecation_info
120			}
121		})
122	});
123
124	quote::quote!(
125		impl<#type_impl_gen> #pallet_ident<#type_use_gen> #completed_where_clause{
126
127			#[doc(hidden)]
128			pub fn pallet_constants_metadata()
129				-> #frame_support::__private::Vec<#frame_support::__private::metadata_ir::PalletConstantMetadataIR>
130			{
131				#frame_support::__private::vec![ #( #consts ),* ]
132			}
133		}
134	)
135}