vibe/web_runtime/ui_controls/
mod.rs1pub mod composite;
7pub mod standard;
8
9use std::collections::BTreeMap;
10
11use crate::error::{DiagCode, Diagnostic};
12use crate::span::Span;
13use crate::value::Value;
14
15pub use standard::VALID_CONTROL_KINDS;
16
17pub fn call_ui_controls(
19 path: &str,
20 args: &[Value],
21 span: Span,
22) -> Result<Option<Value>, Diagnostic> {
23 if let Some(val) = standard::call_standard_control(path, args, span)? {
24 return Ok(Some(val));
25 }
26 composite::call_composite_control(path, args, span)
27}
28
29pub(crate) fn prop_bool(props: &BTreeMap<String, Value>, key: &str, default: bool) -> bool {
30 match props.get(key) {
31 Some(Value::Bool(b)) => *b,
32 _ => default,
33 }
34}
35
36pub(crate) fn opt_bool(args: &[Value], idx: usize, default: bool) -> bool {
37 match args.get(idx) {
38 Some(Value::Bool(b)) => *b,
39 _ => default,
40 }
41}
42
43pub(crate) fn opt_str(args: &[Value], idx: usize, default: &str) -> String {
44 match args.get(idx) {
45 Some(Value::String(s)) | Some(Value::Iri(s)) => s.clone(),
46 _ => default.to_string(),
47 }
48}
49
50pub(crate) fn opt_i64(args: &[Value], idx: usize, default: i64) -> i64 {
51 args.get(idx).and_then(Value::as_i64).unwrap_or(default)
52}
53
54pub(crate) fn opt_f64(args: &[Value], idx: usize, default: f64) -> f64 {
55 args.get(idx).and_then(Value::as_f64).unwrap_or(default)
56}
57
58pub(crate) fn extract_str(
59 args: &[Value],
60 idx: usize,
61 ctx: &str,
62 span: Span,
63) -> Result<String, Diagnostic> {
64 match args.get(idx) {
65 Some(Value::String(s)) | Some(Value::Iri(s)) => Ok(s.clone()),
66 _ => Err(Diagnostic::new(
67 DiagCode::E100,
68 span,
69 format!("{ctx}: argument #{idx} must be a string"),
70 )),
71 }
72}
73
74pub(crate) fn extract_i64(
75 args: &[Value],
76 idx: usize,
77 ctx: &str,
78 span: Span,
79) -> Result<i64, Diagnostic> {
80 args.get(idx).and_then(Value::as_i64).ok_or_else(|| {
81 Diagnostic::new(
82 DiagCode::E100,
83 span,
84 format!("{ctx}: argument #{idx} must be an integer"),
85 )
86 })
87}
88
89pub(crate) fn extract_f64(
90 args: &[Value],
91 idx: usize,
92 ctx: &str,
93 span: Span,
94) -> Result<f64, Diagnostic> {
95 args.get(idx).and_then(Value::as_f64).ok_or_else(|| {
96 Diagnostic::new(
97 DiagCode::E100,
98 span,
99 format!("{ctx}: argument #{idx} must be a number"),
100 )
101 })
102}
103
104pub(crate) fn extract_list(
105 args: &[Value],
106 idx: usize,
107 ctx: &str,
108 span: Span,
109) -> Result<Vec<Value>, Diagnostic> {
110 match args.get(idx) {
111 Some(Value::List(items)) => Ok(items.clone()),
112 _ => Err(Diagnostic::new(
113 DiagCode::E100,
114 span,
115 format!("{ctx}: argument #{idx} must be a List"),
116 )),
117 }
118}