1#[path = "web_runtime/form_state/mod.rs"]
15pub mod form_state;
16#[path = "web_runtime/input_format/mod.rs"]
17pub mod input_format;
18#[path = "web_runtime/layout_virtual.rs"]
19pub mod layout_virtual;
20#[path = "web_runtime/ui_controls/mod.rs"]
21pub mod ui_controls;
22#[path = "web_runtime/validation.rs"]
23pub mod validation;
24#[path = "web_runtime/web.rs"]
25pub mod web;
26
27use std::collections::BTreeMap;
28
29use crate::cbor_ast::cbor_ld::{
30 call_cbor_ld, decode_value_cbor_ld, encode_value_cbor_ld, DEFAULT_CONTEXT_IRI,
31};
32use crate::error::{DiagCode, Diagnostic};
33use crate::span::Span;
34use crate::value::Value;
35
36pub use web::ZERO_JS_CSP_HEADER;
37
38pub fn call_web_or_ui(
40 path: &str,
41 args: &[Value],
42 span: Span,
43) -> Result<Option<Value>, Diagnostic> {
44 if let Some(v) = call_cbor_ld(path, args, span)? {
45 return Ok(Some(v));
46 }
47 if let Some(v) = web::call_web(path, args, span)? {
48 return Ok(Some(v));
49 }
50 if let Some(v) = ui_controls::call_ui_controls(path, args, span)? {
51 return Ok(Some(v));
52 }
53 if let Some(v) = layout_virtual::call_layout_virtual(path, args, span)? {
54 return Ok(Some(v));
55 }
56 if let Some(v) = form_state::call_form_state(path, args, span)? {
57 return Ok(Some(v));
58 }
59 if let Some(v) = input_format::call_input_format(path, args, span)? {
60 return Ok(Some(v));
61 }
62 if let Some(v) = validation::call_validation(path, args, span)? {
63 return Ok(Some(v));
64 }
65
66 match path {
68 "ui.patch_to_cbor_ld" => {
69 let patches = args.first().ok_or_else(|| {
70 Diagnostic::new(DiagCode::E100, span, "ui.patch_to_cbor_ld requires patches")
71 })?;
72 let ctx = match args.get(1) {
73 Some(Value::String(s)) | Some(Value::Iri(s)) => s.as_str(),
74 _ => "https://qualia.world/context/ui/v1",
75 };
76 call_cbor_ld(
77 "cbor_ld.encode",
78 &[patches.clone(), Value::Iri(ctx.to_string())],
79 span,
80 )
81 }
82 "ui.projectional_editor" => {
83 let id = match args.first() {
84 Some(Value::String(s)) => s.clone(),
85 _ => {
86 return Err(Diagnostic::new(
87 DiagCode::E100,
88 span,
89 "ui.projectional_editor requires control id",
90 ))
91 }
92 };
93 let src = match args.get(1) {
94 Some(Value::String(s)) => s.clone(),
95 _ => String::new(),
96 };
97 let prog = crate::parse_program(&src)?;
98 let projected = crate::project_program(&prog, &crate::ProjectOptions::default());
99 let ast_cbor = crate::cbor_ast::encode(&prog);
100 let mut rec = BTreeMap::new();
101 rec.insert("id".to_string(), Value::String(id));
102 rec.insert(
103 "kind".to_string(),
104 Value::String("projectional-editor".to_string()),
105 );
106 rec.insert("projected_source".to_string(), Value::String(projected));
107 rec.insert(
108 "ast_cbor_tag".to_string(),
109 Value::I64(crate::cbor_ast::TAG_VIBE_AST as i64),
110 );
111 rec.insert(
112 "ast_cbor_byte_len".to_string(),
113 Value::I64(ast_cbor.len() as i64),
114 );
115 Ok(Some(Value::Record(rec)))
116 }
117 "ui.dag_canvas" => {
118 let id = match args.first() {
119 Some(Value::String(s)) => s.clone(),
120 _ => {
121 return Err(Diagnostic::new(
122 DiagCode::E100,
123 span,
124 "ui.dag_canvas requires control id",
125 ))
126 }
127 };
128 let nodes = match args.get(1) {
129 Some(Value::List(xs)) => xs.clone(),
130 _ => Vec::new(),
131 };
132 let edges = match args.get(2) {
133 Some(Value::List(xs)) => xs.clone(),
134 _ => Vec::new(),
135 };
136 let mut rec = BTreeMap::new();
137 rec.insert("id".to_string(), Value::String(id));
138 rec.insert("kind".to_string(), Value::String("dag-canvas".to_string()));
139 rec.insert("node_count".to_string(), Value::I64(nodes.len() as i64));
140 rec.insert("edge_count".to_string(), Value::I64(edges.len() as i64));
141 rec.insert("nodes".to_string(), Value::List(nodes));
142 rec.insert("edges".to_string(), Value::List(edges));
143 Ok(Some(Value::Record(rec)))
144 }
145 "form.to_cbor_ld" => {
146 let form = args.first().ok_or_else(|| {
147 Diagnostic::new(DiagCode::E100, span, "form.to_cbor_ld requires a form record")
148 })?;
149 let ctx = match args.get(1) {
150 Some(Value::String(s)) | Some(Value::Iri(s)) => s.as_str(),
151 _ => {
152 if let Value::Record(r) = form {
153 match r.get("schema_iri") {
154 Some(Value::Iri(s)) | Some(Value::String(s)) => s.as_str(),
155 _ => DEFAULT_CONTEXT_IRI,
156 }
157 } else {
158 DEFAULT_CONTEXT_IRI
159 }
160 }
161 };
162 call_cbor_ld(
163 "cbor_ld.encode",
164 &[form.clone(), Value::Iri(ctx.to_string())],
165 span,
166 )
167 }
168 "form.from_cbor_ld" => call_cbor_ld("cbor_ld.decode", args, span),
169 "form.sign_submission" => {
170 let form = args.first().ok_or_else(|| {
171 Diagnostic::new(
172 DiagCode::E100,
173 span,
174 "form.sign_submission requires a form record",
175 )
176 })?;
177 let ctx = match args.get(1) {
178 Some(Value::String(s)) | Some(Value::Iri(s)) => s.as_str(),
179 _ => DEFAULT_CONTEXT_IRI,
180 };
181 let signer_did = match args.get(2) {
182 Some(Value::String(s)) | Some(Value::Iri(s)) => s.clone(),
183 _ => "did:q42:local-signer".to_string(),
184 };
185 let cbor_bytes = encode_value_cbor_ld(form, ctx);
186 let digest = form_state::fnv1a_64(&cbor_bytes);
187 let (_, roundtrip) = decode_value_cbor_ld(&cbor_bytes).map_err(|e| {
188 Diagnostic::new(DiagCode::E600, span, format!("CBOR-LD verify error: {e}"))
189 })?;
190 let deterministic = encode_value_cbor_ld(&roundtrip, ctx) == cbor_bytes;
191
192 let mut rec = BTreeMap::new();
193 rec.insert("signed".to_string(), Value::Bool(true));
194 rec.insert("signer_did".to_string(), Value::Iri(signer_did));
195 rec.insert("context".to_string(), Value::Iri(ctx.to_string()));
196 rec.insert(
197 "cbor_ld_byte_len".to_string(),
198 Value::I64(cbor_bytes.len() as i64),
199 );
200 rec.insert(
201 "canonical_digest_hex".to_string(),
202 Value::String(format!("0x{digest:016x}")),
203 );
204 rec.insert("deterministic_cde".to_string(), Value::Bool(deterministic));
205 Ok(Some(Value::Record(rec)))
206 }
207 _ => Ok(None),
208 }
209}
210
211#[cfg(test)]
212mod tests;