macro_class_render/
lib.rs

1extern crate proc_macro;
2extern crate syn;
3#[macro_use]
4extern crate quote;
5
6use proc_macro::TokenStream;
7use syn::{Field, DeriveInput};
8use quote::Tokens;
9
10mod mut_getters;
11mod getters;
12mod setters;
13mod constructor;
14
15#[proc_macro_derive(Constructor, attributes(__constructor))]
16pub fn constructor(input: TokenStream) -> TokenStream {
17    // Construct a string representation of the type definition
18    let s = input.to_string();
19
20    // Parse the string representation
21    let ast = syn::parse_derive_input(&s).expect("Couldn't parse for getters");
22
23    // Build the impl
24    let gen = produce_contructor(&ast, constructor::implement);
25
26    println!("String constructor : {}", gen);
27    // Return the generated impl
28    gen.parse().unwrap()
29}
30
31#[proc_macro_derive(Getters, attributes(get))]
32pub fn getters(input: TokenStream) -> TokenStream {
33    // Construct a string representation of the type definition
34    let s = input.to_string();
35
36    // Parse the string representation
37    let ast = syn::parse_derive_input(&s).expect("Couldn't parse for getters");
38
39    // Build the impl
40    let gen = produce(&ast, getters::implement);
41
42    println!("String Getters : {}", gen);
43    // Return the generated impl
44    gen.parse().unwrap()
45}
46
47#[proc_macro_derive(MutGetters, attributes(get_mut))]
48pub fn mut_getters(input: TokenStream) -> TokenStream {
49    // Construct a string representation of the type definition
50    let s = input.to_string();
51
52    // Parse the string representation
53    let ast = syn::parse_derive_input(&s).expect("Couldn't parse for getters");
54
55    // Build the impl
56    let gen = produce(&ast, mut_getters::implement);
57
58    println!("String MutGetters : {}", gen);
59    // Return the generated impl
60    gen.parse().unwrap()
61}
62
63#[proc_macro_derive(Setters, attributes(set))]
64pub fn setters(input: TokenStream) -> TokenStream {
65    // Construct a string representation of the type definition
66    let s = input.to_string();
67
68    // Parse the string representation
69    let ast = syn::parse_derive_input(&s).expect("Couldn't parse for setters");
70
71    // Build the impl
72    let gen = produce(&ast, setters::implement);
73
74    println!("String Setters : {}", gen);
75    // Return the generated impl
76    gen.parse().unwrap()
77}
78
79fn produce(ast: &DeriveInput, worker: fn(&Field) -> Tokens) -> Tokens {
80    let name = &ast.ident;
81    let generics = &ast.generics;
82    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
83
84    // Is it a struct?
85    if let syn::Body::Struct(syn::VariantData::Struct(ref fields)) = ast.body {
86
87        let generated = fields.iter().map(worker).collect::<Vec<_>>();
88
89        quote! {
90            impl #impl_generics #name #ty_generics #where_clause {
91                #(#generated)*
92            }
93        }
94    } else {
95        // Nope. This is an Enum. We cannot handle these!
96        panic!("#[derive(Getters)] is only defined for structs, not for enums!");
97    }
98}
99
100fn produce_contructor(ast: &DeriveInput, worker: fn(&Field, &String) -> Tokens) -> Tokens {
101    let name = &ast.ident;
102    let generics = &ast.generics;
103    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
104
105    // Is it a struct?
106    if let syn::Body::Struct(syn::VariantData::Struct(ref fields)) = ast.body {
107        let classname = format!("{}", ast.ident);
108        let generated = fields.iter().map(|x| { (worker(x, &classname)) }).collect::<Vec<_>>();
109
110        quote! {
111            impl #impl_generics #name #ty_generics #where_clause {
112                #(#generated)*
113            }
114        }
115    } else {
116        // Nope. This is an Enum. We cannot handle these!
117        panic!("#[derive(Getters)] is only defined for structs, not for enums!");
118    }
119}