Skip to main content

rings_derive/
lib.rs

1//! Procedural macros shared by Rings crates.
2
3extern crate proc_macro;
4#[macro_use]
5extern crate quote;
6use syn::parse_macro_input;
7use syn::DeriveInput;
8mod derives;
9use proc_macro::TokenStream;
10
11/// If the feature is not "wasm", the macro does nothing; otherwise, it calls wasm_bindgen.
12/// wasm_export does not work for Js Class. To export a class to js,
13/// you should use wasm_bindgen or __wasm_bindgen_class_marker.
14/// ref: <https://docs.rs/wasm-bindgen-macro/0.2.86/src/wasm_bindgen_macro/lib.rs.html#51>
15#[proc_macro_attribute]
16pub fn wasm_export(attr: TokenStream, input: TokenStream) -> TokenStream {
17    if !attr.is_empty() {
18        return syn::Error::new(
19            proc_macro2::Span::call_site(),
20            "wasm_export does not support attribute arguments",
21        )
22        .to_compile_error()
23        .into();
24    }
25    #[cfg(feature = "wasm")]
26    {
27        let input: proc_macro2::TokenStream = input.into();
28        quote! {
29            #[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
30            #input
31        }
32        .into()
33    }
34
35    #[cfg(not(feature = "wasm"))]
36    return input;
37}
38
39/// Derive connection classification implementations.
40#[proc_macro_derive(JudgeConnection)]
41pub fn impl_judege_connection(input: TokenStream) -> TokenStream {
42    let ast = parse_macro_input!(input as DeriveInput);
43    crate::derives::impl_judge_connection_traits(&ast).into()
44}