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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::Result;
use syn::{DeriveInput, Ident};

use crate::model::Model;
use crate::parsers::PostgresType;
use crate::util::wrap_in_dummy_mod;

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

    let struct_name = &item.ident;
    let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();

    let sqlite_tokens = sqlite_tokens(&item, &model);
    let mysql_tokens = mysql_tokens(&item, &model);
    let pg_tokens = pg_tokens(&item, &model);

    Ok(wrap_in_dummy_mod(quote! {
        impl #impl_generics diesel::sql_types::SqlType
            for #struct_name #ty_generics
        #where_clause
        {
            type IsNull = diesel::sql_types::is_nullable::NotNull;
        }

        impl #impl_generics diesel::sql_types::SingleValue
            for #struct_name #ty_generics
        #where_clause
        {
        }

        #sqlite_tokens
        #mysql_tokens
        #pg_tokens
    }))
}

fn sqlite_tokens(item: &DeriveInput, model: &Model) -> Option<TokenStream> {
    model
        .sqlite_type
        .as_ref()
        .map(|sqlite_type| Ident::new(&sqlite_type.name.value(), Span::call_site()))
        .and_then(|ty| {
            if cfg!(not(feature = "sqlite")) {
                return None;
            }

            let struct_name = &item.ident;
            let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();

            Some(quote! {
                impl #impl_generics diesel::sql_types::HasSqlType<#struct_name #ty_generics>
                    for diesel::sqlite::Sqlite
                #where_clause
                {
                    fn metadata(_: &mut ()) -> diesel::sqlite::SqliteType {
                        diesel::sqlite::SqliteType::#ty
                    }
                }
            })
        })
}

fn mysql_tokens(item: &DeriveInput, model: &Model) -> Option<TokenStream> {
    model
        .mysql_type
        .as_ref()
        .map(|mysql_type| Ident::new(&mysql_type.name.value(), Span::call_site()))
        .and_then(|ty| {
            if cfg!(not(feature = "mysql")) {
                return None;
            }

            let struct_name = &item.ident;
            let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();

            Some(quote! {
                impl #impl_generics diesel::sql_types::HasSqlType<#struct_name #ty_generics>
                    for diesel::mysql::Mysql
                #where_clause
                {
                    fn metadata(_: &mut ()) -> diesel::mysql::MysqlType {
                        diesel::mysql::MysqlType::#ty
                    }
                }
            })
        })
}

fn pg_tokens(item: &DeriveInput, model: &Model) -> Option<TokenStream> {
    model.postgres_type.as_ref().and_then(|ty| {
        if cfg!(not(feature = "postgres")) {
            return None;
        }

        let struct_name = &item.ident;
        let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();

        let metadata_fn = match ty {
            PostgresType::Fixed(oid, array_oid) => quote!(
                fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
                    PgTypeMetadata::new(#oid, #array_oid)
                }
            ),
            PostgresType::Lookup(type_name, Some(type_schema)) => quote!(
                fn metadata(lookup: &mut Self::MetadataLookup) -> PgTypeMetadata {
                    lookup.lookup_type(#type_name, Some(#type_schema))
                }
            ),
            PostgresType::Lookup(type_name, None) => quote!(
                fn metadata(lookup: &mut Self::MetadataLookup) -> PgTypeMetadata {
                    lookup.lookup_type(#type_name, None)
                }
            ),
        };

        Some(quote! {
            use diesel::pg::{PgMetadataLookup, PgTypeMetadata};

            impl #impl_generics diesel::sql_types::HasSqlType<#struct_name #ty_generics>
                for diesel::pg::Pg
            #where_clause
            {
                #metadata_fn
            }
        })
    })
}