sp_runtime_interface_proc_macro/
lib.rs1use 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}