1use serde::{Deserialize, Serialize};
6
7use crate::common::{DynamicBoolean, DynamicNumber, DynamicString, FunctionCall, ReturnType};
8
9pub 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
22pub 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
34pub 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
50pub 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
66pub 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
75pub 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
88pub 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
108pub 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
132pub 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#[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 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
179pub 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
207pub 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
220pub 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
229pub 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
238pub 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}