tp_runtime_interface_proc_macro/
lib.rs

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