Skip to main content

wae_database/orm/
macros.rs

1//! 宏定义模块
2//!
3//! 提供实体字段定义和自动实现 trait 的宏
4
5/// 实体字段定义宏辅助
6#[macro_export]
7macro_rules! entity_fields {
8    ($($name:ident: $type:ty),* $(,)?) => {
9        $(pub $name: $type,)*
10    };
11}
12
13/// 自动实现 ToRow 的宏
14#[macro_export]
15macro_rules! impl_to_row {
16    ($type:ty { $($col:literal => $field:ident),* $(,)? }) => {
17        impl ToRow for $type {
18            fn to_row(&self) -> Vec<(&'static str, wae_types::Value)> {
19                vec![
20                    $((col, self.$field.clone().into()),)*
21                ]
22            }
23        }
24    };
25}
26
27/// 自动实现 FromRow 的宏
28#[macro_export]
29macro_rules! impl_from_row {
30    ($type:ty { $($idx:literal => $field:ident: $ftype:ty),* $(,)? }) => {
31        impl FromRow for $type {
32            fn from_row(row: &$crate::DatabaseRow) -> $crate::DatabaseResult<Self> {
33                Ok(Self {
34                    $($field: row.get::<$ftype>($idx)?,)*
35                })
36            }
37        }
38    };
39}