referrerpolicy=no-referrer-when-downgrade

frame_support_procedural/pallet/expand/
mod.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
18mod call;
19mod composite;
20mod config;
21mod constants;
22mod doc_only;
23mod documentation;
24mod error;
25mod event;
26mod genesis_build;
27mod genesis_config;
28mod hooks;
29mod inherent;
30mod instances;
31mod origin;
32mod pallet_struct;
33mod storage;
34mod tasks;
35mod tt_default_parts;
36mod type_value;
37mod validate_unsigned;
38mod view_functions;
39mod warnings;
40
41use crate::pallet::Def;
42use quote::ToTokens;
43
44/// Merge where clause together, `where` token span is taken from the first not none one.
45pub fn merge_where_clauses(clauses: &[&Option<syn::WhereClause>]) -> Option<syn::WhereClause> {
46	let mut clauses = clauses.iter().filter_map(|f| f.as_ref());
47	let mut res = clauses.next()?.clone();
48	for other in clauses {
49		res.predicates.extend(other.predicates.iter().cloned())
50	}
51	Some(res)
52}
53
54/// Expand definition, in particular:
55/// * add some bounds and variants to type defined,
56/// * create some new types,
57/// * impl stuff on them.
58pub fn expand(mut def: Def) -> proc_macro2::TokenStream {
59	// Remove the `pallet_doc` attribute first.
60	let metadata_docs = documentation::expand_documentation(&mut def);
61	let constants = constants::expand_constants(&mut def);
62	let pallet_struct = pallet_struct::expand_pallet_struct(&mut def);
63	let config = config::expand_config(&mut def);
64	let associated_types = config::expand_config_metadata(&def);
65	let call = call::expand_call(&mut def);
66	let tasks = tasks::expand_tasks(&mut def);
67	let error = error::expand_error(&mut def);
68	let event = event::expand_event(&mut def);
69	let storages = storage::expand_storages(&mut def);
70	let view_functions = view_functions::expand_view_functions(&def);
71	let inherents = inherent::expand_inherents(&mut def);
72	let instances = instances::expand_instances(&mut def);
73	let hooks = hooks::expand_hooks(&mut def);
74	let genesis_build = genesis_build::expand_genesis_build(&mut def);
75	let genesis_config = genesis_config::expand_genesis_config(&mut def);
76	let type_values = type_value::expand_type_values(&mut def);
77	let origin = origin::expand_origin(&mut def);
78	let validate_unsigned = validate_unsigned::expand_validate_unsigned(&mut def);
79	let tt_default_parts = tt_default_parts::expand_tt_default_parts(&mut def);
80	let doc_only = doc_only::expand_doc_only(&mut def);
81	let composites = composite::expand_composites(&mut def);
82
83	let warnings = def.config.warnings;
84
85	def.item.attrs.insert(
86		0,
87		syn::parse_quote!(
88			#[doc = r"The `pallet` module in each FRAME pallet hosts the most important items needed
89to construct this pallet.
90
91The main components of this pallet are:
92- [`Pallet`], which implements all of the dispatchable extrinsics of the pallet, among
93other public functions.
94	- The subset of the functions that are dispatchable can be identified either in the
95	[`dispatchables`] module or in the [`Call`] enum.
96- [`storage_types`], which contains the list of all types that are representing a
97storage item. Otherwise, all storage items are listed among [*Type Definitions*](#types).
98- [`Config`], which contains the configuration trait of this pallet.
99- [`Event`] and [`Error`], which are listed among the [*Enums*](#enums).
100		"]
101		),
102	);
103
104	let new_items = quote::quote!(
105		#(
106			#warnings
107		)*
108		#metadata_docs
109		#constants
110		#pallet_struct
111		#config
112		#associated_types
113		#call
114		#tasks
115		#error
116		#event
117		#storages
118		#view_functions
119		#inherents
120		#instances
121		#hooks
122		#genesis_build
123		#genesis_config
124		#type_values
125		#origin
126		#validate_unsigned
127		#tt_default_parts
128		#doc_only
129		#composites
130	);
131
132	let item = &mut def.item.content.as_mut().expect("This is checked by parsing").1;
133	item.push(syn::Item::Verbatim(new_items));
134
135	def.item.into_token_stream()
136}