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::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		config_consts.push(ConstDef {
59			ident: const_.ident.clone(),
60			type_: const_.type_.clone(),
61			doc: const_.doc.clone(),
62			default_byte_impl: quote::quote!(
63				let value = <<T as Config #trait_use_gen>::#ident as
64					#frame_support::traits::Get<#const_type>>::get();
65				#frame_support::__private::codec::Encode::encode(&value)
66			),
67			metadata_name: None,
68			deprecation_info,
69		})
70	}
71
72	let mut extra_consts = vec![];
73	for const_ in def.extra_constants.iter().flat_map(|d| &d.extra_constants) {
74		let ident = &const_.ident;
75		let deprecation_info = match crate::deprecation::get_deprecation(
76			&quote::quote! { #frame_support },
77			&const_.attrs,
78		) {
79			Ok(deprecation) => deprecation,
80			Err(e) => return e.into_compile_error(),
81		};
82
83		extra_consts.push(ConstDef {
84			ident: const_.ident.clone(),
85			type_: const_.type_.clone(),
86			doc: const_.doc.clone(),
87			default_byte_impl: quote::quote!(
88				let value = <Pallet<#type_use_gen>>::#ident();
89				#frame_support::__private::codec::Encode::encode(&value)
90			),
91			metadata_name: const_.metadata_name.clone(),
92			deprecation_info,
93		})
94	}
95
96	let consts = config_consts.into_iter().chain(extra_consts.into_iter()).map(|const_| {
97		let const_type = &const_.type_;
98		let ident_str = format!("{}", const_.metadata_name.unwrap_or(const_.ident));
99
100		let no_docs = vec![];
101		let doc = if cfg!(feature = "no-metadata-docs") { &no_docs } else { &const_.doc };
102
103		let default_byte_impl = &const_.default_byte_impl;
104		let deprecation_info = &const_.deprecation_info;
105		quote::quote!({
106			#frame_support::__private::metadata_ir::PalletConstantMetadataIR {
107				name: #ident_str,
108				ty: #frame_support::__private::scale_info::meta_type::<#const_type>(),
109				value: { #default_byte_impl },
110				docs: #frame_support::__private::vec![ #( #doc ),* ],
111				deprecation_info: #deprecation_info
112			}
113		})
114	});
115
116	quote::quote!(
117		impl<#type_impl_gen> #pallet_ident<#type_use_gen> #completed_where_clause{
118
119			#[doc(hidden)]
120			pub fn pallet_constants_metadata()
121				-> #frame_support::__private::Vec<#frame_support::__private::metadata_ir::PalletConstantMetadataIR>
122			{
123				#frame_support::__private::vec![ #( #consts ),* ]
124			}
125		}
126	)
127}