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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//! My result of the builder exercise in the excellent [proc-macro-workshop](https://github.com/dtolnay/proc-macro-workshop) by David Tolnay.
//!
//! I strongly recommend checking out this workshop to anyone who would like to get started with procedural macros in rust.
//!
//! This is a personal learning project, do **not** use in production and consider [rust-derive-builder](https://crates.io/crates/derive_builder) instead.

extern crate proc_macro;
extern crate quote;
extern crate syn;

use quote::{format_ident, quote};

/// Derives a `builder()` method that can be used to construct an instance of a `struct`.
///
/// The library is implemented using a procedural macro.
/// The builder is a fluent-design pattern that simplifies the construction of a struct instance.
/// In particular, we support the following features
/// - No uninitialized members: checks the presence of all variables and panics in case of missing variables.
/// - Members of type `Option` do not have to be specified and default to `None`
/// - Repeated arguments: Members of type `Vec` can be constructed using a sequence of individual elements.
///
/// # Examples:
/// ## Basic usage
/// ```
/// use robma_builder::Builder;
///
/// #[derive(Builder, Debug, PartialEq)]
/// pub struct Command {
///     executable: String,
///     args: Vec<String>,
///     env: Vec<String>,
///     current_dir: String,
/// }
///
/// let command = Command::builder()
///        .executable("cargo".into())
///        .args(vec!["build".into(), "--release".into()])
///        .env(vec![])
///        .current_dir("..".into())
///        .build()
///        .expect("missing arguments");
///
/// // The result of the above is equivalent to:
/// let command_equivalent = Command{
///         executable: "cargo".into(),
///         args: vec!["build".into(), "--release".into()],
///         env: vec![],
///         current_dir: "..".into(),
/// };
/// assert_eq!(command, command_equivalent);
/// ```
///
/// ## Optional members
/// Optional members can be ommitted in the `builder` and initialized with `None`
/// ```
/// use robma_builder::Builder;
///
/// #[derive(Builder)]
/// pub struct Command {
///     current_dir: Option<String>,
/// }
///
/// let command = Command::builder()
///     .build()
///     .expect("missing arguments");
///
/// // Unspecified option members will be initialized with None:
/// assert!(command.current_dir.is_none());
/// ```
///
/// ## Repeated arguments
/// Members of type `Vec` can be initialized by a sequence of elements.
/// Use the attribute `#[builder(each = "...")]` to declare the name of the function.
/// ```
/// use robma_builder::Builder;
///
/// #[derive(Builder)]
/// pub struct Command {
///     #[builder(each = "arg")]
///     args: Vec<String>,
/// }
///
/// let command = Command::builder()
///     .arg("build".to_owned())
///     .arg("--release".to_owned())
///     .build()
///     .expect("missing arguments");
///
/// // args contains the elements of each call to `arg()`
/// assert_eq!(command.args, vec!["build", "--release"]);
/// ```
#[proc_macro_derive(Builder, attributes(builder))]
pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let derive_input: syn::DeriveInput = syn::parse_macro_input!(input);

    let name = &derive_input.ident;
    let builder_name = format_ident!("{}Builder", name);
    let fields = get_fields(&derive_input);

    if let Some(error_messages) = check_fields_for_errors(&fields) {
        return error_messages.into();
    }

    let struct_body = fields
        .iter()
        .map(|x| {
            let name = x.name;
            let field_type = x.field_type;
            if x.repeated_name
                .as_ref()
                .expect("Unexpected repeated_name error")
                .is_none()
            {
                quote! {
                    #name: std::option::Option<#field_type>
                }
            } else {
                quote! {
                    #name: #field_type
                }
            }
        })
        .collect::<Vec<proc_macro2::TokenStream>>();

    let builder_body = fields
        .iter()
        .map(|x| {
            let name = x.name;
            let initial_value = if x
                .repeated_name
                .as_ref()
                .expect("Unexpected repeated_name error")
                .is_some()
            {
                quote! {vec![]}
            } else {
                quote! {None}
            };
            quote! {
                #name: #initial_value
            }
        })
        .collect::<Vec<proc_macro2::TokenStream>>();

    let setter_functions = derive_setter_functions(&fields);

    let build_function = derive_build_function(name, &fields);

    let out = quote! {
        #[derive(Debug, PartialEq)]
        struct #builder_name{
            #(#struct_body),*
        }

        impl #name {
            fn builder() -> #builder_name {
                #builder_name {
                    #(#builder_body),*
                }
            }
        }

        impl #builder_name {
            #setter_functions

            #build_function
        }

    };

    out.into()
}

struct Field<'f> {
    name: &'f syn::Ident,
    field_type: &'f syn::Type,
    optional: bool,
    repeated_name: Result<Option<syn::Ident>, syn::Error>,
}

