pub struct ModuleSpec {
pub name: ModuleName,
pub table: Option<TableSpec>,
pub default_permissions: Vec<String>,
pub default_permission_mode: PermissionMode,
pub views: Vec<ViewSpec>,
pub presentation: Option<ModulePresentationSpec>,
pub datasource: Option<DatasourceId>,
/* private fields */
}Expand description
Module 的聚合定义。
Fields§
§name: ModuleName全限定 Module 名称。
table: Option<TableSpec>可选主表。
default_permissions: Vec<String>模块默认权限。
default_permission_mode: PermissionMode模块默认权限匹配方式。
views: Vec<ViewSpec>View 集合。
presentation: Option<ModulePresentationSpec>可选的前端 Module 页面展示声明。
datasource: Option<DatasourceId>模块所属逻辑数据源;None 表示使用 Addon/默认数据源。
Implementations§
Source§impl ModuleSpec
impl ModuleSpec
Sourcepub fn native_action<A>(self, handler: A) -> Selfwhere
A: BusinessAction + TypedAction,
pub fn native_action<A>(self, handler: A) -> Selfwhere
A: BusinessAction + TypedAction,
在手写 ModuleSpec 路径中原子注册一个原生 Action。
Sourcepub fn action_fn<F, I, O, Fut>(
self,
name: ActionName,
handler: F,
) -> ActionFnBuilder<F, I, O>where
F: Fn(ActionContext, I) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<O, BaseError>> + Send,
I: ParamInput + DeserializeOwned + JsonSchema + Send + 'static,
O: Serialize + JsonSchema + Send + 'static,
pub fn action_fn<F, I, O, Fut>(
self,
name: ActionName,
handler: F,
) -> ActionFnBuilder<F, I, O>where
F: Fn(ActionContext, I) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<O, BaseError>> + Send,
I: ParamInput + DeserializeOwned + JsonSchema + Send + 'static,
O: Serialize + JsonSchema + Send + 'static,
以一个普通 async fn / 闭包为 Handler 原子注册函数式 Action。
I/O 由 Handler 签名推断:Handler 形如
async fn(ActionContext, I) -> Result<O, BaseError>;闭包写法必须标注
参数类型以便推断 I。I 通常由 params! 生成,从而同时提供强类型输入
与唯一原生参数定义。
默认契约与 ModuleSpec::native_action 一致:POST /{name},
operation_id 补全为 <module>.<name>;其余定义经返回的
ActionFnBuilder 声明后由 ActionFnBuilder::register 终结注册,
route/params/权限与 Handler 在同一次调用链中原子完成,不存在
“先注册 spec 再绑 handler” 的两步形态。
§示例
ModuleSpec::new(module!("account.user"))
.action_fn(ActionName::new("disable")?, disable_user)
.route(HttpMethod::Post, "/users/disable")
.display_name("停用用户")
.permissions(["account.user:write"])
.register()Source§impl ModuleSpec
impl ModuleSpec
Sourcepub fn new(name: ModuleName) -> Self
pub fn new(name: ModuleName) -> Self
创建空 Module。
Sourcepub fn datasource(self, id: DatasourceId) -> Self
pub fn datasource(self, id: DatasourceId) -> Self
设置 Module 所属逻辑数据源;已声明主表会同步继承。
Sourcepub fn presentation(self, presentation: ModulePresentationSpec) -> Self
pub fn presentation(self, presentation: ModulePresentationSpec) -> Self
设置前端 Module 页面展示声明。
Sourcepub fn crud_at(self, prefix: impl Into<String>) -> Result<Self, BaseError>
pub fn crud_at(self, prefix: impl Into<String>) -> Result<Self, BaseError>
在显式资源路径下注册标准 add/put/del/get/select/table Actions。
用于版本化或资源复数化路由;operation_id 仍始终使用
<module>.<action>,不与 HTTP 路径耦合。
Sourcepub fn crud_at_with_mutations<A, P, D>(
self,
prefix: impl Into<String>,
add: A,
put: P,
del: D,
) -> Result<Self, BaseError>
pub fn crud_at_with_mutations<A, P, D>( self, prefix: impl Into<String>, add: A, put: P, del: D, ) -> Result<Self, BaseError>
Sourcepub fn action<A>(self, action: ActionSpec, handler: A) -> Selfwhere
A: DynAction,
pub fn action<A>(self, action: ActionSpec, handler: A) -> Selfwhere
A: DynAction,
原子注册 Action 定义与其唯一运行时 Handler。
Sourcepub fn dyn_action(self, action: ActionSpec, handler: Arc<dyn DynAction>) -> Self
pub fn dyn_action(self, action: ActionSpec, handler: Arc<dyn DynAction>) -> Self
原子注册已经类型擦除的 Action Handler。
Sourcepub fn middleware<M>(self, middleware: M) -> Selfwhere
M: Middleware,
pub fn middleware<M>(self, middleware: M) -> Selfwhere
M: Middleware,
增加一个模块级中间件。
多次调用按注册顺序构成洋葱链:先注册的中间件位于更外层,请求阶段
先于后注册的中间件执行(响应阶段则相反)。因此依赖上游注入状态的中间件
必须后注册——例如认证类中间件(如 TokenAuthMiddleware)必须先于
TenantResolverMiddleware
注册,租户 resolver 才能从 ActionContext
读到已认证用户。框架会在构建期拒绝这两个内置中间件的反向顺序;自定义
中间件仍应通过 Middleware::role 声明已知角色。
Sourcepub fn default_permissions<I, S>(
self,
permissions: I,
mode: PermissionMode,
) -> Self
pub fn default_permissions<I, S>( self, permissions: I, mode: PermissionMode, ) -> Self
设置模块默认权限。
Sourcepub fn actions(&self) -> &[ActionSpec]
pub fn actions(&self) -> &[ActionSpec]
返回按局部名称稳定排序前的 Action 定义。
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for ModuleSpec
impl !UnwindSafe for ModuleSpec
impl Forget for ModuleSpec
impl Freeze for ModuleSpec
impl Move for ModuleSpec
impl Send for ModuleSpec
impl Sync for ModuleSpec
impl Unpin for ModuleSpec
impl UnsafeUnpin for ModuleSpec
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more