Skip to main content

aster_a2ui/
common.rs

1//! A2UI 通用类型定义
2//!
3//! 对应 A2UI 规范中的 common_types.json
4
5use serde::{Deserialize, Serialize};
6
7/// 组件唯一标识符
8pub type ComponentId = String;
9
10/// 数据绑定 - 引用数据模型中的值
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
12pub struct DataBinding {
13    /// JSON Pointer 路径
14    pub path: String,
15}
16
17/// 函数调用
18#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
19#[serde(rename_all = "camelCase")]
20pub struct FunctionCall {
21    /// 函数名称
22    pub call: String,
23    /// 函数参数
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub args: Option<serde_json::Value>,
26    /// 返回类型
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub return_type: Option<ReturnType>,
29}
30
31/// 函数返回类型
32#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
33#[serde(rename_all = "lowercase")]
34pub enum ReturnType {
35    String,
36    Number,
37    Boolean,
38    Array,
39    Object,
40    Any,
41    Void,
42}
43
44/// 动态值 - 可以是字面量、数据绑定或函数调用
45#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
46#[serde(untagged)]
47pub enum DynamicValue {
48    /// 字符串字面量
49    String(String),
50    /// 数字字面量
51    Number(f64),
52    /// 布尔字面量
53    Boolean(bool),
54    /// 数组字面量
55    Array(Vec<serde_json::Value>),
56    /// 数据绑定
57    Binding(DataBinding),
58    /// 函数调用
59    Function(FunctionCall),
60}
61
62/// 动态字符串
63#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
64#[serde(untagged)]
65pub enum DynamicString {
66    Literal(String),
67    Binding(DataBinding),
68    Function(FunctionCall),
69}
70
71impl From<&str> for DynamicString {
72    fn from(s: &str) -> Self {
73        DynamicString::Literal(s.to_string())
74    }
75}
76
77impl From<String> for DynamicString {
78    fn from(s: String) -> Self {
79        DynamicString::Literal(s)
80    }
81}
82
83/// 动态数字
84#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
85#[serde(untagged)]
86pub enum DynamicNumber {
87    Literal(f64),
88    Binding(DataBinding),
89    Function(FunctionCall),
90}
91
92impl From<f64> for DynamicNumber {
93    fn from(n: f64) -> Self {
94        DynamicNumber::Literal(n)
95    }
96}
97
98impl From<i32> for DynamicNumber {
99    fn from(n: i32) -> Self {
100        DynamicNumber::Literal(n as f64)
101    }
102}
103
104/// 动态布尔值
105#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
106#[serde(untagged)]
107pub enum DynamicBoolean {
108    Literal(bool),
109    Binding(DataBinding),
110    Function(FunctionCall),
111}
112
113impl From<bool> for DynamicBoolean {
114    fn from(b: bool) -> Self {
115        DynamicBoolean::Literal(b)
116    }
117}
118
119/// 动态字符串列表
120#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
121#[serde(untagged)]
122pub enum DynamicStringList {
123    Literal(Vec<String>),
124    Binding(DataBinding),
125    Function(FunctionCall),
126}
127
128/// 子组件列表 - 静态数组或动态模板
129#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
130#[serde(untagged)]
131pub enum ChildList {
132    /// 静态子组件 ID 列表
133    Static(Vec<ComponentId>),
134    /// 动态模板(从数据模型生成)
135    Template(ChildTemplate),
136}
137
138/// 子组件模板
139#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
140#[serde(rename_all = "camelCase")]
141pub struct ChildTemplate {
142    /// 模板组件 ID
143    pub component_id: ComponentId,
144    /// 数据模型中的列表路径
145    pub path: String,
146}
147
148/// 验证规则
149#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
150pub struct CheckRule {
151    /// 验证条件(必须返回布尔值)
152    pub condition: DynamicBoolean,
153    /// 验证失败时的错误消息
154    pub message: String,
155}
156
157/// 可验证组件的属性
158#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
159pub struct Checkable {
160    /// 验证规则列表
161    #[serde(skip_serializing_if = "Option::is_none")]
162    pub checks: Option<Vec<CheckRule>>,
163}
164
165/// 无障碍属性
166#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
167pub struct AccessibilityAttributes {
168    /// 无障碍标签
169    #[serde(skip_serializing_if = "Option::is_none")]
170    pub label: Option<DynamicString>,
171    /// 无障碍描述
172    #[serde(skip_serializing_if = "Option::is_none")]
173    pub description: Option<DynamicString>,
174}
175
176/// 动作定义 - 服务端事件或客户端函数
177#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
178#[serde(untagged)]
179pub enum Action {
180    /// 服务端事件
181    Event(EventAction),
182    /// 客户端函数调用
183    Function(FunctionAction),
184}
185
186/// 服务端事件动作
187#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
188pub struct EventAction {
189    /// 事件定义
190    pub event: EventDefinition,
191}
192
193/// 事件定义
194#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
195pub struct EventDefinition {
196    /// 事件名称
197    pub name: String,
198    /// 事件上下文(键值对,值可以是动态的)
199    #[serde(skip_serializing_if = "Option::is_none")]
200    pub context: Option<serde_json::Map<String, serde_json::Value>>,
201}
202
203/// 客户端函数动作
204#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
205#[serde(rename_all = "camelCase")]
206pub struct FunctionAction {
207    /// 函数调用
208    pub function_call: FunctionCall,
209}