diesel_derives/
identifiable.rs
1use proc_macro2::TokenStream;
2use quote::quote;
3use syn::parse_quote;
4use syn::DeriveInput;
5use syn::Result;
6
7use crate::model::Model;
8use crate::util::wrap_in_dummy_mod;
9
10pub fn derive(item: DeriveInput) -> Result<TokenStream> {
11 let model = Model::from_item(&item, false, false)?;
12
13 let struct_name = &item.ident;
14 let table_name = &model.table_names()[0];
15
16 let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();
17 let mut ref_generics = item.generics.clone();
18 ref_generics.params.push(parse_quote!('ident));
19 let (ref_generics, ..) = ref_generics.split_for_impl();
20
21 let mut field_ty = Vec::new();
22 let mut field_name = Vec::new();
23 for pk in model.primary_key_names.iter() {
24 let f = model.find_column(pk)?;
25 field_ty.push(&f.ty);
26 field_name.push(&f.name);
27 }
28
29 Ok(wrap_in_dummy_mod(quote! {
30 use diesel::associations::{HasTable, Identifiable};
31
32 impl #impl_generics HasTable for #struct_name #ty_generics
33 #where_clause
34 {
35 type Table = #table_name::table;
36
37 fn table() -> Self::Table {
38 #table_name::table
39 }
40 }
41
42 impl #ref_generics Identifiable for &'ident #struct_name #ty_generics
43 #where_clause
44 {
45 type Id = (#(&'ident #field_ty),*);
46
47 fn id(self) -> Self::Id {
48 (#(&self.#field_name),*)
49 }
50 }
51
52 impl #ref_generics Identifiable for &'_ &'ident #struct_name #ty_generics
53 #where_clause
54 {
55 type Id = (#(&'ident #field_ty),*);
56
57 fn id(self) -> Self::Id {
58 (#(&self.#field_name),*)
59 }
60 }
61 }))
62}