lazybe_macros/
lib.rs

1use proc_macro::TokenStream;
2use syn::{DeriveInput, parse_macro_input};
3
4mod common;
5mod entity;
6mod entity_endpoint;
7mod r#enum;
8mod newtype;
9
10#[proc_macro_derive(Enum)]
11/// Generate an enum encoder and decoder for database operation.
12pub fn derive_enum(input: TokenStream) -> TokenStream {
13    let input = parse_macro_input!(input as DeriveInput);
14    r#enum::expand(input).into()
15}
16
17#[proc_macro_derive(Newtype)]
18/// Generate an newtype encoder and decoder for database operation.
19pub fn derive_newtype(input: TokenStream) -> TokenStream {
20    let input = parse_macro_input!(input as DeriveInput);
21    newtype::expand(input).into()
22}
23
24#[proc_macro_derive(Entity, attributes(lazybe))]
25/// Generates building blocks for reading from and writing to the database, and exposes the entity via the API.
26///
27/// # Required struct attributes
28/// - `table = "..."` - The table to read from and write to.
29///
30/// # Optional struct attributes
31/// - `endpoint = "..."` - The base URL path for exposing HTTP API. (e.g. `endpoint = /books`)
32/// - `collection_api = "..."` - The collection API style for listing entities.
33///   - `list` - (default) Return a collection as a list without filtering, sorting, paging.
34///   - `manual` - Do not derive and manually provide the trait impl.
35/// - `validation = "..."` - The validation hook to run on entity modification via API.
36///    - `default` - (default) A no-op validation which always pass.
37///    - `manual` - Do not derive and manually provide the trait impl.
38/// - `derive_to_schema` - Derive `ToSchema` for all sibling types. This is useful for generating OpenAPI documeentation on generated types.
39///
40/// # Field attributes
41/// - `primary_key` - Specify the field to be used as primary key.
42/// - `generate_with = "..."` - A function use for generating an ID. If omitted, ID should be generate by the database.
43/// - `created_at` - Speicify the field for created_at timestamp. The time is stamped once a record is created.
44/// - `updated_at` - Speicify the field for updated_at timestamp. The time is stamped once a record is updated.
45/// - `json` - The field should be encoded as JSON column.
46pub fn derive_entity(input: TokenStream) -> TokenStream {
47    let input = parse_macro_input!(input as DeriveInput);
48    entity::expand(input).into()
49}
50
51#[proc_macro_derive(EntityEndpoint, attributes(lazybe))]
52/// Generates building blocks for exposing entity via the API without directly map it to the database table.
53///
54/// # Required attributes
55/// - `endpoint = "..."` - The base URL path for exposing HTTP API. (e.g. `endpoint = /books`)
56///
57/// # Optional attributes
58/// - `collection_api = "..."` - The collection api style for listing entities.
59///   - `list` - (default) Return a collection as a list without filtering, sorting, paging.
60///   - `manual` - Do not derive and manually provide the trait impl.
61/// - `validation = "..."` - The validation hook to run on database modifications.
62///    - `default` - (default) A no-op validation which always pass.
63///    - `manual` - Do not derive and manually provide the trait impl.
64/// - `pk_ty = "..."` - Define the type of public key if applicable (e.g. `pk_ty = "i32"`)
65/// - `create_ty = "..."` - Define the type that can be use to create this custom entity type if applicable (e.g. `create_ty = "Vec<Book>"`)
66/// - `update_ty = "..."` - Define the type that can be use to update this custom entity type if applicable
67/// - `replace_ty = "..."` - Define the type that can be use to replace this custom entity type if applicable
68pub fn derive_entity_endpoint(input: TokenStream) -> TokenStream {
69    let input = parse_macro_input!(input as DeriveInput);
70    entity_endpoint::expand(input).into()
71}