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
//! See the docs for "diesel-factories" for more info about this.

#![recursion_limit = "128"]

extern crate proc_macro;
extern crate proc_macro2;

use darling::FromDeriveInput;
use proc_macro2::Span;
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(Factory, attributes(factory))]
pub fn derive_factory(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let ast = parse_macro_input!(input as DeriveInput);
    let options = match Options::from_derive_input(&ast) {
        Ok(options) => options,
        Err(err) => panic!("{}", err),
    };

    let out = DeriveData::new(ast, options);
    let tokens = out.build_derive_output();
    tokens.into()
}

#[derive(FromDeriveInput, Debug)]
#[darling(attributes(factory), forward_attrs(doc, cfg, allow))]
struct Options {
    model: syn::Ident,
    #[darling(default)]
    connection: Option<syn::Path>,
    #[darling(default)]
    id: Option<syn::Ident>,
    table: syn::Path,
}

struct DeriveData {
    input: DeriveInput,
    options: Options,
    tokens: TokenStream,
}

impl DeriveData {
    fn new(input: DeriveInput, options: Options) -> Self {
        Self {
            input,
            options,
            tokens: quote! {},
        }
    }

    fn build_derive_output(mut self) -> TokenStream {
        self.gen_factory_methods_impl();
        self.gen_builder_methods();
        self.gen_set_association_traits();

        self.tokens
    }

    fn gen_factory_methods_impl(&mut self) {
        let factory = self.factory_name();
        let generics = self.factory_generics();
        let model_type = self.model_type();
        let id_type = self.id_type();
        let connection_type = self.connection_type();
        let table_path = self.table_path();
        let values = self.diesel_insert_values();

        self.tokens.extend(quote! {
            impl#generics diesel_factories::Factory for #factory#generics {
                type Model = #model_type;
                type Id = #id_type;
                type Connection = #connection_type;

                fn insert(self, con: &Self::Connection) -> Self::Model {
                    use #table_path::dsl::*;
                    use #table_path as table;

                    use diesel::prelude::*;
                    let values = ( #(#values),* );
                    diesel::insert_into(table::table)
                        .values(values)
                        .get_result::<Self::Model>(con)
                        .unwrap()
                }

                fn id_for_model(model: &Self::Model) -> &Self::Id {
                    &model.id
                }
            }
        });
    }

