1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
56// 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.
1718//! 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.
2324use syn::{
25 parse::{Parse, ParseStream},
26 parse_macro_input, ItemTrait, Result, Token,
27};
2829mod runtime_interface;
30mod utils;
3132struct Options {
33 wasm_only: bool,
34 tracing: bool,
35}
3637impl Options {
38fn unpack(self) -> (bool, bool) {
39 (self.wasm_only, self.tracing)
40 }
41}
42impl Default for Options {
43fn default() -> Self {
44 Options { wasm_only: false, tracing: true }
45 }
46}
4748impl Parse for Options {
49fn parse(input: ParseStream) -> Result<Self> {
50let mut res = Self::default();
51while !input.is_empty() {
52let lookahead = input.lookahead1();
53if lookahead.peek(runtime_interface::keywords::wasm_only) {
54let _ = input.parse::<runtime_interface::keywords::wasm_only>();
55 res.wasm_only = true;
56 } else if lookahead.peek(runtime_interface::keywords::no_tracing) {
57let _ = input.parse::<runtime_interface::keywords::no_tracing>();
58 res.tracing = false;
59 } else if lookahead.peek(Token![,]) {
60let _ = input.parse::<Token![,]>();
61 } else {
62return Err(lookahead.error())
63 }
64 }
65Ok(res)
66 }
67}
6869#[proc_macro_attribute]
70pub fn runtime_interface(
71 attrs: proc_macro::TokenStream,
72 input: proc_macro::TokenStream,
73) -> proc_macro::TokenStream {
74let trait_def = parse_macro_input!(input as ItemTrait);
75let (wasm_only, tracing) = parse_macro_input!(attrs as Options).unpack();
7677 runtime_interface::runtime_interface_impl(trait_def, wasm_only, tracing)
78 .unwrap_or_else(|e| e.to_compile_error())
79 .into()
80}