fn get_fields<'f>(derive_input: &'f syn::DeriveInput) -> Vec<Field<'f>> {
    let fields = {
        if let syn::Data::Struct(data_struct) = &derive_input.data {
            if let syn::Fields::Named(fields) = &data_struct.fields {
                &fields.named
            } else {
                unimplemented!()
            }
        } else {
            unimplemented!()
        }
    };

    fields
        .iter()
        .map(|x| {
            if is_option(&x.ty) {
                Field {
                    name: x.ident.as_ref().expect("Expected identifier"),
                    field_type: get_angle_bracket_arg(&x.ty).expect("Expected Option Type"),
                    optional: true,
                    repeated_name: get_repeated_name(&x.attrs),
                }
            } else {
                Field {
                    name: x.ident.as_ref().expect("Expected identifier"),
                    field_type: &x.ty,
                    optional: false,
                    repeated_name: get_repeated_name(&x.attrs),
                }
            }
        })
        .collect()
}

fn check_fields_for_errors(fields: &[Field]) -> Option<proc_macro2::TokenStream> {
    let error_messages: Vec<proc_macro2::TokenStream> = fields
        .iter()
        .filter(|field| field.repeated_name.is_err())
        .map(|field| {
            field
                .repeated_name
                .as_ref()
                .expect_err("Expected repeated_name error")
                .to_compile_error()
        })
        .collect();
    if error_messages.is_empty() {
        None
    } else {
        Some(quote! {
            #(#error_messages)*
        })
    }
}

fn get_repeated_name(attrs: &[syn::Attribute]) -> Result<Option<syn::Ident>, syn::Error> {
    for attr in attrs.iter() {
        for segment in attr.path.segments.iter() {
            if segment.ident == "builder" {
                for token in attr.tokens.clone().into_iter() {
                    if let proc_macro2::TokenTree::Group(group) = token {
                        if group.delimiter() == proc_macro2::Delimiter::Parenthesis {
                            let mut stream = group.stream().into_iter();

                            if let (
                                Some(proc_macro2::TokenTree::Ident(ident)),
                                Some(proc_macro2::TokenTree::Punct(equals)),
                                Some(proc_macro2::TokenTree::Literal(repeated_name)),
                            ) = (stream.next(), stream.next(), stream.next())
                            {
                                if ident == "each" && equals.as_char() == '=' {
                                    if let syn::Lit::Str(repeated_name) =
                                        syn::Lit::new(repeated_name)
                                    {
                                        return Ok(Some(format_ident!(
                                            "{}",
                                            repeated_name.value()
                                        )));
                                    }
                                } else {
                                    return Err(syn::Error::new(
                                        group.span(),
                                        "expected `each = '...'`",
                                    ));
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    Ok(None)
}

fn is_option(t: &syn::Type) -> bool {
    match t {
        syn::Type::Path(t) => match t.path.segments.first() {
            Some(t) => t.ident == "Option",
            _ => false,
        },
        _ => false,
    }
}

fn get_angle_bracket_arg(t: &syn::Type) -> Option<&syn::Type> {
    if let syn::Type::Path(t) = t {
        if let Some(t) = t.path.segments.first() {
            if let syn::PathArguments::AngleBracketed(t) = &t.arguments {
                if let Some(t) = t.args.first() {
                    if let syn::GenericArgument::Type(t) = t {
                        return Some(t);
                    }
                }
            }
        }
    }
    None
}

fn derive_setter_functions(fields: &[Field]) -> proc_macro2::TokenStream {
    let setter_functions = fields
        .iter()
        .map(|field| {
            let name = field.name;
            if let Some(repeated_name) = field
                .repeated_name
                .as_ref()
                .expect("Unexpected repeated_name error")
                .as_ref()
            {
                let repeated_type =
                    get_angle_bracket_arg(field.field_type).expect("Expected vector type");
                quote! {
                    fn #repeated_name(&mut self, x: #repeated_type) -> &mut Self{
                        self.#name.push(x);
                        self
                    }
                }
            } else {
                let field_type = field.field_type;
                quote! {
                    fn #name(&mut self, x: #field_type) -> &mut Self{
                        self.#name = std::option::Option::Some(x);
                        self
                    }
                }
            }
        })
        .collect::<Vec<proc_macro2::TokenStream>>();

    quote! {
        #(#setter_functions)*
    }
}

fn derive_build_function(name: &syn::Ident, fields: &[Field]) -> proc_macro2::TokenStream {
    let field_assignments: Vec<proc_macro2::TokenStream> = fields
        .iter()
        .map(|field| {
            let field_name = field.name;
            let field_error_msg = format!("Field '{}' not initialized.", field_name);
            if !field.optional
                && field
                    .repeated_name
                    .as_ref()
                    .expect("Unexpected repeated_name error")
                    .is_none()
            {
                quote! {
                    #field_name: self.#field_name.take().ok_or(#field_error_msg)?
                }
            } else if field
                .repeated_name
                .as_ref()
                .expect("Unexpected repeated_name error")
                .is_some()
            {
                quote! {
                    #field_name: self.#field_name.clone()
                }
            } else {
                quote! {
                    #field_name: self.#field_name.take()
                }
            }
        })
        .collect();

    quote! {
        fn build(&mut self) -> std::result::Result<#name, std::boxed::Box<dyn std::error::Error>> {
            std::result::Result::Ok(#name {
                #(#field_assignments),*
            })
        }
    }
}