diesel_derives/
query_id.rs

1use proc_macro2::TokenStream;
2use quote::quote;
3use syn::parse_quote;
4use syn::DeriveInput;
5
6use crate::util::wrap_in_dummy_mod;
7
8pub fn derive(mut item: DeriveInput) -> TokenStream {
9    for ty_param in item.generics.type_params_mut() {
10        ty_param.bounds.push(parse_quote!(QueryId));
11    }
12    let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();
13
14    let struct_name = &item.ident;
15    let lifetimes = item.generics.lifetimes();
16
17    let ty_params = item
18        .generics
19        .type_params()
20        .map(|ty_param| &ty_param.ident)
21        .collect::<Vec<_>>();
22
23    let query_id_ty_params = ty_params
24        .iter()
25        .map(|ty_param| quote!(<#ty_param as QueryId>::QueryId));
26    let has_static_query_id = ty_params
27        .iter()
28        .map(|ty_param| quote!(<#ty_param as QueryId>::HAS_STATIC_QUERY_ID));
29
30    wrap_in_dummy_mod(quote! {
31        use diesel::query_builder::QueryId;
32
33        #[allow(non_camel_case_types)]
34        impl #impl_generics QueryId for #struct_name #ty_generics
35        #where_clause
36        {
37            type QueryId = #struct_name<#(#lifetimes,)* #(#query_id_ty_params,)*>;
38
39            const HAS_STATIC_QUERY_ID: bool = #(#has_static_query_id &&)* true;
40        }
41    })
42}