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
/// This will implement `SelectableExpression` and `AppearsOnTable` for "simple"
/// composite nodes where the where clause is roughly `AllTyParams:
/// SelectableExpression<QS>, Self: Expression`.
///
/// This macro is exported because we want to be able to call it from other
/// macros that are exported, but it is not part of our public API.
#[macro_export]
#[doc(hidden)]
macro_rules! impl_selectable_expression {
    ($struct_name:ident) => {
        impl_selectable_expression!(ty_params = (), struct_ty = $struct_name,);
    };

    ($struct_name:ident<$($ty_params:ident),+>) => {
        impl_selectable_expression!(
            ty_params = ($($ty_params),+),
            struct_ty = $struct_name<$($ty_params),+>,
        );
    };

    (ty_params = ($($ty_params:ident),*), struct_ty = $struct_ty:ty,) => {
        impl<$($ty_params,)* QS> $crate::expression::SelectableExpression<QS>
            for $struct_ty where
                $struct_ty: $crate::expression::AppearsOnTable<QS>,
                $($ty_params: $crate::expression::SelectableExpression<QS>,)*
        {
        }

        impl<$($ty_params,)* QS> $crate::expression::AppearsOnTable<QS>
            for $struct_ty where
                $struct_ty: $crate::expression::Expression,
                $($ty_params: $crate::expression::AppearsOnTable<QS>,)*
        {
        }
    };
}

