diesel_derive_more/lib.rs
1#![recursion_limit = "128"]
2//! # Diesel Derive More
3//!
4//! Additional derive functionality for the diesel
5//! models which make it easier to get started
6//!
7//! ## DBEnum
8//!
9//! DBEnum provides diesel implementations for using an enum as a string field
10//! in models. Deriving DBEnum in an enum provides serializationa and
11//! deserialization traits.
12//!
13//! ## DefaultInsertable
14//!
15//! Default insertable provides a default implementation for a diesel model
16//! It allows marking fields as auto_increment so that they are excluded from
17//! the derived struct.
18//!
19//! The new struct is prefixed with 'New' and lives in the same module as the
20//! original struct.
21//!
22//! When using the serialization feature, the stuct is also serializable through
23//! serde
24//!
25//! ## Examples
26//!
27//! For examples of usage check the corresponding tests in the tests/ dir
28
29
30#[macro_use]
31extern crate quote;
32extern crate proc_macro;
33extern crate syn;
34
35mod default_insertable;
36mod db_enum;
37
38use proc_macro::TokenStream;
39
40use default_insertable::impl_default_insertable;
41use db_enum::impl_db_enum;
42use quote::Tokens;
43use syn::DeriveInput;
44
45
46#[proc_macro_derive(DefaultInsertable, attributes(auto_increment, table_name))]
47pub fn default_insertable(input: TokenStream) -> TokenStream {
48 expand(input, impl_default_insertable)
49}
50
51#[proc_macro_derive(DBEnum)]
52pub fn db_enum(input: TokenStream) -> TokenStream {
53 expand(input, impl_db_enum)
54}
55
56fn expand(input: TokenStream, func: fn(&DeriveInput) -> Tokens) -> TokenStream {
57 let ast = syn::parse(input).unwrap();
58 let gen = func(&ast);
59 TokenStream::from(gen)
60}