frame_support_procedural/pallet/expand/doc_only.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 proc_macro2::Span;
19
20use crate::pallet::Def;
21
22pub fn expand_doc_only(def: &mut Def) -> proc_macro2::TokenStream {
23 let dispatchables = if let Some(call_def) = &def.call {
24 let type_impl_generics = def.type_impl_generics(Span::call_site());
25 call_def
26 .methods
27 .iter()
28 .map(|method| {
29 let name = &method.name;
30 let args = &method
31 .args
32 .iter()
33 .map(|(_, arg_name, arg_type)| quote::quote!( #arg_name: #arg_type, ))
34 .collect::<proc_macro2::TokenStream>();
35 let docs = &method.docs;
36
37 let real = format!(" [`Pallet::{}`].", name);
38 quote::quote!(
39 #( #[doc = #docs] )*
40 ///
41 /// # Warning: Doc-Only
42 ///
43 /// This function is an automatically generated, and is doc-only, uncallable
44 /// stub. See the real version in
45 #[ doc = #real ]
46 pub fn #name<#type_impl_generics>(#args) { unreachable!(); }
47 )
48 })
49 .collect::<proc_macro2::TokenStream>()
50 } else {
51 quote::quote!()
52 };
53
54 let storage_types = def
55 .storages
56 .iter()
57 .map(|storage| {
58 let storage_name = &storage.ident;
59 let storage_type_docs = &storage.docs;
60 let real = format!("[`pallet::{}`].", storage_name);
61 quote::quote!(
62 #( #[doc = #storage_type_docs] )*
63 ///
64 /// # Warning: Doc-Only
65 ///
66 /// This type is automatically generated, and is doc-only. See the real version in
67 #[ doc = #real ]
68 pub struct #storage_name();
69 )
70 })
71 .collect::<proc_macro2::TokenStream>();
72
73 quote::quote!(
74 /// Auto-generated docs-only module listing all (public and private) defined storage types
75 /// for this pallet.
76 ///
77 /// # Warning: Doc-Only
78 ///
79 /// Members of this module cannot be used directly and are only provided for documentation
80 /// purposes.
81 ///
82 /// To see the actual storage type, find a struct with the same name at the root of the
83 /// pallet, in the list of [*Type Definitions*](../index.html#types).
84 #[cfg(doc)]
85 pub mod storage_types {
86 use super::*;
87 #storage_types
88 }
89
90 /// Auto-generated docs-only module listing all defined dispatchables for this pallet.
91 ///
92 /// # Warning: Doc-Only
93 ///
94 /// Members of this module cannot be used directly and are only provided for documentation
95 /// purposes. To see the real version of each dispatchable, look for them in [`Pallet`] or
96 /// [`Call`].
97 #[cfg(doc)]
98 pub mod dispatchables {
99 use super::*;
100 #dispatchables
101 }
102 )
103}