frame_support_procedural/pallet/expand/
config.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;
19use proc_macro2::TokenStream;
20use quote::quote;
21use syn::{parse_quote, Item};
22
23///
24/// * Generate default rust doc
25pub fn expand_config(def: &mut Def) -> TokenStream {
26	let config = &def.config;
27	let config_item = {
28		let item = &mut def.item.content.as_mut().expect("Checked by def parser").1[config.index];
29		if let Item::Trait(item) = item {
30			item
31		} else {
32			unreachable!("Checked by config parser")
33		}
34	};
35
36	config_item.attrs.insert(
37		0,
38		parse_quote!(
39			#[doc = r"
40Configuration trait of this pallet.
41
42The main purpose of this trait is to act as an interface between this pallet and the runtime in
43which it is embedded in. A type, function, or constant in this trait is essentially left to be
44configured by the runtime that includes this pallet.
45
46Consequently, a runtime that wants to include this pallet must implement this trait."
47			]
48		),
49	);
50
51	// we only emit `DefaultConfig` if there are trait items, so an empty `DefaultConfig` is
52	// impossible consequently.
53	match &config.default_sub_trait {
54		Some(default_sub_trait) if default_sub_trait.items.len() > 0 => {
55			let trait_items = &default_sub_trait
56				.items
57				.iter()
58				.map(|item| {
59					if item.1 {
60						if let syn::TraitItem::Type(item) = item.0.clone() {
61							let mut item = item.clone();
62							item.bounds.clear();
63							syn::TraitItem::Type(item)
64						} else {
65							item.0.clone()
66						}
67					} else {
68						item.0.clone()
69					}
70				})
71				.collect::<Vec<_>>();
72
73			let type_param_bounds = if default_sub_trait.has_system {
74				let system = &def.frame_system;
75				quote::quote!(: #system::DefaultConfig)
76			} else {
77				quote::quote!()
78			};
79
80			quote!(
81				/// Based on [`Config`]. Auto-generated by
82				/// [`#[pallet::config(with_default)]`](`frame_support::pallet_macros::config`).
83				/// Can be used in tandem with
84				/// [`#[register_default_config]`](`frame_support::register_default_config`) and
85				/// [`#[derive_impl]`](`frame_support::derive_impl`) to derive test config traits
86				/// based on existing pallet config traits in a safe and developer-friendly way.
87				///
88				/// See [here](`frame_support::pallet_macros::config`) for more information and caveats about
89				/// the auto-generated `DefaultConfig` trait and how it is generated.
90				pub trait DefaultConfig #type_param_bounds {
91					#(#trait_items)*
92				}
93			)
94		},
95		_ => Default::default(),
96	}
97}