diesel 1.1.1

A safe, extensible ORM and Query builder
Documentation
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/// Parses the body of a struct field, extracting the relevant information the
/// relevant information that we care about. This macro can handle either named
/// structs or tuple structs. It does not handle unit structs.
///
/// When calling this macro from the outside, it takes three arguments. The
/// first is a single token tree of passthrough information, which will be given
/// to the callback unchanged. The second is the name of the macro to call with
/// the parsed field. The third is the *entire* body of the struct, including
/// either the curly braces or parens.
///
/// If a tuple struct is given, all fields *must* be annotated with
/// `#[column_name(name)]`. Due to the nature of non-procedural macros, we
/// cannot give a helpful error message in this case.
///
/// The callback will be called with the given headers, and a list of fields
/// in record form with the following properties:
///
/// - `field_name` is the name of the field on the struct. This will not be
///   present if the struct is a tuple struct.
/// - `column_name` is the column the field corresponds to. This will either be
///   the value of a `#[column_name]` attribute on the field, or the field name
///   if not present.
/// - `field_type` is the type of the field on the struct.
/// - `field_kind` Will be either `regular` or `option` depending on whether
///   the type of the field was an option or not.
///
/// # Example
///
/// If this macro is called with:
///
/// ```ignore
/// __diesel_parse_struct_body {
///     (my original arguments),
///     callback = my_macro,
///     body = {
///         pub foo: i32,
///         bar: Option<i32>,
///         #[column_name(other)]
///         baz: String,
///     }
/// }
/// ```
///
/// Then the resulting expansion will be:
///
/// ```ignore
/// my_macro! {
///     (my original arguments),
///     fields = [{
///         field_name: foo,
///         column_name: foo,
///         field_ty: i32,
///         field_kind: regular,
///         inner_field_ty: i32,
///     }, {
///         field_name: bar,
///         column_name: bar,
///         field_ty: Option<i32>,
///         field_kind: option,
///         inner_field_ty: i32,
///     }, {
///         field_name: baz,
///         column_name: other,
///         field_ty: String,
///         field_kind: regular,
///         inner_field_ty: String,
///     }],
/// }
#[macro_export]
#[doc(hidden)]
macro_rules! __diesel_parse_struct_body {
    // Entry point for named structs
    (
        $headers:tt,
        callback = $callback:ident,
        body = {$($body:tt)*},
    ) => {
        __diesel_parse_struct_body! {
            $headers,
            callback = $callback,
            fields = [],
            body = ($($body)*,),
        }
    };

    // Entry point for tuple structs
    (
        $headers:tt,
        callback = $callback:ident,
        body = ($($body:tt)*),
    ) => {
        __diesel_parse_struct_body! {
            $headers,
            callback = $callback,
            fields = [],
            body = ($($body)*,),
        }
    };

    // FIXME: Replace with `vis` specifier if relevant RFC lands
    // First, strip `pub` if it exists
    (
        $headers:tt,
        callback = $callback:ident,
        fields = $fields:tt,
        body = (
            $(#$meta:tt)*
            pub $($tail:tt)*),
    ) => {
        __diesel_parse_struct_body! {
            $headers,
            callback = $callback,
            fields = $fields,
            body = ($(#$meta)* $($tail)*),
        }
    };

    // Since we blindly add a comma to the end of the body, we might have a
    // double trailing comma.  If it's the only token left, that's what
    // happened. Strip it.
    (
        $headers:tt,
        callback = $callback:ident,
        fields = $fields:tt,
        body = (,),
    ) => {
        __diesel_parse_struct_body! {
            $headers,
            callback = $callback,
            fields = $fields,
            body = (),
        }
    };

    // When we find #[column_name] followed by an option type, handle the
    // tuple struct field
    (
        $headers:tt,
        callback = $callback:ident,
        fields = [$($fields:tt)*],
        body = (
            #[column_name($column_name:ident)]
            Option<$field_ty:ty> , $($tail:tt)*),
    ) => {
        __diesel_parse_struct_body! {
            $headers,
            callback = $callback,
            fields = [$($fields)* {
                column_name: $column_name,
                field_ty: Option<$field_ty>,
                field_kind: option,
                inner_field_ty: $field_ty,
            }],
            body = ($($tail)*),
        }
    };

    // When we find #[column_name] followed by a type, handle the tuple struct
    // field
    (
        $headers:tt,
        callback = $callback:ident,
        fields = [$($fields:tt)*],
        body = (
            #[column_name($column_name:ident)]
            $field_ty:ty , $($tail:tt)*),
    ) => {
        __diesel_parse_struct_body! {
            $headers,
            callback = $callback,
            fields = [$($fields)* {
                column_name: $column_name,
                field_ty: $field_ty,
                field_kind: regular,
                inner_field_ty: $field_ty,
            }],
            body = ($($tail)*),
        }
    };

    // When we find #[column_name] followed by a named field, handle it
    (
        $headers:tt,
        callback = $callback:ident,
        fields = $fields:tt,
        body = (
            #[column_name($column_name:ident)]
            $field_name:ident : $($tail:tt)*),
    ) => {
        __diesel_parse_struct_body! {
            $headers,
            callback = $callback,
            fields = $fields,
            body = ($field_name as $column_name : $($tail)*),
        }
    };

    // If we got here and didn't have a #[column_name] attr,
    // then the column name is the same as the field name
    (
        $headers:tt,
        callback = $callback:ident,
        fields = $fields:tt,
        body = ($field_name:ident : $($tail:tt)*),
    ) => {
        __diesel_parse_struct_body! {
            $headers,
            callback = $callback,
            fields = $fields,
            body = ($field_name as $field_name : $($tail)*),
        }
    };

    // At this point we know the column and field name, handle when the type is option
    (
        $headers:tt,
        callback = $callback:ident,
        fields = [$($fields:tt)*],
        body = ($field_name:ident as $column_name:ident : Option<$field_ty:ty>, $($tail:tt)*),
    ) => {
        __diesel_parse_struct_body! {
            $headers,
            callback = $callback,
            fields = [$($fields)* {
                field_name: $field_name,
                column_name: $column_name,
                field_ty: Option<$field_ty>,
                field_kind: option,
                inner_field_ty: $field_ty,
            }],
            body = ($($tail)*),
        }
    };

    // Handle any type other than option
    (
        $headers:tt,
        callback = $callback:ident,
        fields = [$($fields:tt)*],
        body = ($field_name:ident as $column_name:ident : $field_ty:ty, $($tail:tt)*),
    ) => {
        __diesel_parse_struct_body! {
            $headers,
            callback = $callback,
            fields = [$($fields)* {
                field_name: $field_name,
                column_name: $column_name,
                field_ty: $field_ty,
                field_kind: regular,
                inner_field_ty: $field_ty,
            }],
            body = ($($tail)*),
        }
    };

    // When we reach a type with no column name annotation, handle the unnamed
    // tuple struct field. Since we require that either all fields are annotated
    // or none are, we could actually handle the whole body in one pass for this
    // case. However, anything using tuple structs without the column name
    // likely needs some ident per field to be useable and by handling each
    // field separately this way, the `field_kind` acts as a fresh ident each
    // time.
    (
        $headers:tt,
        callback = $callback:ident,
        fields = [$($fields:tt)*],
        body = ($field_ty:ty , $($tail:tt)*),
    ) => {
        __diesel_parse_struct_body! {
            $headers,
            callback = $callback,
            fields = [$($fields)* {
                field_ty: $field_ty,
                field_kind: bare,
                inner_field_ty: $field_ty,
            }],
            body = ($($tail)*),
        }
    };

    // At this point we've parsed the entire body. We create the pattern
    // for destructuring, and pass all the information back to the main macro
    // to generate the final impl
    (
        $headers:tt,
        callback = $callback:ident,
        fields = $fields:tt,
        body = (),
    ) => {
        $callback! {
            $headers,
            fields = $fields,
        }
    };
}

/// Hack to tell the compiler that something is in fact an item. This is needed
/// when `tt` fragments are used in specific positions.
#[doc(hidden)]
#[macro_export]
macro_rules!  __diesel_parse_as_item {
    ($i:item) => { $i }
}

#[doc(hidden)]
#[macro_export]
macro_rules! __diesel_field_with_column_name {
    (
        $headers:tt,
        callback = $callback:ident,
        target = $target_column_name:ident,
        fields = [$({
            field_name: $field_name:ident,
            column_name: $column_name:ident,
            $($field_info:tt)*
        })*],
    ) => {
        $(
            static_cond! {
                if $target_column_name == $column_name {
                    $callback! {
                        $headers,
                        found_field_with_column_name = $column_name,
                        field = {
                            field_name: $field_name,
                            column_name: $column_name,
                            $($field_info)*
                        },
                    }
                }
            }
        )*
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __diesel_field_with_field_name {
    (
        $headers:tt,
        callback = $callback:ident,
        target = $target_field_name:ident,
        fields = [$({
            field_name: $field_name:ident,
            $($field_info:tt)*
        })*],
    ) => {
        $(
            static_cond! {
                if $target_field_name == $field_name {
                    $callback! {
                        $headers,
                        found_field_with_field_name = $field_name,
                        field = {
                            field_name: $field_name,
                            $($field_info)*
                        },
                    }
                }
            }
        )*
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __diesel_fields_with_field_names {
    // Entrypoint, start search
    (
        $headers:tt,
        callback = $callback:ident,
        targets = ($target_field_name:ident $(,$rest:ident)*),
        fields = $fields:tt,
    ) => {
        __diesel_field_with_field_name! {
            (
                targets = ($($rest),*),
                fields = $fields,
                headers = $headers,
                callback = $callback,
                found_fields = [],
            ),
            callback = __diesel_fields_with_field_names,
            target = $target_field_name,
            fields = $fields,
        }
    };

    // Found field, more to search for
    (
        (
            targets = ($target_field_name:ident $(,$rest:ident)*),
            fields = $fields:tt,
            headers = $headers:tt,
            callback = $callback:ident,
            found_fields = [$($found_fields:tt)*],
        ),
        found_field_with_field_name = $ignore:tt,
        field = $field:tt,
    ) => {
        __diesel_field_with_field_name! {
            (
                targets = ($($rest),*),
                fields = $fields,
                headers = $headers,
                callback = $callback,
                found_fields = [$($found_fields)* $field],
            ),
            callback = __diesel_fields_with_field_names,
            target = $target_field_name,
            fields = $fields,
        }
    };

    // Found field, no more to search for
    (
        (
            targets = (),
            fields = $fields:tt,
            headers = $headers:tt,
            callback = $callback:ident,
            found_fields = [$($found_fields:tt)*],
        ),
        found_field_with_field_name = $ignore:tt,
        field = $field:tt,
    ) => {
        $callback! {
            $headers,
            found_fields_with_field_names,
            fields = [$($found_fields)* $field],
        }
    }
}

#[doc(hidden)]
#[macro_export]
macro_rules! __diesel_find_option_with_name {
    // When no default is given, just a simple search will work
    (
        $headers:tt,
        option_name = $target_option_name:ident,
        args = [$(#[$option_name:ident $option_value:tt])*],
        callback = $callback:ident,
    ) => { $(
        static_cond! {
            if $target_option_name == $option_name {
                $callback! {
                    $headers,
                    found_option_with_name = $target_option_name,
                    value = $option_value,
                }
            }
        }
    )* };

    // When a default is given, we need to recurse so we know when we're done
    (
        $headers:tt,
        option_name = $target_option_name:ident,
        args = [#[$option_name:ident $option_value:tt] $($rest:tt)*],
        callback = $callback:ident,
        default = $default:tt,
    ) => {
        static_cond! {
            if $target_option_name == $option_name {
                $callback! {
                    $headers,
                    found_option_with_name = $target_option_name,
                    value = $option_value,
                }
            } else {
                __diesel_find_option_with_name! {
                    $headers,
                    option_name = $target_option_name,
                    args = [$($rest)*],
                    callback = $callback,
                    default = $default,
                }
            }
        }
    };

    // Default was given, and we didn't find a value
    (
        $headers:tt,
        option_name = $target_option_name:ident,
        args = [],
        callback = $callback:ident,
        default = $default:tt,
    ) => {
        $callback! {
            $headers,
            found_option_with_name = $target_option_name,
            value = $default,
        }
    };
}