a2ui_types/v09/common_types.rs
1use serde::{Deserialize, Serialize};
2
3use crate::common::ComponentId;
4
5// ---------------------------------------------------------------------------
6// Dynamic value types — the core of v0.9 data binding
7// ---------------------------------------------------------------------------
8
9/// A data binding reference to the data model via JSON Pointer.
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11#[serde(deny_unknown_fields)]
12pub struct DataBinding {
13 /// A JSON Pointer path to a value in the data model.
14 pub path: String,
15}
16
17/// Invokes a named function on the client.
18#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
19pub struct FunctionCall {
20 /// The name of the function to call.
21 pub call: String,
22
23 /// Arguments passed to the function.
24 #[serde(skip_serializing_if = "Option::is_none")]
25 pub args: Option<serde_json::Value>,
26
27 /// The expected return type of the function call.
28 #[serde(rename = "returnType", skip_serializing_if = "Option::is_none")]
29 pub return_type: Option<FunctionReturnType>,
30}
31
32/// Possible return types for a function call.
33#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34#[serde(rename_all = "lowercase")]
35pub enum FunctionReturnType {
36 String,
37 Number,
38 Boolean,
39 Array,
40 Object,
41 Any,
42 Void,
43}
44
45/// A string that can be a literal, a data binding, or a function call returning a string.
46#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
47#[serde(untagged)]
48pub enum DynamicString {
49 /// A static string value.
50 Literal(String),
51 /// A data binding to the data model.
52 Binding(DataBinding),
53 /// A function call returning a string.
54 Function(FunctionCall),
55}
56
57/// A number that can be literal, data-bound, or a function call.
58#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
59#[serde(untagged)]
60pub enum DynamicNumber {
61 /// A static number value.
62 Literal(f64),
63 /// A data binding to the data model.
64 Binding(DataBinding),
65 /// A function call returning a number.
66 Function(FunctionCall),
67}
68
69/// A boolean that can be literal, data-bound, or a function call.
70#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
71#[serde(untagged)]
72pub enum DynamicBoolean {
73 /// A static boolean value.
74 Literal(bool),
75 /// A data binding to the data model.
76 Binding(DataBinding),
77 /// A function call returning a boolean.
78 Function(FunctionCall),
79}
80
81/// A string list that can be literal, data-bound, or a function call.
82#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
83#[serde(untagged)]
84pub enum DynamicStringList {
85 /// A static list of strings.
86 Literal(Vec<String>),
87 /// A data binding to the data model.
88 Binding(DataBinding),
89 /// A function call returning a string list.
90 Function(FunctionCall),
91}
92
93/// A value that can be any literal, a data binding, or a function call.
94#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
95#[serde(untagged)]
96pub enum DynamicValue {
97 /// A string literal.
98 String(String),
99 /// A number literal.
100 Number(f64),
101 /// A boolean literal.
102 Bool(bool),
103 /// An array literal.
104 Array(Vec<serde_json::Value>),
105 /// A data binding to the data model.
106 Binding(DataBinding),
107 /// A function call.
108 Function(FunctionCall),
109}
110
111// ---------------------------------------------------------------------------
112// Child list — how containers hold children
113// ---------------------------------------------------------------------------
114
115/// A template for generating children from a data model list.
116#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
117#[serde(deny_unknown_fields)]
118pub struct ChildListTemplate {
119 /// The component to use as a template for each item.
120 #[serde(rename = "componentId")]
121 pub component_id: ComponentId,
122
123 /// The path to the list in the data model.
124 pub path: String,
125}
126
127/// Defines how a container holds its children.
128/// Either a static array of component IDs, or a template for dynamic lists.
129#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
130#[serde(untagged)]
131pub enum ChildList {
132 /// A static list of child component IDs.
133 Static(Vec<ComponentId>),
134 /// A template for generating children from a data model list.
135 Template(ChildListTemplate),
136}
137
138// ---------------------------------------------------------------------------
139// Actions
140// ---------------------------------------------------------------------------
141
142/// An event to dispatch to the server.
143#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
144pub struct ActionEvent {
145 /// The name of the action to dispatch.
146 pub name: String,
147
148 /// Key-value pairs for the action context.
149 #[serde(skip_serializing_if = "Option::is_none")]
150 pub context: Option<serde_json::Value>,
151}
152
153/// Defines an interaction handler: either a server-side event or a local function call.
154#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
155#[serde(deny_unknown_fields)]
156pub struct Action {
157 /// Triggers a server-side event.
158 #[serde(skip_serializing_if = "Option::is_none")]
159 pub event: Option<ActionEvent>,
160
161 /// Executes a local client-side function.
162 #[serde(rename = "functionCall", skip_serializing_if = "Option::is_none")]
163 pub function_call: Option<FunctionCall>,
164}
165
166// ---------------------------------------------------------------------------
167// Checks / Validation
168// ---------------------------------------------------------------------------
169
170/// A single validation rule applied to an input component.
171#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
172#[serde(deny_unknown_fields)]
173pub struct CheckRule {
174 /// The condition to evaluate (must resolve to a boolean).
175 pub condition: DynamicBoolean,
176
177 /// The error message to display if the check fails.
178 pub message: String,
179}
180
181// ---------------------------------------------------------------------------
182// Accessibility
183// ---------------------------------------------------------------------------
184
185/// Attributes for assistive technologies.
186#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
187pub struct AccessibilityAttributes {
188 /// A short label for the element.
189 #[serde(skip_serializing_if = "Option::is_none")]
190 pub label: Option<DynamicString>,
191
192 /// Additional description for the element.
193 #[serde(skip_serializing_if = "Option::is_none")]
194 pub description: Option<DynamicString>,
195}