Skip to main content

yang_base_derive/
lib.rs

1//! yang-base 派生宏入口。
2
3#![warn(missing_docs)]
4
5use proc_macro::TokenStream;
6use proc_macro_error::proc_macro_error;
7use syn::{parse_macro_input, DeriveInput};
8
9mod action;
10mod table_entity;
11mod util;
12
13/// 为 struct 派生 `TypedAction` 实现,自动生成 `ActionMeta` 静态聚合。
14///
15/// # 必填属性
16///
17/// - `#[action(name = "...")]`:Action 唯一标识。
18///
19/// # 可选属性
20///
21/// - `#[action(display_name = "...")]`:用户可见名称(默认同 `name`)。
22/// - `#[action(description = "...")]`:简介(默认空字符串)。
23/// - `#[action(public)]`:标记为公开 Action(默认 `false`)。
24/// - `#[action(permissions("perm:a", "perm:b"))]`:所需权限列表。
25#[proc_macro_error]
26#[proc_macro_derive(Action, attributes(action))]
27pub fn derive_action(input: TokenStream) -> TokenStream {
28    let input = parse_macro_input!(input as DeriveInput);
29    action::expand(input).into()
30}
31
32/// 为 struct 派生 `TableEntity` 实现,同时生成对应的 `<Name>Field` 和 `<Name>Where` 枚举。
33///
34/// # 必填属性
35///
36/// - `#[table(name = "...")]`:数据库表名。
37/// - 至少一个字段标注 `#[entity(primary_key)]`。
38///
39/// # 可选表级属性
40///
41/// - `#[table(display_name = "...")]`:展示名,默认同表名。
42/// - `#[table(soft_delete = "...")]`:软删除字段名(当前版本保留,暂不展开生成)。
43///
44/// # 可选字段级属性(`#[entity(...)]`)
45///
46/// - `primary_key`:标记主键字段(必须有且仅有一个)。
47/// - `max_length = <n>`:字符串字段最大长度(默认 255)。
48/// - `unique`:在 TableConfig 中添加该字段的唯一索引。
49/// - `required = true/false`:覆盖"非 Option 默认必填"的推断。
50/// - `column = "..."`: 指定列名(默认同字段名)。
51/// - `skip`:跳过此字段,不出现在枚举和 TableConfig 中。
52#[proc_macro_error]
53#[proc_macro_derive(TableEntity, attributes(table, entity))]
54pub fn derive_table_entity(input: TokenStream) -> TokenStream {
55    let input = parse_macro_input!(input as DeriveInput);
56    table_entity::expand(input).into()
57}