referrerpolicy=no-referrer-when-downgrade

sp_runtime_interface_proc_macro/runtime_interface/
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
18use crate::utils::generate_runtime_interface_include;
19
20use proc_macro2::{Span, TokenStream};
21
22use syn::{Ident, ItemTrait, Result};
23
24use inflector::Inflector;
25
26use quote::quote;
27
28mod bare_function_interface;
29mod host_function_interface;
30mod trait_decl_impl;
31
32/// Custom keywords supported by the `runtime_interface` attribute.
33pub mod keywords {
34	// Custom keyword `wasm_only` that can be given as attribute to [`runtime_interface`].
35	syn::custom_keyword!(wasm_only);
36	// Disable tracing-macros added to the [`runtime_interface`] by specifying this optional entry
37	syn::custom_keyword!(no_tracing);
38}
39
40/// Implementation of the `runtime_interface` attribute.
41///
42/// It expects the trait definition the attribute was put above and if this should be an wasm only
43/// interface.
44pub fn runtime_interface_impl(
45	trait_def: ItemTrait,
46	is_wasm_only: bool,
47	tracing: bool,
48) -> Result<TokenStream> {
49	let bare_functions = bare_function_interface::generate(&trait_def, is_wasm_only, tracing)?;
50	let crate_include = generate_runtime_interface_include();
51	let mod_name = Ident::new(&trait_def.ident.to_string().to_snake_case(), Span::call_site());
52	let trait_decl_impl = trait_decl_impl::process(&trait_def, is_wasm_only)?;
53	let host_functions = host_function_interface::generate(&trait_def, is_wasm_only)?;
54	let vis = trait_def.vis;
55	let attrs = &trait_def.attrs;
56
57	let res = quote! {
58		#( #attrs )*
59		#vis mod #mod_name {
60			use super::*;
61			#crate_include
62
63			#bare_functions
64
65			#trait_decl_impl
66
67			#host_functions
68		}
69	};
70
71	let res = expander::Expander::new("runtime_interface")
72		.dry(std::env::var("EXPAND_MACROS").is_err())
73		.verbose(true)
74		.write_to_out_dir(res)
75		.expect("Does not fail because of IO in OUT_DIR; qed");
76
77	Ok(res)
78}