diesel_derives/
queryable.rs

1use proc_macro2::{Span, TokenStream};
2use quote::quote;
3use syn::{parse_quote, DeriveInput, Ident, Index, Result};
4
5use crate::field::Field;
6use crate::model::Model;
7use crate::util::wrap_in_dummy_mod;
8
9pub fn derive(item: DeriveInput) -> Result<TokenStream> {
10    let model = Model::from_item(&item, false, true)?;
11
12    let struct_name = &item.ident;
13    let field_ty = &model
14        .fields()
15        .iter()
16        .map(Field::ty_for_deserialize)
17        .collect::<Vec<_>>();
18    let build_expr = model.fields().iter().enumerate().map(|(i, f)| {
19        let field_name = &f.name;
20        let i = Index::from(i);
21        quote!(#field_name: row.#i.try_into()?)
22    });
23    let sql_type = &(0..model.fields().len())
24        .map(|i| {
25            let i = Ident::new(&format!("__ST{i}"), Span::call_site());
26            quote!(#i)
27        })
28        .collect::<Vec<_>>();
29
30    let (_, ty_generics, _) = item.generics.split_for_impl();
31    let mut generics = item.generics.clone();
32    generics
33        .params
34        .push(parse_quote!(__DB: diesel::backend::Backend));
35    for id in 0..model.fields().len() {
36        let ident = Ident::new(&format!("__ST{id}"), Span::call_site());
37        generics.params.push(parse_quote!(#ident));
38    }
39    {
40        let where_clause = generics.where_clause.get_or_insert(parse_quote!(where));
41        where_clause
42            .predicates
43            .push(parse_quote!((#(#field_ty,)*): FromStaticSqlRow<(#(#sql_type,)*), __DB>));
44    }
45    let (impl_generics, _, where_clause) = generics.split_for_impl();
46
47    Ok(wrap_in_dummy_mod(quote! {
48        use diesel::deserialize::{self, FromStaticSqlRow, Queryable};
49        use diesel::row::{Row as _, Field as _};
50        use std::convert::TryInto;
51
52        impl #impl_generics Queryable<(#(#sql_type,)*), __DB> for #struct_name #ty_generics
53            #where_clause
54        {
55            type Row = (#(#field_ty,)*);
56
57            fn build(row: Self::Row) -> deserialize::Result<Self> {
58                Ok(Self {
59                    #(#build_expr,)*
60                })
61            }
62        }
63    }))
64}