geekorm_derive/
lib.rs

1#![allow(unused_imports, dead_code)]
2#![forbid(unsafe_code)]
3#![doc = include_str!("../README.md")]
4#![doc(
5    html_logo_url = "https://raw.githubusercontent.com/42ByteLabs/geekorm/main/assets/geekorm.png"
6)]
7
8extern crate proc_macro;
9extern crate proc_macro2;
10extern crate quote;
11extern crate syn;
12
13#[macro_use]
14mod macros;
15
16mod attr;
17mod derive;
18mod errors;
19mod internal;
20mod parsers;
21
22use parsers::{derive_parser, enum_parser};
23use proc_macro::TokenStream;
24use quote::{ToTokens, quote};
25use syn::{Data, DataEnum, DataStruct, DeriveInput, Fields, parse_macro_input};
26
27/// Derive macro for `Table` trait.
28///
29/// This macro will generate the implementation of `Table` trait for the given struct.
30/// The struct must have named fields.
31///
32/// # Example
33///
34/// This macro generates a number of methods for the struct, including `table` and `table_name` methods.
35///
36/// ```rust
37/// use geekorm::prelude::*;
38///
39/// #[derive(Table, Default, serde::Serialize, serde::Deserialize)]
40/// struct Users {
41///     id: PrimaryKeyInteger,
42///     name: String,
43///     age: i32,
44///     occupation: String,
45/// }
46///
47/// // This will get you the underlying table information.
48/// let table = Users::table();
49/// assert_eq!(Users::table_name(), "Users");
50/// ```
51#[proc_macro_derive(Table, attributes(geekorm, gorm))]
52pub fn table_derive(input: TokenStream) -> TokenStream {
53    let ast: DeriveInput = parse_macro_input!(input as DeriveInput);
54
55    derive_parser(&ast).unwrap().into()
56}
57
58#[deprecated(
59    since = "0.4.2",
60    note = "This macro is depricated, please use `Table` instead."
61)]
62#[proc_macro_derive(GeekTable, attributes(geekorm))]
63pub fn depricated_table_derive(input: TokenStream) -> TokenStream {
64    table_derive(input)
65}
66
67/// Data is the derive macro for serializing and deserializing custom column types.
68///
69/// ```rust
70/// use geekorm::prelude::*;
71///
72/// # #[derive(Eq, PartialEq, Debug)]
73/// #[derive(Data, Default, Clone)]
74/// enum Role {
75///     Admin,
76///     Moderator,
77///     #[geekorm(key = "UserAccounts")]
78///     User,
79///     #[default]
80///     Guest,
81/// }
82///
83/// #[derive(Table, Default, serde::Serialize, serde::Deserialize)]
84/// struct Users {
85///     #[geekorm(primary_key, auto_increment)]
86///     id: PrimaryKeyInteger,
87///     username: String,
88///     role: Role,
89/// }
90///
91/// let geekmasher = Users::new("geekmasher", Role::Admin);
92///
93/// # assert_eq!(geekmasher.role, Role::Admin);
94/// # assert_eq!(Value::from(geekmasher.role), Value::Text("Admin".to_string()));
95/// # assert_eq!(Role::from(Value::Text("Admin".to_string())), Role::Admin);
96/// # assert_eq!(Role::from(Value::Text("UserAccounts".to_string())), Role::User);
97///
98/// let role = Role::from("UserAccounts");
99/// # assert_eq!(role, Role::User);
100/// # assert_eq!(role.to_string(), String::from("UserAccounts"));
101/// ```
102#[proc_macro_derive(Data, attributes(geekorm))]
103pub fn data_derive(input: TokenStream) -> TokenStream {
104    let ast: DeriveInput = parse_macro_input!(input as DeriveInput);
105
106    enum_parser(&ast).unwrap().into()
107}
108
109/// Value is the derive macro for serializing and deserializing custom column types.
110#[deprecated(
111    since = "0.4.2",
112    note = "This macro is depricated, please use `Data` instead."
113)]
114#[proc_macro_derive(GeekValue)]
115pub fn depricated_value_derive(input: TokenStream) -> TokenStream {
116    data_derive(input)
117}