diesel_derives/
from_sql_row.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::{ty_for_foreign_derive, wrap_in_dummy_mod};
9
10pub fn derive(mut item: DeriveInput) -> Result<TokenStream> {
11    let model = Model::from_item(&item, true, false)?;
12    let struct_ty = ty_for_foreign_derive(&item, &model)?;
13
14    {
15        item.generics.params.push(parse_quote!(__DB));
16        item.generics.params.push(parse_quote!(__ST));
17        let where_clause = item.generics.make_where_clause();
18        where_clause
19            .predicates
20            .push(parse_quote!(__DB: diesel::backend::Backend));
21        where_clause
22            .predicates
23            .push(parse_quote!(__ST: diesel::sql_types::SingleValue));
24        where_clause
25            .predicates
26            .push(parse_quote!(Self: FromSql<__ST, __DB>));
27    }
28    let (impl_generics, _, where_clause) = item.generics.split_for_impl();
29
30    Ok(wrap_in_dummy_mod(quote! {
31        use diesel::deserialize::{self, FromSql, Queryable};
32
33        // Need to put __ST and __DB after lifetimes but before const params
34        impl #impl_generics Queryable<__ST, __DB> for #struct_ty
35        #where_clause
36        {
37            type Row = Self;
38
39            fn build(row: Self::Row) -> deserialize::Result<Self> {
40                Ok(row)
41            }
42        }
43    }))
44}