    fn gen_builder_methods(&mut self) {
        let factory = self.factory_name();
        let generics = self.factory_generics();
        let methods = self.builder_methods();

        self.tokens.extend(quote! {
            impl#generics #factory#generics {
                #(#methods)*
            }
        })
    }

    fn factory_name(&self) -> &syn::Ident {
        &self.input.ident
    }

    fn model_type(&self) -> &syn::Ident {
        &self.options.model
    }

    fn id_type(&self) -> TokenStream {
        self.options
            .id
            .as_ref()
            .map(|inner| quote! { #inner })
            .unwrap_or_else(|| quote! { i32 })
    }

    fn connection_type(&self) -> TokenStream {
        self.options
            .connection
            .as_ref()
            .map(|inner| quote! { #inner })
            .unwrap_or_else(|| quote! { diesel::pg::PgConnection })
    }

    fn table_path(&self) -> &syn::Path {
        &self.options.table
    }

    fn factory_generics(&self) -> &syn::Generics {
        &self.input.generics
    }

    fn struct_fields(&self) -> syn::punctuated::Iter<syn::Field> {
        use syn::{Data, Fields};

        match &self.input.data {
            Data::Union(_) => panic!("Factory can only be derived on structs"),
            Data::Enum(_) => panic!("Factory can only be derived on structs"),
            Data::Struct(data) => match &data.fields {
                Fields::Named(named) => named.named.iter(),
                Fields::Unit => panic!("Factory can only be derived on structs with named fields"),
                Fields::Unnamed(_) => {
                    panic!("Factory can only be derived on structs with named fields")
                }
            },
        }
    }

    fn diesel_insert_values(&self) -> Vec<TokenStream> {
        self.struct_fields()
            .map(|field| self.diesel_insert_value(field))
            .collect()
    }

    fn diesel_insert_value(&self, field: &syn::Field) -> TokenStream {
        let name = field
            .ident
            .as_ref()
            .unwrap_or_else(|| panic!("Factory can only be derived for named fields"));

        if let Some(association) = self.parse_association_type(&field.ty) {
            let foreign_key_field = ident(&format!("{}_id", name));
            if association.is_option {
                quote! {
                    {
                        let value = self.#name.map(|inner| {
                            inner.insert_returning_id(con)
                        });
                        #foreign_key_field.eq(value)
                    }
                }
            } else {
                quote! {
                    #foreign_key_field.eq(self.#name.insert_returning_id(con))
                }
            }
        } else {
            quote! {
                #name.eq(&self.#name)
            }
        }
    }

    fn is_association_field(&self, ty: &syn::Type) -> bool {
        let as_string = self.type_to_string(ty);
        as_string.contains("Association <")
    }

    fn type_to_string(&self, ty: &syn::Type) -> String {
        use quote::ToTokens;

        let mut tokenized = quote! {};
        ty.to_tokens(&mut tokenized);
        tokenized.to_string()
    }

    fn builder_methods(&self) -> Vec<TokenStream> {
        self.struct_fields()
            .filter_map(|field| self.builder_method(field))
            .collect()
    }

    fn builder_method(&self, field: &syn::Field) -> Option<TokenStream> {
        let name = &field.ident;
        let ty = &field.ty;

        if self.is_association_field(&field.ty) {
            None
        } else {
            Some(quote! {
                #[allow(missing_docs, dead_code)]
                pub fn #name<T: Into<#ty>>(mut self, t: T) -> Self {
                    self.#name = t.into();
                    self
                }
            })
        }
    }

    fn gen_set_association_traits(&mut self) {
        let association_traits = self.association_traits();

        self.tokens.extend(quote! {
            #(#association_traits)*
        });
    }

    fn association_traits(&self) -> Vec<TokenStream> {
        self.struct_fields()
            .filter_map(|field| self.association_trait(field))
            .collect()
    }

    fn association_trait(&self, field: &syn::Field) -> Option<TokenStream> {
        use heck::CamelCase;

        if self.is_association_field(&field.ty) {
            let factory = self.factory_name();
            let field_name = field.ident.as_ref().expect("field without name");
            let camel_field_name = field_name.to_string().to_camel_case();

            let association = self.parse_association_type(&field.ty).unwrap_or_else(|| {
                use std::fmt::Write;
                let mut s = String::new();
                writeln!(
                    s,
                    "Invalid association attribute. Must be on one of the following forms"
                )
                .unwrap();
                writeln!(s).unwrap();
                writeln!(s, "Association<'a, Model, Factory>").unwrap();
                writeln!(s, "Option<Association<'a, Model, Factory>>").unwrap();
                writeln!(s, "Association<'a, Model, Factory<'a>>").unwrap();
                writeln!(s, "Option<Association<'a, Model, Factory<'a>>>").unwrap();
                writeln!(s).unwrap();
                writeln!(s, "Got\n{}", self.type_to_string(&field.ty)).unwrap();
                panic!("{}", s);
            });

            let model = association.model;
            let other_factory = association.factory;

            let other_factory_without_lifetime =
                self.type_to_string(&other_factory).replace(" < 'a >", "");
            let trait_name = ident(&format!(
                "Set{}On{}For{}",
                other_factory_without_lifetime, factory, camel_field_name
            ));

            let model_impl = if association.is_option {
                quote! {
                    impl<'a> #trait_name<Option<&'a #model>> for #factory<'a> {
                        fn #field_name(mut self, t: Option<&'a #model>) -> Self {
                            self.#field_name = t.map(|k| diesel_factories::Association::new_model(k));
                            self
                        }
                    }
                }
            } else {
                quote! {
                    impl<'a> #trait_name<&'a #model> for #factory<'a> {
                        fn #field_name(mut self, t: &'a #model) -> Self {
                            self.#field_name = diesel_factories::Association::new_model(t);
                            self
                        }
                    }
                }
            };

            let factory_impl = if association.is_option {
                quote! {
                    impl<'a> #trait_name<Option<#other_factory>> for #factory<'a> {
                        fn #field_name(mut self, t: Option<#other_factory>) -> Self {
                            self.#field_name = t.map(|k| diesel_factories::Association::new_factory(k));
                            self
                        }
                    }
                }
            } else {
                quote! {
                    impl<'a> #trait_name<#other_factory> for #factory<'a> {
                        fn #field_name(mut self, t: #other_factory) -> Self {
                            self.#field_name = diesel_factories::Association::new_factory(t);
                            self
                        }
                    }
                }
            };

            Some(quote! {
                #[allow(missing_docs, dead_code)]
                pub trait #trait_name<T> {
                    fn #field_name(self, t: T) -> Self;
                }

                #model_impl

                #factory_impl
            })
        } else {
            None
        }
    }

    fn parse_association_type(&self, ty: &syn::Type) -> Option<Association> {
        use regex::Regex;

        let re = Regex::new(
            r"(Option < )?Association < 'a , (?P<model>[^ ]+) , (?P<factory>[^ ]+( < 'a >)?) >( >)?",
        )
        .unwrap();
        let as_string = self.type_to_string(ty);
        let caps = re.captures(&as_string)?;

        let model = &caps["model"];
        let model = syn::parse_str::<syn::Type>(model).unwrap_or_else(|e| {
            panic!("{}", e);
        });

        let factory = &caps["factory"];
        let factory = syn::parse_str::<syn::Type>(factory).unwrap_or_else(|e| {
            panic!("{}", e);
        });

        let is_option = as_string.contains("Option < ");

        Some(Association {
            is_option,
            model,
            factory,
        })
    }
}

fn ident(s: &str) -> syn::Ident {
    syn::Ident::new(s, Span::call_site())
}

struct Association {
    is_option: bool,
    model: syn::Type,
    factory: syn::Type,
}