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
// Built-in Lints
#![deny(warnings, missing_copy_implementations)]

// Clippy lints
#![cfg_attr(feature = "clippy", allow(needless_pass_by_value))]
#![cfg_attr(feature = "clippy", feature(plugin))]
#![cfg_attr(feature = "clippy", plugin(clippy(conf_file="../clippy.toml")))]
#![cfg_attr(feature = "clippy", allow(
    option_map_unwrap_or_else, option_map_unwrap_or,
))]
#![cfg_attr(feature = "clippy", warn(
    wrong_pub_self_convention, mut_mut, non_ascii_literal, similar_names, unicode_not_nfc,
    if_not_else, items_after_statements, used_underscore_binding,
))]

macro_rules! t {
    ($expr:expr) => {
        match $expr {
            Ok(val) => val,
            Err(e) => panic!("{}", e),
        }
    };
}

#[cfg(all(feature = "dotenv", feature = "diesel_infer_schema"))]
extern crate dotenv;
#[cfg(feature = "diesel_infer_schema")]
extern crate diesel_infer_schema;
extern crate diesel;
#[macro_use]
extern crate quote;
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate serde;
extern crate serde_json;

mod as_changeset;
mod associations;
mod ast_builder;
mod attr;
mod embed_migrations;
mod identifiable;
mod insertable;
mod model;
mod queryable;
#[cfg(feature = "diesel_infer_schema")]
mod schema_inference;
#[cfg(feature = "diesel_infer_schema")]
mod database_url;
mod util;
mod migrations;
mod check_version;

use proc_macro::TokenStream;
use syn::parse_derive_input;

#[proc_macro_derive(Queryable, attributes(column_name))]
pub fn derive_queryable(input: TokenStream) -> TokenStream {
    expand_derive(input, queryable::derive_queryable)
}

#[proc_macro_derive(Identifiable, attributes(table_name, primary_key))]
pub fn derive_identifiable(input: TokenStream) -> TokenStream {
    expand_derive(input, identifiable::derive_identifiable)
}

#[proc_macro_derive(Insertable, attributes(table_name, column_name))]
pub fn derive_insertable(input: TokenStream) -> TokenStream {
    expand_derive(input, insertable::derive_insertable)
}

#[proc_macro_derive(AsChangeset, attributes(table_name, primary_key, column_name, changeset_options))]
pub fn derive_as_changeset(input: TokenStream) -> TokenStream {
    expand_derive(input, as_changeset::derive_as_changeset)
}

#[proc_macro_derive(Associations, attributes(table_name, belongs_to))]
pub fn derive_associations(input: TokenStream) -> TokenStream {
    expand_derive(input, associations::derive_associations)
}

#[proc_macro_derive(InferSchema, attributes(infer_schema_options))]
#[cfg(feature = "diesel_infer_schema")]
pub fn derive_infer_schema(input: TokenStream) -> TokenStream {
    expand_derive(input, schema_inference::derive_infer_schema)
}

#[proc_macro_derive(InferTableFromSchema, attributes(infer_table_from_schema_options))]
#[cfg(feature = "diesel_infer_schema")]
pub fn derive_infer_table_from_schema(input: TokenStream) -> TokenStream {
    expand_derive(input, schema_inference::derive_infer_table_from_schema)
}

#[proc_macro_derive(EmbedMigrations, attributes(embed_migrations_options))]
pub fn derive_embed_migrations(input: TokenStream) -> TokenStream {
    expand_derive(input, embed_migrations::derive_embed_migrations)
}

fn expand_derive(input: TokenStream, f: fn(syn::DeriveInput) -> quote::Tokens) -> TokenStream {
    let _ = self::check_version::check_version();
    let item = parse_derive_input(&input.to_string()).unwrap();
    f(item).to_string().parse().unwrap()
}