Skip to main content

ModuleSpec

Struct ModuleSpec 

Source
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>,
    /* 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 页面展示声明。

Implementations§

Source§

impl ModuleSpec

Source

pub fn native_action<A>(self, handler: A) -> Self

在手写 ModuleSpec 路径中原子注册一个原生 Action。

Source

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

Source

pub fn new(name: ModuleName) -> Self

创建空 Module。

Source

pub fn table(self, table: TableSpec) -> Self

设置 Module 主表。

Source

pub fn presentation(self, presentation: ModulePresentationSpec) -> Self

设置前端 Module 页面展示声明。

Source

pub fn crud(self) -> Result<Self, BaseError>

为模块主表注册标准 add/put/del/get/select/table Actions。

Source

pub fn crud_at(self, prefix: impl Into<String>) -> Result<Self, BaseError>

在显式资源路径下注册标准 add/put/del/get/select/table Actions。

用于版本化或资源复数化路由;operation_id 仍始终使用 <module>.<action>,不与 HTTP 路径耦合。

Source

pub fn crud_at_with_mutations<A, P, D>( self, prefix: impl Into<String>, add: A, put: P, del: D, ) -> Result<Self, BaseError>
where A: DynAction, P: DynAction, D: DynAction,

在显式资源路径下注册标准 CRUD 契约,并替换三个写 Action 的运行时 Handler。

路由、权限以及与表定义绑定的动态 JSON Schema 仍由框架统一生成;业务只负责 add/put/del 的事务语义。自定义 Handler 必须分别接受与内置 Action 相同的 Record、 PutInput 与 GetByPk 输入,并返回对应的标准结果。

Source

pub fn action<A>(self, action: ActionSpec, handler: A) -> Self
where A: DynAction,

原子注册 Action 定义与其唯一运行时 Handler。

Source

pub fn dyn_action(self, action: ActionSpec, handler: Arc<dyn DynAction>) -> Self

原子注册已经类型擦除的 Action Handler。

Source

pub fn middleware<M>(self, middleware: M) -> Self
where M: Middleware,

增加一个模块级中间件。

多次调用按注册顺序构成洋葱链:先注册的中间件位于更外层,请求阶段 先于后注册的中间件执行(响应阶段则相反)。因此依赖上游注入状态的中间件 必须后注册——例如认证类中间件(如 TokenAuthMiddleware)必须先于 TenantResolverMiddleware 注册,租户 resolver 才能从 ActionContext 读到已认证用户。框架会在构建期拒绝这两个内置中间件的反向顺序;自定义 中间件仍应通过 Middleware::role 声明已知角色。

Source

pub fn default_permissions<I, S>( self, permissions: I, mode: PermissionMode, ) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

设置模块默认权限。

Source

pub fn actions(&self) -> &[ActionSpec]

返回按局部名称稳定排序前的 Action 定义。

Source

pub fn view(self, view: ViewSpec) -> Self

增加一个 View。

Trait Implementations§

Source§

impl Clone for ModuleSpec

Source§

fn clone(&self) -> ModuleSpec

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ModuleSpec

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ModuleSpec

Source§

impl PartialEq for ModuleSpec

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more