rcommunity_macros/
lib.rs

1extern crate proc_macro;
2
3use proc_macro2::TokenStream;
4use quote::quote;
5
6#[proc_macro_derive(ID)]
7pub fn id_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
8    derive_id(input.into()).into()
9}
10
11fn derive_id(input: TokenStream) -> TokenStream {
12    // Construct a representation of Rust code as a syntax tree
13    // that we can manipulate
14    let ast: syn::DeriveInput = syn::parse2(input).unwrap();
15
16    let name = &ast.ident;
17    let gen = quote! {
18        impl ::rcommunity_core::ID for #name {
19            fn id(&self) -> &str {
20                &self.0
21            }
22        }
23    };
24    gen
25}
26
27#[proc_macro_derive(UserType)]
28pub fn user_type_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
29    derive_user(input.into()).into()
30}
31
32fn derive_user(input: TokenStream) -> TokenStream {
33    // Construct a representation of Rust code as a syntax tree
34    // that we can manipulate
35    let ast: syn::DeriveInput = syn::parse2(input).unwrap();
36
37    let name = &ast.ident;
38    let gen = quote! {
39        impl ::rcommunity_core::UserType for #name {}
40    };
41    gen
42}
43
44#[proc_macro_derive(ReactionType)]
45pub fn reaction_type_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
46    derive_reaction(input.into()).into()
47}
48
49fn derive_reaction(input: TokenStream) -> TokenStream {
50    // Construct a representation of Rust code as a syntax tree
51    // that we can manipulate
52    let ast: syn::DeriveInput = syn::parse2(input).unwrap();
53
54    let name = &ast.ident;
55    let gen = quote! {
56        impl ::rcommunity_core::ReactionType for #name {}
57    };
58    gen
59}