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//! The following macros are provided:
22//!
23//! 1. The [`#[runtime_interface]`](attr.runtime_interface.html) attribute macro for generating the
24//!    runtime interfaces.
25//! 2. The [`PassByCodec`](derive.PassByCodec.html) derive macro for implementing `PassBy` with
26//! `Codec`. 3. The [`PassByEnum`](derive.PassByInner.html) derive macro for implementing `PassBy`
27//! with `Enum`. 4. The [`PassByInner`](derive.PassByInner.html) derive macro for implementing
28//! `PassBy` with `Inner`.
29
30use syn::{
31	parse::{Parse, ParseStream},
32	parse_macro_input, DeriveInput, ItemTrait, Result, Token,
33};
34
35mod pass_by;
36mod runtime_interface;
37mod utils;
38
39struct Options {
40	wasm_only: bool,
41	tracing: bool,
42}
43
44impl Options {
45	fn unpack(self) -> (bool, bool) {
46		(self.wasm_only, self.tracing)
47	}
48}
49impl Default for Options {
50	fn default() -> Self {
51		Options { wasm_only: false, tracing: true }
52	}
53}
54
55impl Parse for Options {
56	fn parse(input: ParseStream) -> Result<Self> {
57		let mut res = Self::default();
58		while !input.is_empty() {
59			let lookahead = input.lookahead1();
60			if lookahead.peek(runtime_interface::keywords::wasm_only) {
61				let _ = input.parse::<runtime_interface::keywords::wasm_only>();
62				res.wasm_only = true;
63			} else if lookahead.peek(runtime_interface::keywords::no_tracing) {
64				let _ = input.parse::<runtime_interface::keywords::no_tracing>();
65				res.tracing = false;
66			} else if lookahead.peek(Token![,]) {
67				let _ = input.parse::<Token![,]>();
68			} else {
69				return Err(lookahead.error())
70			}
71		}
72		Ok(res)
73	}
74}
75
76#[proc_macro_attribute]
77pub fn runtime_interface(
78	attrs: proc_macro::TokenStream,
79	input: proc_macro::TokenStream,
80) -> proc_macro::TokenStream {
81	let trait_def = parse_macro_input!(input as ItemTrait);
82	let (wasm_only, tracing) = parse_macro_input!(attrs as Options).unpack();
83
84	runtime_interface::runtime_interface_impl(trait_def, wasm_only, tracing)
85		.unwrap_or_else(|e| e.to_compile_error())
86		.into()
87}
88
89#[proc_macro_derive(PassByCodec)]
90pub fn pass_by_codec(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
91	let input = parse_macro_input!(input as DeriveInput);
92	pass_by::codec_derive_impl(input)
93		.unwrap_or_else(|e| e.to_compile_error())
94		.into()
95}
96
97#[proc_macro_derive(PassByInner)]
98pub fn pass_by_inner(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
99	let input = parse_macro_input!(input as DeriveInput);
100	pass_by::inner_derive_impl(input)
101		.unwrap_or_else(|e| e.to_compile_error())
102		.into()
103}
104
105#[proc_macro_derive(PassByEnum)]
106pub fn pass_by_enum(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
107	let input = parse_macro_input!(input as DeriveInput);
108	pass_by::enum_derive_impl(input).unwrap_or_else(|e| e.to_compile_error()).into()
109}