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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//! A procedural macro for creating a Builder object for any struct.
//! This crate is meant to be used by the `simple-builder` crate and is not meant for direct consumption.

use proc_macro2::{self, Span, TokenStream};
use quote::quote;
use std::vec::Vec;
use syn::{
    parse_macro_input, Attribute, DeriveInput, Field, Fields, GenericArgument, Ident, Path,
    PathArguments, Type,
};

/// Macro that derives a Builder object for any given struct. E.g. `SomeType` -> `SomeTypeBuilder`.
///
/// Simple-Builder takes ownership of inputs and stores them in an `Option<T>` for each field. Fields
/// that are marked `#[builder(required)]` will be part of the `new()` call so they're guaranteed
/// to be set on the final object.
///
///
/// # Example: Builder on a Simple Object
/// ```
/// # use simple_builder_macro::Builder;
///
/// // Debug, PartialEq, Eq are only for assertions
/// #[derive(Debug, PartialEq, Eq, Builder)]
/// struct Breakfast {
///     #[builder(required)]
///     pub coffee: i64, // coffee is required, and therefore not Option<T>
///     pub toast: Option<i64>,
///     pub eggs: Option<i64>,
///     pub bacon: Option<i64>,
/// }
///
/// pub fn main() {
///     let desired_breakfast = Breakfast {
///         coffee: 1,
///         toast: None,
///         eggs: Some(3),
///         bacon: Some(2),
///     };
///
///     // semantically equivalent to `Breakfast::builder(16)`
///     let mut builder = BreakfastBuilder::new(16);
///
///     let breakfast = builder.eggs(3).bacon(2).build();
///
///     assert_eq!(desired_breakfast, breakfast);
/// }
/// ```
///
/// ## Attributes
/// Builder supports attributes under `#[builder(...)]` on individual fields to carry metadata.
/// At this time, the available attributes are:
/// - required -- marks a field as required, meaning it can be `T` instead of `Option<T>` on the struct
/// and will be an argument to the `StructBuilder::new()` or `Struct::builder()` methods.
#[proc_macro_derive(Builder, attributes(builder))]
pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let ast: DeriveInput = parse_macro_input!(input);
    let (vis, ident, generics) = (&ast.vis, &ast.ident, &ast.generics);
    let builder_ident = Ident::new(&(ident.to_string() + "Builder"), Span::call_site());

    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    let fields: &Fields = match ast.data {
        syn::Data::Struct(ref s) => &s.fields,
        _ => panic!("Can only derive Builder for structs."),
    };

    let named_fields: Vec<&Field> = fields
        .iter()
        .filter(|field| field.ident.is_some())
        .collect();

    let required_fields: Vec<&Field> = named_fields
        .iter()
        .filter_map(|field| {
            if has_attr(field, "required") {
                Some(*field)
            } else {
                None
            }
        })
        .collect();

    let optional_fields: Vec<&Field> = named_fields
        .iter()
        .filter_map(|field| {
            if has_attr(field, "required") {
                None
            } else {
                Some(*field)
            }
        })
        .collect();

    let builder_setter_methods: TokenStream = optional_fields
        .iter()
        .map(|field| {
            let field_ident = &field.ident;
            let field_ty = &field.ty;

            let type_of_option = extract_type_from_option(field_ty);

            quote! {
                pub fn #field_ident(&mut self, #field_ident: #type_of_option) -> &mut Self {
                    self.#field_ident = ::std::option::Option::Some(#field_ident);
                    self
                }
            }
        })
        .collect();

    let required_new_fields: TokenStream = required_fields
        .iter()
        .map(|field| {
            let ident = &field.ident;
            quote! {
                #ident: ::std::option::Option::Some(#ident),
            }
        })
        .collect();

    let empty_new_fields: TokenStream = optional_fields
        .iter()
        .map(|field| {
            let ident = &field.ident;
            quote! {
                #ident: None,
            }
        })
        .collect();

    let builder_required_fields: TokenStream = required_fields
        .iter()
        .map(|field| {
            let ident = &field.ident;
            let ty = &field.ty;
            quote! {
                #ident: ::std::option::Option<#ty>,
            }
        })
        .collect();

    let builder_optional_fields: TokenStream = optional_fields
        .iter()
        .map(|field| {
            let ident = &field.ident;
            let ty = &field.ty;
            quote! {
                #ident: #ty,
            }
        })
        .collect();

    let builder_struct_fields: TokenStream = builder_required_fields
        .into_iter()
        .chain(builder_optional_fields)
        .collect();

    let new_method_params: TokenStream = required_fields
        .iter()
        .map(|field| {
            let (arg, ty) = (&field.ident, &field.ty);
            quote! {
                #arg: #ty,
            }
        })
        .collect();

    let build_fn_struct_fields: TokenStream = named_fields
        .iter()
        .map(|field| {
            let is_required = has_attr(field, "required");

            let ident = &field.ident;

            if is_required {
                // .expect() should be possible only when build is called twice, since these are required private fields set by `new`
                quote! {
                    #ident: self.#ident.take().expect("Option must be Some(T) for required fields. Builder may have already been consumed by calling `build`"),
                }
            } else {
                quote! {
                    #ident: self.#ident.take(),
                }
            }
        })
        .collect();

    let struct_impl = quote! {
        impl #impl_generics #ident #ty_generics #where_clause {
            pub fn builder(#new_method_params) -> #builder_ident #ty_generics {
                #builder_ident {
                    #required_new_fields
                    #empty_new_fields
                }
            }
        }
    };

    let builder_struct = quote! {
        #vis struct #builder_ident #ty_generics #where_clause {
            #builder_struct_fields
        }

        impl #impl_generics #builder_ident #ty_generics #where_clause {

            pub fn new(#new_method_params) -> #builder_ident #ty_generics {
                #builder_ident {
                    #required_new_fields
                    #empty_new_fields
                }
            }

            pub fn build(&mut self) -> #ident #ty_generics {
                #ident {
                    #build_fn_struct_fields
                }
            }

            #builder_setter_methods
        }
    };

    let output = quote! {
        #struct_impl
        #builder_struct
    };

    output.into()
}

fn has_attr(field: &Field, attr: &'static str) -> bool {
    field.attrs.iter().any(|a| has_nested_attr(a, attr))
}

fn has_nested_attr(attr: &Attribute, name: &'static str) -> bool {
    let mut has_attr: bool = false;

    if attr.path().is_ident("builder") {
        attr.parse_nested_meta(|m| {
            if m.path.is_ident(name) {
                has_attr = true;
            }

            Ok(())
        })
        .expect("Parsing nested meta within #[builder(...)] failed.");
    }

    has_attr
}

fn extract_type_from_option(ty: &Type) -> &Type {
    fn path_is_option(path: &Path) -> bool {
        path.leading_colon.is_none()
            && path.segments.len() == 1
            && path.segments.iter().next().unwrap().ident == "Option"
    }

    match ty {
        Type::Path(type_path) if type_path.qself.is_none() && path_is_option(&type_path.path) => {
            let type_params: &PathArguments = &(type_path.path.segments.first().unwrap()).arguments;

            let generic_arg = match type_params {
                PathArguments::AngleBracketed(params) => params.args.first().unwrap(),
                _ => panic!("Could not find generic parameter in Option<...>"),
            };

            match generic_arg {
                GenericArgument::Type(ty) => ty,
                _ => panic!(
                    "Found something other than a type as a generic parameter to Option<...>"
                ),
            }
        }
        _ => panic!(
            "Struct fields must be of type Option<...>, or have #[builder(required)] attribute."
        ),
    }
}