evenframe_derive/
lib.rs

1use evenframe_core::derive::{enum_impl, struct_impl};
2use proc_macro::TokenStream;
3use syn::{Data, DeriveInput, parse_macro_input};
4
5/// For structs it generates both:
6/// - A `table_schema()` function returning a `helpers::TableSchema`, and
7/// - CRUD async functions (`create`, `update`, `delete`, `read`, `fetch`)
8///   that build JSON payloads and generate SQL query strings including:
9///     - For fields with an `edge` attribute, subqueries in the SELECT clause.
10///     - For fields with a `fetch` attribute, a FETCH clause listing the fetched field names.
11///   If the struct does not contain an "id" field, the handler functions are omitted.
12/// For enums it generates a `variants()` method returning a `TaggedUnion`.
13#[proc_macro_derive(
14    Evenframe,
15    attributes(
16        edge,
17        fetch,
18        define_field_statement,
19        subquery,
20        format,
21        permissions,
22        mock_data,
23        validators,
24        relation
25    )
26)]
27pub fn evenframe_derive(input: TokenStream) -> TokenStream {
28    let input = parse_macro_input!(input as DeriveInput);
29
30    match input.data {
31        Data::Struct(_) => struct_impl::generate_struct_impl(input).into(),
32        Data::Enum(_) => enum_impl::generate_enum_impl(input).into(),
33        _ => syn::Error::new(
34            input.ident.span(),
35            "Evenframe can only be used on structs and enums",
36        )
37        .to_compile_error()
38        .into(),
39    }
40}