Skip to main content

supabase_client_derive/
lib.rs

1extern crate proc_macro;
2
3mod parse;
4mod table_impl;
5
6use proc_macro::TokenStream;
7use syn::{parse_macro_input, DeriveInput};
8
9/// Derive the `Table` trait for a struct.
10///
11/// # Attributes
12///
13/// ## Struct-level: `#[table(...)]`
14/// - `name = "table_name"` - Database table name (defaults to snake_case of struct name)
15/// - `schema = "schema_name"` - Database schema (defaults to "public")
16///
17/// ## Field-level: `#[primary_key]` / `#[primary_key(auto_generate)]`
18/// - Marks a field as part of the primary key
19/// - `auto_generate` excludes it from inserts (e.g., serial/identity columns)
20///
21/// ## Field-level: `#[column(...)]`
22/// - `name = "col_name"` - Database column name (defaults to field name)
23/// - `skip` - Skip this field entirely
24/// - `auto_generate` - Exclude from inserts (e.g., auto-populated columns)
25///
26/// # Example
27///
28/// ```ignore
29/// #[derive(Table, sqlx::FromRow)]
30/// #[table(name = "cities")]
31/// struct City {
32///     #[primary_key(auto_generate)]
33///     pub id: i32,
34///     #[column(name = "name")]
35///     pub name: String,
36///     pub country_id: i32,
37/// }
38/// ```
39#[proc_macro_derive(Table, attributes(table, primary_key, column))]
40pub fn derive_table(input: TokenStream) -> TokenStream {
41    let input = parse_macro_input!(input as DeriveInput);
42    match table_impl::expand_table_derive(&input) {
43        Ok(tokens) => tokens.into(),
44        Err(e) => e.to_compile_error().into(),
45    }
46}