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
use proc_macro2::TokenStream;
use quote::quote;
use syn::parse_quote;
use syn::DeriveInput;
use syn::Result;

use crate::model::Model;
use crate::util::{ty_for_foreign_derive, wrap_in_dummy_mod};

pub fn derive(mut item: DeriveInput) -> Result<TokenStream> {
    let model = Model::from_item(&item, true, false)?;
    let struct_ty = ty_for_foreign_derive(&item, &model)?;

    let type_params = item
        .generics
        .type_params()
        .map(|param| param.ident.clone())
        .collect::<Vec<_>>();

    for type_param in type_params {
        let where_clause = item.generics.make_where_clause();
        where_clause
            .predicates
            .push(parse_quote!(#type_param: ValidGrouping<__GroupByClause>));
    }

    if model.aggregate {
        item.generics.params.push(parse_quote!(__GroupByClause));
        let (impl_generics, _, where_clause) = item.generics.split_for_impl();

        Ok(wrap_in_dummy_mod(quote! {
            use diesel::expression::{ValidGrouping, MixedAggregates, is_aggregate};

            impl #impl_generics ValidGrouping<__GroupByClause> for #struct_ty
            #where_clause
            {
                type IsAggregate = is_aggregate::Yes;
            }
        }))
    } else {
        let mut aggregates = item
            .generics
            .type_params()
            .map(|t| quote!(#t::IsAggregate))
            .collect::<Vec<_>>()
            .into_iter();

        let is_aggregate = aggregates
            .next()
            .map(|first| {
                let where_clause = item.generics.make_where_clause();
                aggregates.fold(first, |left, right| {
                    where_clause.predicates.push(parse_quote!(
                        #left: MixedAggregates<#right>
                    ));
                    quote!(<#left as MixedAggregates<#right>>::Output)
                })
            })
            .unwrap_or_else(|| quote!(is_aggregate::Never));

        item.generics.params.push(parse_quote!(__GroupByClause));
        let (impl_generics, _, where_clause) = item.generics.split_for_impl();

        Ok(wrap_in_dummy_mod(quote! {
            use diesel::expression::{ValidGrouping, MixedAggregates, is_aggregate};

            impl #impl_generics ValidGrouping<__GroupByClause> for #struct_ty
            #where_clause
            {
                type IsAggregate = #is_aggregate;
            }
        }))
    }
}