1use std::collections::BTreeMap;
4
5use crate::error::{DiagCode, Diagnostic};
6use crate::span::Span;
7use crate::value::Value;
8
9use super::{extract_list, extract_str, opt_bool, opt_str, prop_bool};
10
11pub const VALID_CONTROL_KINDS: &[&str] = &[
13 "button",
15 "checkbox",
16 "color",
17 "date",
18 "datetime-local",
19 "email",
20 "file",
21 "hidden",
22 "image",
23 "month",
24 "number",
25 "password",
26 "radio",
27 "range",
28 "reset",
29 "search",
30 "submit",
31 "tel",
32 "text",
33 "time",
34 "url",
35 "week",
36 "select",
38 "option",
39 "optgroup",
40 "textarea",
41 "output",
42 "datalist",
43 "fieldset",
44 "legend",
45 "label",
46 "progress",
47 "meter",
48 "dialog",
49 "details",
50];
51
52pub fn call_standard_control(
54 path: &str,
55 args: &[Value],
56 span: Span,
57) -> Result<Option<Value>, Diagnostic> {
58 match path {
59 "ui.patch" => {
60 let target_id = extract_str(args, 0, "ui.patch(target_id, property, value)", span)?;
61 let property = extract_str(args, 1, "ui.patch(target_id, property, value)", span)?;
62 let value = args.get(2).cloned().unwrap_or(Value::Null);
63 let mut rec = BTreeMap::new();
64 rec.insert("op".into(), Value::String("ui.patch".into()));
65 rec.insert("target_id".into(), Value::String(target_id));
66 rec.insert("property".into(), Value::String(property));
67 rec.insert("value".into(), value);
68 Ok(Some(Value::Record(rec)))
69 }
70 "ui.patch_batch" => {
71 let patches = extract_list(args, 0, "ui.patch_batch(patches)", span)?;
72 let count = patches.len() as i64;
73 let mut rec = BTreeMap::new();
74 rec.insert("op".into(), Value::String("ui.patch_batch".into()));
75 rec.insert("count".into(), Value::I64(count));
76 rec.insert("patches".into(), Value::List(patches));
77 Ok(Some(Value::Record(rec)))
78 }
79 "ui.select_persona" => {
80 let persona = extract_str(args, 0, "ui.select_persona(persona, page)", span)?;
81 let page = extract_str(args, 1, "ui.select_persona(persona, page)", span)?;
82 let badge = match persona.as_str() {
83 "novice" => "Active View: 1. Novice (Guided)",
84 "geek" => "Active View: 2. Tech Geek (Sysadmin/Net)",
85 "dev" => "Active View: 3. Developer (SPARQL/ABI)",
86 _ => "Active View: All 3 Personas Stacked",
87 };
88 let mut rec = BTreeMap::new();
89 rec.insert("persona".into(), Value::String(persona));
90 rec.insert("page".into(), Value::String(page));
91 rec.insert("badge".into(), Value::String(badge.into()));
92 rec.insert("runtime".into(), Value::String("vibescript-0.2".into()));
93 rec.insert(
94 "wire_format".into(),
95 Value::String("application/cbor-ld".into()),
96 );
97 Ok(Some(Value::Record(rec)))
98 }
99 "ui.control" => {
100 let id = extract_str(args, 0, "ui.control(id, kind, props)", span)?;
101 let kind = extract_str(args, 1, "ui.control(id, kind, props)", span)?;
102 if !VALID_CONTROL_KINDS.contains(&kind.as_str()) {
103 return Err(Diagnostic::new(
104 DiagCode::E100,
105 span,
106 format!("ui.control: unsupported control kind '{kind}'"),
107 ));
108 }
109 let props = match args.get(2) {
110 Some(Value::Record(m)) => m.clone(),
111 None | Some(Value::Null) => BTreeMap::new(),
112 _ => {
113 return Err(Diagnostic::new(
114 DiagCode::E100,
115 span,
116 "ui.control(id, kind, props): argument #2 must be a Record",
117 ))
118 }
119 };
120 let disabled = prop_bool(&props, "disabled", false);
121 let read_only =
122 prop_bool(&props, "read_only", prop_bool(&props, "readOnly", false));
123 let required = prop_bool(&props, "required", false);
124 let value = props
125 .get("value")
126 .cloned()
127 .unwrap_or_else(|| Value::String(String::new()));
128
129 let mut rec = BTreeMap::new();
130 rec.insert("id".into(), Value::String(id));
131 rec.insert("kind".into(), Value::String(kind));
132 rec.insert("disabled".into(), Value::Bool(disabled));
133 rec.insert("read_only".into(), Value::Bool(read_only));
134 rec.insert("required".into(), Value::Bool(required));
135 rec.insert("value".into(), value);
136 rec.insert("props".into(), Value::Record(props));
137 Ok(Some(Value::Record(rec)))
138 }
139 "ui.set_tri_state" => {
140 let field_id = extract_str(args, 0, "ui.set_tri_state(field_id, state)", span)?;
141 let state = extract_str(args, 1, "ui.set_tri_state(field_id, state)", span)?;
142 let (checked, indeterminate, contradiction) = match state.as_str() {
143 "checked" => (true, false, false),
144 "unchecked" => (false, false, false),
145 "indeterminate" => (false, true, false),
146 "contradiction" => (false, false, true),
147 _ => {
148 return Err(Diagnostic::new(
149 DiagCode::E100,
150 span,
151 format!("ui.set_tri_state: invalid state '{state}'"),
152 ))
153 }
154 };
155 let mut rec = BTreeMap::new();
156 rec.insert("op".into(), Value::String("ui.set_tri_state".into()));
157 rec.insert("target_id".into(), Value::String(field_id));
158 rec.insert("checked".into(), Value::Bool(checked));
159 rec.insert("indeterminate".into(), Value::Bool(indeterminate));
160 rec.insert("contradiction".into(), Value::Bool(contradiction));
161 rec.insert("state".into(), Value::String(state));
162 Ok(Some(Value::Record(rec)))
163 }
164 "ui.select_set_options" => {
165 let id = extract_str(args, 0, "ui.select_set_options(id, options)", span)?;
166 let options = extract_list(args, 1, "ui.select_set_options(id, options)", span)?;
167 let count = options.len() as i64;
168 let mut rec = BTreeMap::new();
169 rec.insert("op".into(), Value::String("ui.select_set_options".into()));
170 rec.insert("target_id".into(), Value::String(id));
171 rec.insert("count".into(), Value::I64(count));
172 rec.insert("options".into(), Value::List(options));
173 Ok(Some(Value::Record(rec)))
174 }
175 "ui.select_get_selected" => {
176 let options = extract_list(args, 0, "ui.select_get_selected(options)", span)?;
177 let mut selected = Vec::new();
178 for opt in options {
179 if let Value::Record(map) = opt {
180 if map.get("selected") == Some(&Value::Bool(true)) {
181 selected.push(
182 map.get("value")
183 .cloned()
184 .unwrap_or_else(|| Value::String(String::new())),
185 );
186 }
187 }
188 }
189 Ok(Some(Value::List(selected)))
190 }
191 "ui.focus" => {
192 let target_id = extract_str(args, 0, "ui.focus(target_id, ...)", span)?;
193 let prevent_scroll = opt_bool(args, 1, false);
194 let focus_visible = opt_bool(args, 2, true);
195 let mut rec = BTreeMap::new();
196 rec.insert("op".into(), Value::String("ui.focus".into()));
197 rec.insert("target_id".into(), Value::String(target_id));
198 rec.insert("prevent_scroll".into(), Value::Bool(prevent_scroll));
199 rec.insert("focus_visible".into(), Value::Bool(focus_visible));
200 Ok(Some(Value::Record(rec)))
201 }
202 "ui.blur" => {
203 let target_id = extract_str(args, 0, "ui.blur(target_id)", span)?;
204 let mut rec = BTreeMap::new();
205 rec.insert("op".into(), Value::String("ui.blur".into()));
206 rec.insert("target_id".into(), Value::String(target_id));
207 Ok(Some(Value::Record(rec)))
208 }
209 "ui.focus_next" => {
210 let items = extract_list(args, 0, "ui.focus_next(items, current_id, reverse)", span)?;
211 let current_id =
212 extract_str(args, 1, "ui.focus_next(items, current_id, reverse)", span)?;
213 let reverse = opt_bool(args, 2, false);
214 let mut enabled = Vec::new();
215 for item in &items {
216 match item {
217 Value::Record(m) => {
218 let disabled = m.get("disabled") == Some(&Value::Bool(true))
219 || m.get("inert") == Some(&Value::Bool(true));
220 if !disabled {
221 if let Some(Value::String(id)) = m.get("id") {
222 if !id.is_empty() {
223 enabled.push(id.clone());
224 }
225 }
226 }
227 }
228 Value::String(id) if !id.is_empty() => enabled.push(id.clone()),
229 _ => {}
230 }
231 }
232 if enabled.is_empty() {
233 return Ok(Some(Value::String(String::new())));
234 }
235 let pos = enabled.iter().position(|id| id == ¤t_id);
236 let next_idx = match (pos, reverse) {
237 (Some(i), false) => (i + 1) % enabled.len(),
238 (Some(i), true) => (i + enabled.len() - 1) % enabled.len(),
239 (None, false) => 0,
240 (None, true) => enabled.len() - 1,
241 };
242 Ok(Some(Value::String(enabled[next_idx].clone())))
243 }
244 "ui.dialog_open" => {
245 let id = extract_str(args, 0, "ui.dialog_open(id, modal)", span)?;
246 let modal = opt_bool(args, 1, true);
247 let mut rec = BTreeMap::new();
248 rec.insert("op".into(), Value::String("ui.dialog_open".into()));
249 rec.insert("target_id".into(), Value::String(id));
250 rec.insert("open".into(), Value::Bool(true));
251 rec.insert("modal".into(), Value::Bool(modal));
252 rec.insert("top_layer".into(), Value::Bool(true));
253 rec.insert("background_inert".into(), Value::Bool(modal));
254 Ok(Some(Value::Record(rec)))
255 }
256 "ui.dialog_close" => {
257 let id = extract_str(args, 0, "ui.dialog_close(id, return_value)", span)?;
258 let return_value = args
259 .get(1)
260 .cloned()
261 .unwrap_or_else(|| Value::String(String::new()));
262 let mut rec = BTreeMap::new();
263 rec.insert("op".into(), Value::String("ui.dialog_close".into()));
264 rec.insert("target_id".into(), Value::String(id));
265 rec.insert("open".into(), Value::Bool(false));
266 rec.insert("return_value".into(), return_value);
267 Ok(Some(Value::Record(rec)))
268 }
269 "ui.popover_toggle" => {
270 let id = extract_str(args, 0, "ui.popover_toggle(id, action)", span)?;
271 let action = opt_str(args, 1, "toggle");
272 let open = match action.as_str() {
273 "show" | "open" | "toggle" => true,
274 "hide" | "close" => false,
275 _ => {
276 return Err(Diagnostic::new(
277 DiagCode::E100,
278 span,
279 format!("ui.popover_toggle: invalid action '{action}'"),
280 ))
281 }
282 };
283 let mut rec = BTreeMap::new();
284 rec.insert("op".into(), Value::String("ui.popover_toggle".into()));
285 rec.insert("target_id".into(), Value::String(id));
286 rec.insert("action".into(), Value::String(action));
287 rec.insert("open".into(), Value::Bool(open));
288 rec.insert("top_layer".into(), Value::Bool(open));
289 Ok(Some(Value::Record(rec)))
290 }
291 "ui.accordion_toggle" => {
292 let group_name =
293 extract_str(args, 0, "ui.accordion_toggle(group, panel, open)", span)?;
294 let panel_id = extract_str(args, 1, "ui.accordion_toggle(group, panel, open)", span)?;
295 let open = opt_bool(args, 2, true);
296 let mut rec = BTreeMap::new();
297 rec.insert("op".into(), Value::String("ui.accordion_toggle".into()));
298 rec.insert("group_name".into(), Value::String(group_name));
299 rec.insert("active_panel_id".into(), Value::String(panel_id));
300 rec.insert("open".into(), Value::Bool(open));
301 rec.insert("close_siblings".into(), Value::Bool(open));
302 Ok(Some(Value::Record(rec)))
303 }
304 "ui.show_picker" => {
305 let target_id = extract_str(args, 0, "ui.show_picker(target_id)", span)?;
306 let mut rec = BTreeMap::new();
307 rec.insert("op".into(), Value::String("ui.show_picker".into()));
308 rec.insert("target_id".into(), Value::String(target_id));
309 Ok(Some(Value::Record(rec)))
310 }
311 _ => Ok(None),
312 }
313}