Skip to main content

aster_a2ui/
functions.rs

1//! A2UI 标准函数定义
2//!
3//! 对应 A2UI 规范中 standard_catalog.json 的 functions 部分
4
5use serde::{Deserialize, Serialize};
6
7use crate::common::{DynamicBoolean, DynamicNumber, DynamicString, FunctionCall, ReturnType};
8
9// ============================================================================
10// 验证函数
11// ============================================================================
12
13/// 创建 required 函数调用
14pub fn required(value: DynamicString) -> FunctionCall {
15    FunctionCall {
16        call: "required".to_string(),
17        args: Some(serde_json::json!({ "value": value })),
18        return_type: Some(ReturnType::Boolean),
19    }
20}
21
22/// 创建 regex 函数调用
23pub fn regex(value: DynamicString, pattern: &str) -> FunctionCall {
24    FunctionCall {
25        call: "regex".to_string(),
26        args: Some(serde_json::json!({
27            "value": value,
28            "pattern": pattern
29        })),
30        return_type: Some(ReturnType::Boolean),
31    }
32}
33
34/// 创建 length 函数调用
35pub fn length(value: DynamicString, min: Option<u32>, max: Option<u32>) -> FunctionCall {
36    let mut args = serde_json::json!({ "value": value });
37    if let Some(min) = min {
38        args["min"] = serde_json::json!(min);
39    }
40    if let Some(max) = max {
41        args["max"] = serde_json::json!(max);
42    }
43    FunctionCall {
44        call: "length".to_string(),
45        args: Some(args),
46        return_type: Some(ReturnType::Boolean),
47    }
48}
49
50/// 创建 numeric 函数调用
51pub fn numeric(value: DynamicNumber, min: Option<f64>, max: Option<f64>) -> FunctionCall {
52    let mut args = serde_json::json!({ "value": value });
53    if let Some(min) = min {
54        args["min"] = serde_json::json!(min);
55    }
56    if let Some(max) = max {
57        args["max"] = serde_json::json!(max);
58    }
59    FunctionCall {
60        call: "numeric".to_string(),
61        args: Some(args),
62        return_type: Some(ReturnType::Boolean),
63    }
64}
65
66/// 创建 email 函数调用
67pub fn email(value: DynamicString) -> FunctionCall {
68    FunctionCall {
69        call: "email".to_string(),
70        args: Some(serde_json::json!({ "value": value })),
71        return_type: Some(ReturnType::Boolean),
72    }
73}
74
75// ============================================================================
76// 格式化函数
77// ============================================================================
78
79/// 创建 formatString 函数调用
80pub fn format_string(value: DynamicString) -> FunctionCall {
81    FunctionCall {
82        call: "formatString".to_string(),
83        args: Some(serde_json::json!({ "value": value })),
84        return_type: Some(ReturnType::String),
85    }
86}
87
88/// 创建 formatNumber 函数调用
89pub fn format_number(
90    value: DynamicNumber,
91    decimals: Option<u32>,
92    grouping: Option<bool>,
93) -> FunctionCall {
94    let mut args = serde_json::json!({ "value": value });
95    if let Some(decimals) = decimals {
96        args["decimals"] = serde_json::json!(decimals);
97    }
98    if let Some(grouping) = grouping {
99        args["grouping"] = serde_json::json!(grouping);
100    }
101    FunctionCall {
102        call: "formatNumber".to_string(),
103        args: Some(args),
104        return_type: Some(ReturnType::String),
105    }
106}
107
108/// 创建 formatCurrency 函数调用
109pub fn format_currency(
110    value: DynamicNumber,
111    currency: &str,
112    decimals: Option<u32>,
113    grouping: Option<bool>,
114) -> FunctionCall {
115    let mut args = serde_json::json!({
116        "value": value,
117        "currency": currency
118    });
119    if let Some(decimals) = decimals {
120        args["decimals"] = serde_json::json!(decimals);
121    }
122    if let Some(grouping) = grouping {
123        args["grouping"] = serde_json::json!(grouping);
124    }
125    FunctionCall {
126        call: "formatCurrency".to_string(),
127        args: Some(args),
128        return_type: Some(ReturnType::String),
129    }
130}
131
132/// 创建 formatDate 函数调用
133///
134/// format 参数使用 Unicode TR35 日期模式:
135/// - 年: 'yy' (26), 'yyyy' (2026)
136/// - 月: 'M' (1), 'MM' (01), 'MMM' (Jan), 'MMMM' (January)
137/// - 日: 'd' (1), 'dd' (01), 'E' (Tue), 'EEEE' (Tuesday)
138/// - 时(12h): 'h' (1-12), 'hh' (01-12)
139/// - 时(24h): 'H' (0-23), 'HH' (00-23)
140/// - 分: 'mm' (00-59)
141/// - 秒: 'ss' (00-59)
142/// - 上下午: 'a' (AM/PM)
143pub fn format_date(value: DynamicString, format: &str) -> FunctionCall {
144    FunctionCall {
145        call: "formatDate".to_string(),
146        args: Some(serde_json::json!({
147            "value": value,
148            "format": format
149        })),
150        return_type: Some(ReturnType::String),
151    }
152}
153
154/// 复数形式参数
155#[derive(Debug, Clone, Serialize, Deserialize)]
156pub struct PluralizeArgs {
157    pub zero: Option<DynamicString>,
158    pub one: Option<DynamicString>,
159    pub two: Option<DynamicString>,
160    pub few: Option<DynamicString>,
161    pub many: Option<DynamicString>,
162    pub other: DynamicString,
163}
164
165impl PluralizeArgs {
166    /// 创建新的复数形式参数
167    pub fn new(other: impl Into<DynamicString>) -> Self {
168        Self {
169            zero: None,
170            one: None,
171            two: None,
172            few: None,
173            many: None,
174            other: other.into(),
175        }
176    }
177}
178
179/// 创建 pluralize 函数调用
180pub fn pluralize(value: DynamicNumber, args: PluralizeArgs) -> FunctionCall {
181    let mut json_args = serde_json::json!({
182        "value": value,
183        "other": args.other
184    });
185    if let Some(zero) = args.zero {
186        json_args["zero"] = serde_json::json!(zero);
187    }
188    if let Some(one) = args.one {
189        json_args["one"] = serde_json::json!(one);
190    }
191    if let Some(two) = args.two {
192        json_args["two"] = serde_json::json!(two);
193    }
194    if let Some(few) = args.few {
195        json_args["few"] = serde_json::json!(few);
196    }
197    if let Some(many) = args.many {
198        json_args["many"] = serde_json::json!(many);
199    }
200    FunctionCall {
201        call: "pluralize".to_string(),
202        args: Some(json_args),
203        return_type: Some(ReturnType::String),
204    }
205}
206
207// ============================================================================
208// 逻辑函数
209// ============================================================================
210
211/// 创建 and 函数调用
212pub fn and(values: Vec<DynamicBoolean>) -> FunctionCall {
213    FunctionCall {
214        call: "and".to_string(),
215        args: Some(serde_json::json!({ "values": values })),
216        return_type: Some(ReturnType::Boolean),
217    }
218}
219
220/// 创建 or 函数调用
221pub fn or(values: Vec<DynamicBoolean>) -> FunctionCall {
222    FunctionCall {
223        call: "or".to_string(),
224        args: Some(serde_json::json!({ "values": values })),
225        return_type: Some(ReturnType::Boolean),
226    }
227}
228
229/// 创建 not 函数调用
230pub fn not(value: DynamicBoolean) -> FunctionCall {
231    FunctionCall {
232        call: "not".to_string(),
233        args: Some(serde_json::json!({ "value": value })),
234        return_type: Some(ReturnType::Boolean),
235    }
236}
237
238// ============================================================================
239// 客户端动作函数
240// ============================================================================
241
242/// 创建 openUrl 函数调用
243pub fn open_url(url: &str) -> FunctionCall {
244    FunctionCall {
245        call: "openUrl".to_string(),
246        args: Some(serde_json::json!({ "url": url })),
247        return_type: Some(ReturnType::Void),
248    }
249}