referrerpolicy=no-referrer-when-downgrade

sp_runtime_interface_proc_macro/
lib.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//! This crate provides procedural macros for usage within the context of the Substrate runtime
19//! interface.
20//!
21//! It provides the [`#[runtime_interface]`](attr.runtime_interface.html) attribute macro
22//! for generating the runtime interfaces.
23
24use syn::{
25	parse::{Parse, ParseStream},
26	parse_macro_input, ItemTrait, Result, Token,
27};
28
29mod runtime_interface;
30mod utils;
31
32struct Options {
33	wasm_only: bool,
34	tracing: bool,
35}
36
37impl Options {
38	fn unpack(self) -> (bool, bool) {
39		(self.wasm_only, self.tracing)
40	}
41}
42impl Default for Options {
43	fn default() -> Self {
44		Options { wasm_only: false, tracing: true }
45	}
46}
47
48impl Parse for Options {
49	fn parse(input: ParseStream) -> Result<Self> {
50		let mut res = Self::default();
51		while !input.is_empty() {
52			let lookahead = input.lookahead1();
53			if lookahead.peek(runtime_interface::keywords::wasm_only) {
54				let _ = input.parse::<runtime_interface::keywords::wasm_only>();
55				res.wasm_only = true;
56			} else if lookahead.peek(runtime_interface::keywords::no_tracing) {
57				let _ = input.parse::<runtime_interface::keywords::no_tracing>();
58				res.tracing = false;
59			} else if lookahead.peek(Token![,]) {
60				let _ = input.parse::<Token![,]>();
61			} else {
62				return Err(lookahead.error())
63			}
64		}
65		Ok(res)
66	}
67}
68
69#[proc_macro_attribute]
70pub fn runtime_interface(
71	attrs: proc_macro::TokenStream,
72	input: proc_macro::TokenStream,
73) -> proc_macro::TokenStream {
74	let trait_def = parse_macro_input!(input as ItemTrait);
75	let (wasm_only, tracing) = parse_macro_input!(attrs as Options).unpack();
76
77	runtime_interface::runtime_interface_impl(trait_def, wasm_only, tracing)
78		.unwrap_or_else(|e| e.to_compile_error())
79		.into()
80}