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
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
extern crate proc_macro;
#[macro_use]
extern crate quote;
extern crate rand;
extern crate syn;

use std::iter::repeat;
use proc_macro::TokenStream;

#[proc_macro_derive(Builder)]
pub fn code_gen_builder(input: TokenStream) -> TokenStream {
    let s: String = input.to_string();
    let ast: syn::DeriveInput = syn::parse_derive_input(&s).unwrap();
    let gen: quote::Tokens = impl_builder(&ast);
    gen.parse().unwrap()
}

fn repeat_n(n: usize) -> std::iter::Take<std::iter::Repeat<()>> {
    repeat(()).take(n)
}

fn impl_builder(ast: &syn::DeriveInput) -> quote::Tokens {
    let name = &ast.ident;
    let builder_name = syn::Ident::new(format!("{}Builder", name));

    let mod_name = syn::Ident::new(format!("__{}_internal", name).to_lowercase());

    let fields: &Vec<syn::Field> = match &ast.body {
        &syn::Body::Enum(_) => panic!("Cannot derive Builder for enums"),
        &syn::Body::Struct(ref data) => match data {
            &syn::VariantData::Tuple(_) => panic!("Canont derive Builder for Tuple structs"),
            &syn::VariantData::Unit => panic!("Cannot derive Builder for unit structs"),
            &syn::VariantData::Struct(ref fields) => fields,
        },
    };

    let number_of_fields = fields.len();

    let withouts = repeat_n(number_of_fields)
        .map(|_| {
            quote! { #mod_name::Without }
        })
        .collect::<Vec<_>>();

    let field_inits = fields
        .iter()
        .map(|field| {
            //
            let name = field.clone().ident.unwrap();
            quote! { #name: #mod_name::Without }
        })
        .collect::<Vec<_>>();

    let generic_builder_fields = repeat_n(number_of_fields)
        .map(|_| {
            let r = rand::random::<u64>();
            syn::Ident::new(format!("{}Field{}", builder_name, r))
        })
        .collect::<Vec<_>>();

    let builder_fields = fields
        .iter()
        .zip(&generic_builder_fields)
        .map(|(field, ty)| {
            let name = field.clone().ident.unwrap();
            quote! { #name: #ty }
        })
        .collect::<Vec<_>>();

    let done_cons_fields = fields
        .iter()
        .map(|field| {
            let name = field.clone().ident.unwrap();
            quote! { #name: self.#name.item }
        })
        .collect::<Vec<_>>();

    let done_withs = fields
        .iter()
        .map(|field| {
            let ty = field.clone().ty;
            quote! { #mod_name::With<#ty> }
        })
        .collect::<Vec<_>>();

    let fns = fields
        .iter()
        .map(|field| {
            let name = field.clone().ident.unwrap();
            let ty = field.clone().ty;

            let with_withouts = fields
                .iter()
                .enumerate()
                .map(|(idx, other_field)| {
                    if field == other_field {
                        quote! { #mod_name::With<#ty> }
                    } else {
                        let generic_type_name = generic_builder_fields.get(idx).unwrap();
                        quote!{ #generic_type_name }
                    }
                })
                .collect::<Vec<_>>();

            let assigns = fields
                .iter()
                .map(|other_field| {
                    if field == other_field {
                        quote! { #name: #mod_name::With { item: #name.into() } }
                    } else {
                        let name = other_field.clone().ident.unwrap();
                        quote!{ #name: self.#name }
                    }
                })
                .collect::<Vec<_>>();

            quote! {
                #[inline]
                #[doc(hidden)]
                pub fn #name<T: Into<#ty>>(self, #name: T) -> #builder_name<#(#with_withouts),*> {
                    #builder_name {
                        #(#assigns),*
                    }
                }
            }
        })
        .collect::<Vec<_>>();

    let generic_builder_fields = quote! { #(#generic_builder_fields),* };
    let builder_fields = quote! { #(#builder_fields),* };

    quote! {
        mod #mod_name {
            #[derive(Debug, Copy, Clone)]
            #[doc(hidden)]
            pub struct Without;

            #[derive(Debug)]
            #[doc(hidden)]
            pub struct With<T> { pub item: T }
        }

        impl #name {
            #[inline]
            #[doc(hidden)]
            pub fn build() -> #builder_name<#(#withouts),*> {
                #builder_name {
                    #(#field_inits),*
                }
            }
        }

        #[derive(Debug)]
        #[doc(hidden)]
        pub struct #builder_name<#generic_builder_fields> {
            #builder_fields
        }

        impl<#generic_builder_fields> #builder_name<#generic_builder_fields> {
            #(#fns)*
        }

        impl #builder_name<#(#done_withs),*> {
            #[inline]
            #[doc(hidden)]
            pub fn done(self) -> #name {
                #name { #(#done_cons_fields),* }
            }
        }
    }
}