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