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    /// 可选页面主 Action。
58    pub primary_action: Option<ActionRef>,
59    /// 不属于 TableView 的页面级 Action 展示语义。
60    pub action_presentations: BTreeMap<ActionRef, ActionPresentationSpec>,
61}
62
63impl ModulePresentationSpec {
64    /// 创建 Module 展示声明。
65    pub fn new(
66        identity: AccountIdentitySpec,
67        title: impl Into<String>,
68        icon: impl Into<String>,
69    ) -> Self {
70        Self {
71            identity,
72            title: title.into(),
73            description: String::new(),
74            icon: icon.into(),
75            order: 0,
76            primary_action: None,
77            action_presentations: BTreeMap::new(),
78        }
79    }
80
81    /// 设置 Module 业务说明。
82    #[must_use]
83    pub fn description(mut self, description: impl Into<String>) -> Self {
84        self.description = description.into();
85        self
86    }
87
88    /// 设置同一身份下的稳定顺序。
89    #[must_use]
90    pub fn order(mut self, order: i32) -> Self {
91        self.order = order;
92        self
93    }
94
95    /// 设置页面主 Action。
96    #[must_use]
97    pub fn primary_action(mut self, action: ActionRef) -> Self {
98        self.primary_action = Some(action);
99        self
100    }
101
102    /// 声明一个页面级 Action 的展示语义。
103    #[must_use]
104    pub fn present_action(
105        mut self,
106        action: ActionRef,
107        presentation: ActionPresentationSpec,
108    ) -> Self {
109        self.action_presentations.insert(action, presentation);
110        self
111    }
112}
113
114/// 请求级账户身份展示契约。
115#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
116pub struct AccountIdentitySchema {
117    /// 跨模块稳定身份标识。
118    pub id: String,
119    /// 用户可见标题。
120    pub title: String,
121    /// 前端语义图标 token。
122    pub icon: String,
123    /// 身份切换器中的稳定顺序。
124    pub order: i32,
125}
126
127/// 请求级 Module 页面展示契约。
128#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
129pub struct ModulePresentationSchema {
130    /// 全限定 Module ID。
131    pub module_id: String,
132    /// Module 所属账户身份。
133    pub identity: AccountIdentitySchema,
134    /// 用户可见标题。
135    pub title: String,
136    /// Module 业务说明。
137    pub description: String,
138    /// 前端语义图标 token。
139    pub icon: String,
140    /// 同一身份下的稳定顺序。
141    pub order: i32,
142    /// 当前请求有权访问的页面主 Action。
143    pub primary_action: Option<String>,
144    /// 当前请求有权访问的页面级 Actions。
145    pub actions: Vec<String>,
146    /// 页面级 Action 的显式展示语义。
147    pub action_presentations: Vec<ActionPresentationSchema>,
148    /// 当前请求有权访问且归属此 Module 的 TableView IDs。
149    pub views: Vec<String>,
150}