1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! # rust_transit_derive
//! 
//! Derive macros to easily define event types and consumer groups
extern crate proc_macro;

use crate::proc_macro::TokenStream;
use quote::quote;
use syn;

/// EventType code is the actual type name
#[proc_macro_derive(EventType)]
pub fn event_type_derive(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();

    impl_event_type(&ast)
}

fn impl_event_type(ast: &syn::DeriveInput) -> TokenStream {
    let name = &ast.ident;
    let gen = quote! {
        impl EventType for #name {
            fn code() -> String {
                stringify!(#name).to_owned()
            }
        }
    };
    gen.into()
}

/// Consumer group is the actual type name
#[proc_macro_derive(ConsumerGroup)]
pub fn consumer_group_derive(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();

    impl_consumer_group(&ast)
}

fn impl_consumer_group(ast: &syn::DeriveInput) -> TokenStream {
    let name = &ast.ident;
    let gen = quote! {
        impl ConsumerGroup for #name {
            fn group() -> String {
                stringify!(#name).to_owned()
            }
        }
    };
    gen.into()
}