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