referrerpolicy=no-referrer-when-downgrade

frame_support_procedural/pallet/expand/
validate_unsigned.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, COUNTER};
19use proc_macro2::TokenStream;
20use quote::quote;
21use syn::{spanned::Spanned, Ident};
22
23pub fn expand_validate_unsigned(def: &mut Def) -> TokenStream {
24	let count = COUNTER.with(|counter| counter.borrow_mut().inc());
25	let macro_ident =
26		Ident::new(&format!("__is_validate_unsigned_part_defined_{}", count), def.item.span());
27
28	let maybe_compile_error = if def.validate_unsigned.is_none() {
29		quote! {
30			compile_error!(concat!(
31				"`",
32				stringify!($pallet_name),
33				"` does not have #[pallet::validate_unsigned] defined, perhaps you should \
34				remove `ValidateUnsigned` from construct_runtime?",
35			));
36		}
37	} else {
38		TokenStream::new()
39	};
40
41	quote! {
42		#[doc(hidden)]
43		pub mod __substrate_validate_unsigned_check {
44			#[macro_export]
45			#[doc(hidden)]
46			macro_rules! #macro_ident {
47				($pallet_name:ident) => {
48					#maybe_compile_error
49				}
50			}
51
52			#[doc(hidden)]
53			pub use #macro_ident as is_validate_unsigned_part_defined;
54		}
55	}
56}