gen_attributes_interface_generator/
lib.rs

1extern crate proc_macro;
2
3use proc_macro::TokenStream;
4
5#[proc_macro_attribute]
6pub fn generate_interface(attr: TokenStream, item: TokenStream) -> TokenStream {
7    let item = syn::parse(item).unwrap();
8    let mut is_func = false;
9
10    match item {
11        syn::Item::Fn(ref fun) => {
12            //general function
13            is_func = true;
14            //generics not supported
15            let gene = &fun.sig.generics;
16            assert!(gene.gt_token.is_none(), "Generics not yet supported");
17            assert!(gene.lt_token.is_none(), "Generics not yet supported")
18        }
19        syn::Item::Enum(_) => {}
20        syn::Item::Trait(_) => {}
21        syn::Item::Struct(_) => panic!(
22            "Annotate methods of this struct instead. \
23        To use enable doc comments on this struct use #[generate_interface_doc] macro instead."
24        ),
25        _ => panic!("unsuppoted type"),
26    }
27    let attr = attr.to_string();
28    if !attr.is_empty() {
29        assert_eq!(
30            attr, "constructor",
31            "only constructor attributes are supported for now"
32        );
33        if !is_func {
34            panic!("call constructor on function")
35        }
36    }
37    let y = quote::quote! {
38        #item
39    };
40    y.into()
41}
42
43#[proc_macro_attribute]
44pub fn generate_interface_doc(attr: TokenStream, item: TokenStream) -> TokenStream {
45    let item = syn::parse(item).unwrap();
46    match item {
47        syn::Item::Struct(_) => {}
48        _ => panic!("Use this macro on only struct`"),
49    }
50    assert!(attr.is_empty(), "No attributes allowed yet");
51    let fin = quote::quote! {
52        #item
53    };
54    fin.into()
55}
56
57#[cfg(test)]
58mod tests {
59    #[test]
60    fn it_works() {
61        assert_eq!(2 + 2, 4);
62    }
63}