1. 注册码支付后台
1.1 Seaorm
1.1.1 entity代码生成
user.rs
yaml
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize, ToSchema)]
#[sea_orm(table_name = "user")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub username: String,
pub password: String,
#[schema(value_type = String)]
pub created_at: DateTime,
#[schema(value_type = f64)]
pub balance: f64,
pub inviter_id: i32,
pub invite_count: i32,
#[schema(value_type = f64)]
pub invite_rebate_total: f64,
pub role_id: Option<i32>,
}
DeriveEntityModel 这个宏会生成代码
yaml
let mut ts: TokenStream = derives::expand_derive_entity_model(data, attrs)
.unwrap_or_else(Error::into_compile_error)
.into();
ts.extend([
derive_model(input_ts.clone()),
derive_active_model(input_ts),
]);
其中expand_derive_entity_model
生成了entity代码
py
let entity_def = table_name
.as_ref()
.map(|_table_name_| {
quote! {
#[doc = " Generated by sea-orm-macros"]
#[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)]
pub struct Entity;
#[automatically_derived]
impl sea_orm::prelude::EntityName for Entity {
fn schema_name(&self) -> Option<&str> {
#schema_name
}
fn table_name(&self) -> &str {
#_table_name_
}
fn comment(&self) -> Option<&str> {
#comment
}
}
}
})
.unwrap_or_default();
sea_orm::prelude::DeriveEntity
py
#[cfg(feature = "derive")]
#[proc_macro_derive(DeriveEntity, attributes(sea_orm))]
pub fn derive_entity(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
derives::expand_derive_entity(input)
.unwrap_or_else(Error::into_compile_error)
.into()
}
最终就是帮你的entiy实现了DeriveEntity的相关方法

1.2 宏展开后的代码
rust宏太强大了,但是有点太不透明了,怎么查看宏展开后的代码
:
- 将你的光标放在你想要展开的宏调用上,例如 #[derive(DeriveEntityModel)] 或者 println!。
- 打开命令面板(在 VS Code 中是 Ctrl+Shift+P)。
- 搜索并选择命令 "Rust Analyzer: Expand macro recursively" (递归展开宏)。
- 这会打开一个新的只读窗口,里面就是完全展开后的代码。

的确是有点难看懂。