Files
aho_corasick
bigdecimal
bitflags
byteorder
cfg_if
chrono
diesel
associations
connection
expression
expression_methods
macros
migration
mysql
pg
query_builder
query_dsl
query_source
sql_types
sqlite
type_impls
types
diesel_derives
diesel_migrations
env_logger
idna
instant
ipnetwork
itoa
kernel32
libc
libsqlite3_sys
lock_api
log
matches
memchr
migrations_internals
migrations_macros
mysqlclient_sys
num_bigint
num_integer
num_traits
parking_lot
parking_lot_core
percent_encoding
pq_sys
proc_macro2
quickcheck
quote
r2d2
regex
regex_syntax
ryu
scheduled_thread_pool
scopeguard
serde
serde_derive
serde_json
smallvec
syn
thread_id
thread_local
time
tinyvec
tinyvec_macros
unicode_bidi
unicode_normalization
unicode_xid
url
utf8_ranges
uuid
winapi
  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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use proc_macro2;
use proc_macro2::*;
use syn;

use meta::*;
use util::*;

pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagnostic> {
    let struct_name = &item.ident;
    let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();

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

    let dummy_name = format!("_impl_sql_type_for_{}", item.ident);
    Ok(wrap_in_dummy_mod(
        Ident::new(&dummy_name.to_lowercase(), Span::call_site()),
        quote! {
            impl #impl_generics diesel::sql_types::NotNull
                for #struct_name #ty_generics
            #where_clause
            {
            }

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

            #sqlite_tokens
            #mysql_tokens
            #pg_tokens
        },
    ))
}

fn sqlite_tokens(item: &syn::DeriveInput) -> Option<proc_macro2::TokenStream> {
    MetaItem::with_name(&item.attrs, "sqlite_type")
        .map(|attr| attr.expect_ident_value())
        .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(_: &()) -> diesel::sqlite::SqliteType {
                        diesel::sqlite::SqliteType::#ty
                    }
                }
            })
        })
}

fn mysql_tokens(item: &syn::DeriveInput) -> Option<proc_macro2::TokenStream> {
    MetaItem::with_name(&item.attrs, "mysql_type")
        .map(|attr| attr.expect_ident_value())
        .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(_: &()) -> diesel::mysql::MysqlType {
                        diesel::mysql::MysqlType::#ty
                    }
                }
            })
        })
}

fn pg_tokens(item: &syn::DeriveInput) -> Option<proc_macro2::TokenStream> {
    MetaItem::with_name(&item.attrs, "postgres")
        .map(|attr| {
            if let Some(x) = get_type_name(&attr)? {
                Ok(x)
            } else if let Some(x) = get_oids(&attr)? {
                Ok(x)
            } else {
                Err(attr
                    .span()
                    .error("Missing required options")
                    .help("Valid options are `type_name` or `oid` and `array_oid`"))
            }
        })
        .and_then(|res| res.map_err(|e| e.emit()).ok())
        .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 {
                PgType::Fixed { oid, array_oid } => quote!(
                    fn metadata(_: &PgMetadataLookup) -> PgTypeMetadata {
                        PgTypeMetadata {
                            oid: #oid,
                            array_oid: #array_oid,
                        }
                    }
                ),
                PgType::Lookup(type_name) => quote!(
                    fn metadata(lookup: &PgMetadataLookup) -> PgTypeMetadata {
                        lookup.lookup_type(#type_name)
                    }
                ),
            };

            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
                }
            })
        })
}

fn get_type_name(attr: &MetaItem) -> Result<Option<PgType>, Diagnostic> {
    Ok(attr.nested_item("type_name")?.map(|ty| {
        attr.warn_if_other_options(&["type_name"]);
        PgType::Lookup(ty.expect_str_value())
    }))
}

fn get_oids(attr: &MetaItem) -> Result<Option<PgType>, Diagnostic> {
    if let Some(oid) = attr.nested_item("oid")? {
        attr.warn_if_other_options(&["oid", "array_oid"]);
        let array_oid = attr.required_nested_item("array_oid")?.expect_int_value();
        let oid = oid.expect_int_value();
        Ok(Some(PgType::Fixed {
            oid: oid as u32,
            array_oid: array_oid as u32,
        }))
    } else {
        Ok(None)
    }
}

enum PgType {
    Fixed { oid: u32, array_oid: u32 },
    Lookup(String),
}