frame_support_procedural/pallet/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
18//! Implementation for pallet attribute macro.
19//!
20//! General workflow:
21//! 1 - parse all pallet attributes:
22//! This step remove all attributes `#[pallet::*]` from the ItemMod and build the `Def` struct
23//! which holds the ItemMod without `#[pallet::*]` and information given by those attributes
24//! 2 - expand from the parsed information
25//! This step will modify the ItemMod by adding some derive attributes or phantom data variants
26//! to user defined types. And also crate new types and implement block.
27
28mod expand;
29pub(crate) mod parse;
30
31pub use parse::{composite::keyword::CompositeKeyword, Def};
32use syn::spanned::Spanned;
33
34mod keyword {
35 syn::custom_keyword!(dev_mode);
36}
37
38pub fn pallet(
39 attr: proc_macro::TokenStream,
40 item: proc_macro::TokenStream,
41) -> proc_macro::TokenStream {
42 let mut dev_mode = false;
43 if !attr.is_empty() {
44 if let Ok(_) = syn::parse::<keyword::dev_mode>(attr.clone()) {
45 dev_mode = true;
46 } else {
47 let msg = "Invalid pallet macro call: unexpected attribute. Macro call must be \
48 bare, such as `#[frame_support::pallet]` or `#[pallet]`, or must specify the \
49 `dev_mode` attribute, such as `#[frame_support::pallet(dev_mode)]` or \
50 #[pallet(dev_mode)].";
51 let span = proc_macro2::TokenStream::from(attr).span();
52 return syn::Error::new(span, msg).to_compile_error().into()
53 }
54 }
55
56 let item = syn::parse_macro_input!(item as syn::ItemMod);
57 match parse::Def::try_from(item, dev_mode) {
58 Ok(def) => expand::expand(def).into(),
59 Err(e) => e.to_compile_error().into(),
60 }
61}