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