/// Parses a sequence of type parameters and their bounds.
///
/// Ideally we would just be able to write this as
/// `$($param:ident $(: $bound:ty)*),* $(,)*`, but Rust doesn't allow a `ty`
/// fragment in a trait bound position.
///
/// Assuming we don't care about lifetimes or existentials, we could also
/// possibly write `$($param:ident $(: $bound:path)+*),* $(,)*` but Rust doesn't
/// allow `+` as a separator. In fact, we can't even use the `path` fragment
/// at all, as Rust does not allow it to be followed by `+` or `tt`.
///
/// This macro takes three arguments.
///
/// - `data`: Any arbitrary token tree you'd like given back to you when parsing
///   is complete.
/// - `callback`: The name of the macro to call when parsing is complete
/// - `tokens`: The tokens to be parsed.
///
/// This macro will consume tokens until it reaches a `>` that was not
/// preceded by a `<`. It will then invoke `callback` with 4 arguments:
///
/// - `data`: Whatever you gave this macro.
/// - `type_args`: The names of the type parameters. Always contains a trailing
///   comma if non-empty.
/// - `type_args_with_bounds`: The arguments and their bounds. Always contains
///   a trailing comma if non-empty
/// - `unparsed_tokens`: Any tokens that were not consumed by this macro.
#[macro_export]
#[doc(hidden)]
macro_rules! __diesel_parse_type_args {
    // ENTRYPOINT
    //
    // Set up our lists.
    // Continues to NEXT_ARG, NEXT_ARG_NO_BOUNDS, or EXIT
    (
        data = $data:tt,
        callback = $callback:ident,
        tokens = $tokens:tt,
    ) => {
        __diesel_parse_type_args! {
            data = $data,
            callback = $callback,
            args = (),
            bounds = (),
            tokens = $tokens,
        }
    };

    // NEXT_ARG
    //
    // No arg currently being parsed, next token is an argument with a `:`.
    // From here we set up a stack of `<` to track,
    // and start putting tokens into the bounds.
    //
    // Continues to BOUNDS or OPENING_BRACKET
    (
        data = $data:tt,
        callback = $callback:ident,
        args = ($($args:tt)*),
        bounds = ($($bounds:tt)*),
        tokens = ($next_arg:ident : $($tokens:tt)*),
    ) => {
        __diesel_parse_type_args! {
            brackets = (),
            data = $data,
            callback = $callback,
            args = ($($args)* $next_arg,),
            bounds = ($($bounds)* $next_arg:),
            tokens = ($($tokens)*),
        }
    };

    // NEXT_ARG_NO_BOUNDS
    //
    // No arg currently being parsed, next token is an argument without a :.
    //
    // Continues to NEXT_ARG, NEXT_ARG_NO_BOUNDS, NO_ARG_COMMA, or EXIT
    (
        data = $data:tt,
        callback = $callback:ident,
        args = ($($args:tt)*),
        bounds = ($($bounds:tt)*),
        tokens = ($next_arg:ident $($tokens:tt)*),
    ) => {
        __diesel_parse_type_args! {
            data = $data,
            callback = $callback,
            args = ($($args)* $next_arg,),
            bounds = ($($bounds)* $next_arg,),
            tokens = ($($tokens)*),
        }
    };

    // FINAL_CLOSING_BRACKET
    //
    // > encountered, no bracket on the stack. We're done.
    //
    // Continues to EXIT
    (
        brackets = (),
        data = $data:tt,
        callback = $callback:ident,
        args = $args:tt,
        bounds = ($($bounds:tt)*),
        tokens = (> $($tokens:tt)*),
    ) => {
        __diesel_parse_type_args! {
            data = $data,
            callback = $callback,
            args = $args,
            bounds = ($($bounds)*,),
            tokens = (> $($tokens)*),
        }
    };

    // END_OF_BOUND
    //
    // , encountered, no bracket on the stack. We're done.
    //
    // Continues to NEXT_ARG, NEXT_ARG_NO_BOUNDS, or EXIT
    (
        brackets = (),
        data = $data:tt,
        callback = $callback:ident,
        args = $args:tt,
        bounds = ($($bounds:tt)*),
        tokens = (, $($tokens:tt)*),
    ) => {
        __diesel_parse_type_args! {
            data = $data,
            callback = $callback,
            args = $args,
            bounds = ($($bounds)* ,),
            tokens = ($($tokens)*),
        }
    };

    // OPENING_BRACKET
    //
    // Token is <, push it on the stack.
    //
    // Continues to BOUNDS, OPENING_BRACKET, CLOSING_BRACKET, or
    // DOUBLE_CLOSING_BRACKET.
    (
        brackets = ($($brackets:tt)*),
        data = $data:tt,
        callback = $callback:ident,
        args = $args:tt,
        bounds = ($($bounds:tt)*),
        tokens = (< $($tokens:tt)*),
    ) => {
        __diesel_parse_type_args! {
            brackets = (< $($brackets)*),
            data = $data,
            callback = $callback,
            args = $args,
            bounds = ($($bounds)* <),
            tokens = ($($tokens)*),
        }
    };

    // DOUBLE_CLOSING_BRACKET
    //
    // Rust treats >> as a single token, so we have to special case it.
    //
    // Continues to CLOSING_BRACKET or FINAL_CLOSING_BRACKET.
    (
        brackets = (< $($brackets:tt)*),
        data = $data:tt,
        callback = $callback:ident,
        args = $args:tt,
        bounds = ($($bounds:tt)*),
        tokens = (>> $($tokens:tt)*),
    ) => {
        __diesel_parse_type_args! {
            brackets = ($($brackets)*),
            data = $data,
            callback = $callback,
            args = $args,
            bounds = ($($bounds)* >),
            tokens = (> $($tokens)*),
        }
    };

    // CLOSING_BRACKET
    //
    // Token is >, and we have a non-empty bracket stack.
    // Pop it and continue.
    //
    // Continues to BOUNDS, OPENING_BRACKET, CLOSING_BRACKET,
    // DOUBLE_CLOSING_BRACKET, FINAL_CLOSING_BRACKET, or END_OF_BOUND
    (
        brackets = (< $($brackets:tt)*),
        data = $data:tt,
        callback = $callback:ident,
        args = $args:tt,
        bounds = ($($bounds:tt)*),
        tokens = (> $($tokens:tt)*),
    ) => {
        __diesel_parse_type_args! {
            brackets = ($($brackets)*),
            data = $data,
            callback = $callback,
            args = $args,
            bounds = ($($bounds)* >),
            tokens = ($($tokens)*),
        }
    };

    // BOUNDS
    //
    // Token is not a , or >. It's part of the trait bounds.
    //
    // Continues to BOUNDS, OPENING_BRACKET, CLOSING_BRACKET,
    // DOUBLE_CLOSING_BRACKET, FINAL_CLOSING_BRACKET, or END_OF_BOUND.
    (
        brackets = $brackets:tt,
        data = $data:tt,
        callback = $callback:ident,
        args = $args:tt,
        bounds = ($($bounds:tt)*),
        tokens = ($token:tt $($tokens:tt)*),
    ) => {
        __diesel_parse_type_args! {
            brackets = $brackets,
            data = $data,
            callback = $callback,
            args = $args,
            bounds = ($($bounds)* $token),
            tokens = ($($tokens)*),
        }
    };

    // NO_ARG_COMMA
    //
    // No arg currently being parsed, , encountered. Skip.
    //
    // Continues to NEXT_ARG, NEXT_ARG_NO_BOUNDS, or EXIT
    (
        data = $data:tt,
        callback = $callback:ident,
        args = $args:tt,
        bounds = $bounds:tt,
        tokens = (, $($tokens:tt)*),
    ) => {
        __diesel_parse_type_args! {
            data = $data,
            callback = $callback,
            args = $args,
            bounds = $bounds,
            tokens = ($($tokens)*),
        }
    };

    // EXIT
    //
    // No arg currently being parsed, > encountered.
    (
        data = $data:tt,
        callback = $callback:ident,
        args = $args:tt,
        bounds = $bounds:tt,
        tokens = (> $($tokens:tt)*),
    ) => {
        $callback! {
            data = $data,
            type_args = $args,
            type_args_with_bounds = $bounds,
            unparsed_tokens = ($($tokens)*),
        }
    };
}

