naphtha_proc_macro/
lib.rs

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
//! This crate is a support crate that contains the necessary macros for naphtha to compile.

extern crate proc_macro;
extern crate quote;

use {
    quote::quote,
    syn::{parse, DeriveInput},
};

#[cfg(any(
    feature = "barrel-sqlite",
    feature = "barrel-mysql",
    feature = "barrel-pg"
))]
mod barrel_impl;
mod database_impl;
mod database_traits;
#[allow(dead_code)]
mod helper;

#[proc_macro_attribute]
pub fn model(
    attr: ::proc_macro::TokenStream,
    item: ::proc_macro::TokenStream,
) -> ::proc_macro::TokenStream {
    let ast: DeriveInput = parse(item).expect(
        "proc_macro_attribute model: Could not parse TokenStream input!",
    );
    let attr = format!("#[{}]", attr);
    let attr: ::proc_macro2::TokenStream = attr.parse().unwrap();

    // QUERY BY PROPERTY TRAIT
    #[cfg(not(any(feature = "sqlite", feature = "mysql", feature = "pg")))]
    let impl_trait_query_by_properties = quote! {};
    #[cfg(any(feature = "sqlite", feature = "mysql", feature = "pg"))]
    let impl_trait_query_by_properties =
        database_traits::impl_trait_query_by_properties(&ast);

    // SQLITE
    #[cfg(not(feature = "sqlite"))]
    let impl_sqlite = quote! {};
    #[cfg(feature = "sqlite")]
    let impl_sqlite = database_impl::sqlite::impl_sqlite(&ast, &attr);

    #[cfg(not(feature = "barrel-sqlite"))]
    let impl_barrel_sqlite = quote! {};
    #[cfg(feature = "barrel-sqlite")]
    let impl_barrel_sqlite = barrel_impl::sqlite::impl_sqlite(&ast);

    // MYSQL
    #[cfg(not(feature = "mysql"))]
    let impl_mysql = quote! {};
    #[cfg(feature = "mysql")]
    let impl_mysql = database_impl::mysql::impl_mysql(&ast, &attr);

    #[cfg(not(feature = "barrel-mysql"))]
    let impl_barrel_mysql = quote! {};
    #[cfg(feature = "barrel-mysql")]
    let impl_barrel_mysql = barrel_impl::mysql::impl_mysql(&ast);

    // PostgreSQL
    #[cfg(not(feature = "pg"))]
    let impl_pg = quote! {};
    #[cfg(feature = "pg")]
    let impl_pg = database_impl::pg::impl_pg(&ast, &attr);
    #[cfg(not(feature = "barrel-pg"))]
    let impl_barrel_pg = quote! {};
    #[cfg(feature = "barrel-pg")]
    let impl_barrel_pg = barrel_impl::pg::impl_pg(&ast);

    let output = quote! {
        use self::schema::*;
        #[cfg(any(feature = "sqlite", feature = "mysql", feature = "pg"))]
        use {
            ::naphtha::diesel::{backend::Backend, prelude::*},
        };

        #[derive(
            Debug,
            Queryable,
            Identifiable,
            AsChangeset,
            Associations
            )]
        #attr
        #ast

        #impl_trait_query_by_properties

        #impl_sqlite
        #impl_barrel_sqlite

        #impl_mysql
        #impl_barrel_mysql

        #impl_pg
        #impl_barrel_pg
    };

    ::proc_macro::TokenStream::from(output)
}