Skip to main content

yang_base/definition/ui/
module.rs

1//! 账户身份与 Module 页面的构建期展示声明及请求级投影契约。
2
3use super::action::{ActionPresentationSchema, ActionPresentationSpec};
4use crate::definition::ActionRef;
5use schemars::JsonSchema;
6use serde::Serialize;
7use std::collections::BTreeMap;
8
9/// 一个可切换账户身份的构建期展示声明。
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct AccountIdentitySpec {
12    /// 跨模块稳定身份标识。
13    pub id: String,
14    /// 用户可见标题。
15    pub title: String,
16    /// 前端语义图标 token,不是组件或文件路径。
17    pub icon: String,
18    /// 身份切换器中的稳定顺序。
19    pub order: i32,
20}
21
22impl AccountIdentitySpec {
23    /// 创建账户身份展示声明。
24    pub fn new(id: impl Into<String>, title: impl Into<String>, icon: impl Into<String>) -> Self {
25        Self {
26            id: id.into(),
27            title: title.into(),
28            icon: icon.into(),
29            order: 0,
30        }
31    }
32
33    /// 设置身份切换器中的稳定顺序。
34    #[must_use]
35    pub fn order(mut self, order: i32) -> Self {
36        self.order = order;
37        self
38    }
39}
40
41/// Module 的构建期展示声明。
42///
43/// 页面主 Action 与附加 Action 均使用强类型 [`ActionRef`];前端只消费投影后的
44/// operation id,不再从 Action 名称后缀猜测页面语义。
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct ModulePresentationSpec {
47    /// Module 所属账户身份。
48    pub identity: AccountIdentitySpec,
49    /// 用户可见标题。
50    pub title: String,
51    /// Module 业务说明。
52    pub description: String,
53    /// 前端语义图标 token。
54    pub icon: String,
55    /// 同一身份下的稳定顺序。
56    pub order: i32,
57    /// 站内入口路由,缺省使用通用 Module 页面。
58    pub app_route: Option<String>,
59    /// 可选页面主 Action。
60    pub primary_action: Option<ActionRef>,
61    /// 不属于 TableView 的页面级 Action 展示语义。
62    pub action_presentations: BTreeMap<ActionRef, ActionPresentationSpec>,
63}
64
65impl ModulePresentationSpec {
66    /// 创建 Module 展示声明。
67    pub fn new(
68        identity: AccountIdentitySpec,
69        title: impl Into<String>,
70        icon: impl Into<String>,
71    ) -> Self {
72        Self {
73            identity,
74            title: title.into(),
75            description: String::new(),
76            icon: icon.into(),
77            order: 0,
78            app_route: None,
79            primary_action: None,
80            action_presentations: BTreeMap::new(),
81        }
82    }
83
84    /// 设置 Module 业务说明。
85    #[must_use]
86    pub fn description(mut self, description: impl Into<String>) -> Self {
87        self.description = description.into();
88        self
89    }
90
91    /// 设置同一身份下的稳定顺序。
92    #[must_use]
93    pub fn order(mut self, order: i32) -> Self {
94        self.order = order;
95        self
96    }
97
98    /// 设置站内入口路由。
99    #[must_use]
100    pub fn app_route(mut self, route: impl Into<String>) -> Self {
101        self.app_route = Some(route.into());
102        self
103    }
104
105    /// 设置页面主 Action。
106    #[must_use]
107    pub fn primary_action(mut self, action: ActionRef) -> Self {
108        self.primary_action = Some(action);
109        self
110    }
111
112    /// 声明一个页面级 Action 的展示语义。
113    #[must_use]
114    pub fn present_action(
115        mut self,
116        action: ActionRef,
117        presentation: ActionPresentationSpec,
118    ) -> Self {
119        self.action_presentations.insert(action, presentation);
120        self
121    }
122}
123
124/// 请求级账户身份展示契约。
125#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
126pub struct AccountIdentitySchema {
127    /// 跨模块稳定身份标识。
128    pub id: String,
129    /// 用户可见标题。
130    pub title: String,
131    /// 前端语义图标 token。
132    pub icon: String,
133    /// 身份切换器中的稳定顺序。
134    pub order: i32,
135}
136
137/// 请求级 Module 页面展示契约。
138#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
139pub struct ModulePresentationSchema {
140    /// 全限定 Module ID。
141    pub module_id: String,
142    /// Module 所属账户身份。
143    pub identity: AccountIdentitySchema,
144    /// 用户可见标题。
145    pub title: String,
146    /// Module 业务说明。
147    pub description: String,
148    /// 前端语义图标 token。
149    pub icon: String,
150    /// 同一身份下的稳定顺序。
151    pub order: i32,
152    /// 站内入口路由,缺省使用通用 Module 页面。
153    pub app_route: Option<String>,
154    /// 当前请求有权访问的页面主 Action。
155    pub primary_action: Option<String>,
156    /// 当前请求有权访问的页面级 Actions。
157    pub actions: Vec<String>,
158    /// 页面级 Action 的显式展示语义。
159    pub action_presentations: Vec<ActionPresentationSchema>,
160    /// 当前请求有权访问且归属此 Module 的 TableView IDs。
161    pub views: Vec<String>,
162}