#[test]
fn parse_type_args_empty() {
    let expected = stringify!(
        data = (),
        type_args = (),
        type_args_with_bounds = (),
        unparsed_tokens = (),
    );
    let actual = __diesel_parse_type_args! {
        data = (),
        callback = stringify,
        tokens = (>),
    };

    assert_eq!(expected, actual);
}

#[test]
fn parse_type_args_simple() {
    let expected = stringify!(
        data = (foo),
        type_args = (T,),
        type_args_with_bounds = (T,),
        unparsed_tokens = (),
    );
    let actual = __diesel_parse_type_args! {
        data = (foo),
        callback = stringify,
        tokens = (T>),
    };

    assert_eq!(expected, actual);

    let actual = __diesel_parse_type_args! {
        data = (foo),
        callback = stringify,
        tokens = (T,>),
    };

    assert_eq!(expected, actual);
}

#[test]
fn parse_type_args_multiple() {
    let expected = stringify!(
        data = (foo),
        type_args = (T, U,),
        type_args_with_bounds = (T, U,),
        unparsed_tokens = (),
    );
    let actual = __diesel_parse_type_args! {
        data = (foo),
        callback = stringify,
        tokens = (T,U>),
    };

    assert_eq!(expected, actual);

    let actual = __diesel_parse_type_args! {
        data = (foo),
        callback = stringify,
        tokens = (T,U,>),
    };

    assert_eq!(expected, actual);
}

#[test]
fn parse_type_args_with_bounds() {
    let expected = stringify!(
        data = (foo),
        type_args = (T, U,),
        type_args_with_bounds = (T: Foo + Bar, U: Baz,),
        unparsed_tokens = (),
    );
    let actual = __diesel_parse_type_args! {
        data = (foo),
        callback = stringify,
        tokens = (T: Foo + Bar, U: Baz>),
    };

    assert_eq!(expected, actual);

    let actual = __diesel_parse_type_args! {
        data = (foo),
        callback = stringify,
        tokens = (T: Foo + Bar, U: Baz,>),
    };

    assert_eq!(expected, actual);
}

#[test]
fn parse_type_args_with_bounds_containing_braces_and_commas() {
    let expected = stringify!(
        data = (foo),
        type_args = (T,U,),
        type_args_with_bounds = (T: Foo<X> + Bar<U, V>,U: Baz<Vec<i32>, String>,),
        unparsed_tokens = (),
    );
    let actual = __diesel_parse_type_args! {
        data = (foo),
        callback = stringify,
        tokens = (T: Foo<X> + Bar<U, V>,U: Baz<Vec<i32>, String>>),
    };

    assert_eq!(expected, actual);

    let actual = __diesel_parse_type_args! {
        data = (foo),
        callback = stringify,
        tokens = (T: Foo<X> + Bar<U, V>,U: Baz<Vec<i32>, String>,>),
    };

    assert_eq!(expected, actual);
}

#[test]
fn parse_type_args_with_trailer() {
    let expected = stringify!(
        data = (
            meta = (#[aggregate]),
            fn_name = max,
        ),
        type_args = (ST,),
        type_args_with_bounds = (ST: SqlOrd + IntoNullable,),
        unparsed_tokens = ((expr: ST) -> ST::Nullable),
    );
    let actual = __diesel_parse_type_args! {
        data = (
            meta = (#[aggregate]),
            fn_name = max,
        ),
        callback = stringify,
        tokens = (ST: SqlOrd + IntoNullable>(expr: ST) -> ST::Nullable),
    };

    assert_eq!(expected, actual);
}

#[test]
fn parse_type_args_with_existentials_and_lifetimes() {
    let expected = stringify! (
        data = (),
        type_args = (ST,U,),
        type_args_with_bounds = (ST: for<'a> Foo<'a, U> + 'static, U,),
        unparsed_tokens = (),
    );
    let actual = __diesel_parse_type_args! {
        data = (),
        callback = stringify,
        tokens = (ST: for<'a> Foo<'a, U> + 'static, U>),
    };

    assert_eq!(expected, actual);
}