@mun-ui/ui 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +114 -2
- package/bin/compile.mjs +38 -0
- package/bin/desktop.mjs +143 -0
- package/bin/dev-protocol.mjs +94 -0
- package/bin/formatter.mjs +109 -0
- package/bin/hot-reload.mjs +38 -0
- package/bin/mun.mjs +538 -0
- package/bin/native.mjs +133 -0
- package/bin/pnpm-workspace.mjs +58 -0
- package/bin/program-patch.mjs +104 -0
- package/bin/project.mjs +322 -0
- package/bin/watch.mjs +316 -0
- package/bin/workflow.mjs +261 -0
- package/dist/astro.d.ts +3 -0
- package/dist/astro.d.ts.map +1 -0
- package/dist/astro.js +3 -0
- package/dist/astro.js.map +1 -0
- package/dist/closures.d.ts +2 -0
- package/dist/closures.d.ts.map +1 -0
- package/dist/closures.js +2 -0
- package/dist/closures.js.map +1 -0
- package/dist/compiler/index.d.ts +2 -0
- package/dist/compiler/index.d.ts.map +1 -0
- package/dist/compiler/index.js +2 -0
- package/dist/compiler/index.js.map +1 -0
- package/dist/core.d.ts +3 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +3 -0
- package/dist/core.js.map +1 -0
- package/dist/experimental.d.ts +2 -0
- package/dist/experimental.d.ts.map +1 -0
- package/dist/experimental.js +2 -0
- package/dist/experimental.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-dev-runtime.d.ts +2 -0
- package/dist/jsx-dev-runtime.d.ts.map +1 -0
- package/dist/jsx-dev-runtime.js +2 -0
- package/dist/jsx-dev-runtime.js.map +1 -0
- package/dist/jsx-runtime.d.ts +2 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.js +2 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/legacy.d.ts +3 -0
- package/dist/legacy.d.ts.map +1 -0
- package/dist/legacy.js +3 -0
- package/dist/legacy.js.map +1 -0
- package/dist/mun.d.ts +5 -0
- package/dist/mun.d.ts.map +1 -0
- package/dist/mun.js +4 -0
- package/dist/mun.js.map +1 -0
- package/dist/progressive-blur/index.d.ts +8 -0
- package/dist/progressive-blur/index.d.ts.map +1 -0
- package/dist/progressive-blur/index.js +8 -0
- package/dist/progressive-blur/index.js.map +1 -0
- package/dist/progressive-blur/plan.d.ts +3 -0
- package/dist/progressive-blur/plan.d.ts.map +1 -0
- package/dist/progressive-blur/plan.js +3 -0
- package/dist/progressive-blur/plan.js.map +1 -0
- package/dist/react-types.d.ts +2 -0
- package/dist/react-types.d.ts.map +1 -0
- package/dist/react-types.js +2 -0
- package/dist/react-types.js.map +1 -0
- package/dist/rendering-quality.d.ts +4 -0
- package/dist/rendering-quality.d.ts.map +1 -0
- package/dist/rendering-quality.js +4 -0
- package/dist/rendering-quality.js.map +1 -0
- package/dist/state.d.ts +2 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +2 -0
- package/dist/state.js.map +1 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/vite.d.ts +2 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +2 -0
- package/dist/vite.js.map +1 -0
- package/editors/install.mjs +102 -0
- package/editors/lsp/mun-lsp.mjs +123 -0
- package/editors/lsp/service.mjs +306 -0
- package/editors/lsp/source.mjs +58 -0
- package/editors/vscode/client.mjs +40 -0
- package/editors/vscode/dev-client.mjs +83 -0
- package/editors/vscode/discovery.mjs +46 -0
- package/editors/vscode/export.mjs +42 -0
- package/editors/vscode/extension.cjs +226 -0
- package/editors/vscode/language-configuration.json +15 -0
- package/editors/vscode/package.json +119 -0
- package/editors/vscode/protocol.mjs +29 -0
- package/editors/vscode/syntaxes/mun.tmLanguage.json +130 -0
- package/native/Cargo.lock +3581 -0
- package/native/Cargo.toml +21 -0
- package/native/README.md +21 -0
- package/native/mun-native/Cargo.toml +23 -0
- package/native/mun-native/src/accessibility.rs +738 -0
- package/native/mun-native/src/dev.rs +604 -0
- package/native/mun-native/src/host_services.rs +202 -0
- package/native/mun-native/src/lib.rs +2843 -0
- package/native/mun-native/src/main.rs +204 -0
- package/native/mun-native/src/offscreen.rs +393 -0
- package/native/mun-native/src/rect.wgsl +63 -0
- package/native/mun-native/src/text.rs +401 -0
- package/native/mun-native/tests/offscreen_rendering.rs +161 -0
- package/native/mun-native/tests/stress.rs +408 -0
- package/native/mun-native/tests/window_lifecycle.rs +183 -0
- package/native/mun-runtime/Cargo.toml +13 -0
- package/native/mun-runtime/src/accessibility.rs +193 -0
- package/native/mun-runtime/src/collection.rs +1090 -0
- package/native/mun-runtime/src/gesture.rs +1070 -0
- package/native/mun-runtime/src/input.rs +325 -0
- package/native/mun-runtime/src/ir.rs +852 -0
- package/native/mun-runtime/src/kinetics.rs +500 -0
- package/native/mun-runtime/src/layout.rs +258 -0
- package/native/mun-runtime/src/layout_cache.rs +209 -0
- package/native/mun-runtime/src/lib.rs +52 -0
- package/native/mun-runtime/src/motion.rs +1162 -0
- package/native/mun-runtime/src/retained.rs +574 -0
- package/native/mun-runtime/src/runtime.rs +7449 -0
- package/native/mun-runtime/src/scene.rs +567 -0
- package/native/mun-runtime/src/scroll.rs +521 -0
- package/native/mun-runtime/src/scroll_view.rs +220 -0
- package/native/mun-runtime/src/text_edit.rs +670 -0
- package/native/mun-runtime/src/timeline.rs +1534 -0
- package/native/mun-runtime/src/validate.rs +677 -0
- package/native/mun-runtime/tests/accessibility_semantics.rs +168 -0
- package/native/mun-runtime/tests/compiler_contract.rs +110 -0
- package/native/mun-runtime/tests/flexible_frames.rs +47 -0
- package/native/mun-runtime/tests/host_services.rs +107 -0
- package/native/mun-runtime/tests/hot_update.rs +367 -0
- package/native/mun-runtime/tests/incremental_runtime.rs +323 -0
- package/native/mun-runtime/tests/ir_validation.rs +167 -0
- package/native/mun-runtime/tests/keyed_collections.rs +251 -0
- package/native/mun-runtime/tests/minimum_window_size.rs +80 -0
- package/native/mun-runtime/tests/native_controls_style.rs +491 -0
- package/native/mun-runtime/tests/native_demo.rs +285 -0
- package/native/mun-runtime/tests/native_layout_style.rs +175 -0
- package/native/mun-runtime/tests/native_settings_pane.rs +152 -0
- package/native/mun-runtime/tests/priority_proposals.rs +69 -0
- package/native/mun-runtime/tests/scroll_view.rs +403 -0
- package/native/mun-runtime/tests/text_editing.rs +491 -0
- package/native/mun-runtime/tests/text_proposals.rs +42 -0
- package/native/mun-runtime/tests/typography.rs +63 -0
- package/package.json +213 -3
- package/schemas/README.md +19 -0
- package/schemas/semantic-ui-ir-v1.schema.json +2061 -0
- package/templates/desktop/gitignore +5 -0
- package/templates/desktop/package.json +10 -0
- package/templates/desktop/src-tauri/Cargo.toml +14 -0
- package/templates/desktop/src-tauri/build.rs +1 -0
- package/templates/desktop/src-tauri/capabilities/main.json +12 -0
- package/templates/desktop/src-tauri/icons/icon.icns +0 -0
- package/templates/desktop/src-tauri/icons/icon.ico +0 -0
- package/templates/desktop/src-tauri/icons/icon.png +0 -0
- package/templates/desktop/src-tauri/src/main.rs +2 -0
- package/templates/desktop/src-tauri/tauri.conf.json +42 -0
- package/templates/desktop/vite.config.ts +10 -0
- package/templates/native/App.mun +15 -0
- package/templates/native/gitignore +3 -0
- package/templates/native/mun.toml +8 -0
- package/templates/project/gitignore +5 -0
- package/templates/project/index.html +13 -0
- package/templates/project/package.json +24 -0
- package/templates/project/src/App.mun +13 -0
- package/templates/project/src/index.css +30 -0
- package/templates/project/src/main.ts +5 -0
- package/templates/project/tsconfig.json +20 -0
- package/templates/project/vite.config.ts +6 -0
- package/templates/vite/src/App.css +134 -0
- package/templates/vite/src/App.tsx +91 -0
- package/templates/vite/src/index.css +23 -0
- package/templates/vite/vite.config.ts +10 -0
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
//! Strict Semantic UI IR validation.
|
|
2
|
+
//!
|
|
3
|
+
//! The checked-in JSON Schema (`schemas/semantic-ui-ir-v1.schema.json`) is the
|
|
4
|
+
//! single versioned wire contract shared by the compiler, the TypeScript
|
|
5
|
+
//! types and this runtime. The runtime evaluates it directly against the raw
|
|
6
|
+
//! JSON before deserializing, so unknown fields, unknown kinds and malformed
|
|
7
|
+
//! shapes are rejected instead of being silently ignored by serde. Checks the
|
|
8
|
+
//! schema cannot express (references between states, nodes and `forEach`
|
|
9
|
+
//! scopes) follow in [`validate_references`].
|
|
10
|
+
//!
|
|
11
|
+
//! Only the JSON Schema subset the contract uses is implemented; an
|
|
12
|
+
//! unsupported keyword is a contract error, never silently skipped.
|
|
13
|
+
use std::collections::{HashMap, HashSet};
|
|
14
|
+
use std::fmt;
|
|
15
|
+
use std::sync::OnceLock;
|
|
16
|
+
|
|
17
|
+
use serde_json::{Map, Value};
|
|
18
|
+
|
|
19
|
+
const SCHEMA_SOURCE: &str = include_str!("../../../schemas/semantic-ui-ir-v1.schema.json");
|
|
20
|
+
|
|
21
|
+
/// A contract violation located by the nearest enclosing semantic node and the
|
|
22
|
+
/// JSON path of the offending field.
|
|
23
|
+
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
24
|
+
pub struct IrValidationError {
|
|
25
|
+
pub node: Option<String>,
|
|
26
|
+
pub path: String,
|
|
27
|
+
pub message: String,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
impl fmt::Display for IrValidationError {
|
|
31
|
+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
32
|
+
match &self.node {
|
|
33
|
+
Some(node) => write!(f, "node '{node}' at {}: {}", self.path, self.message),
|
|
34
|
+
None => write!(f, "{}: {}", self.path, self.message),
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
impl std::error::Error for IrValidationError {}
|
|
40
|
+
|
|
41
|
+
fn schema() -> &'static Value {
|
|
42
|
+
static SCHEMA: OnceLock<Value> = OnceLock::new();
|
|
43
|
+
SCHEMA
|
|
44
|
+
.get_or_init(|| serde_json::from_str(SCHEMA_SOURCE).expect("bundled Semantic UI IR schema"))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// Every `$ref` in the bundled schema, resolved once to its (non-reference)
|
|
48
|
+
/// target. `None` marks a reference that cannot be resolved; using it is a
|
|
49
|
+
/// contract error, reported when it is first reached as before.
|
|
50
|
+
fn references() -> &'static HashMap<String, Option<&'static Value>> {
|
|
51
|
+
static REFERENCES: OnceLock<HashMap<String, Option<&'static Value>>> = OnceLock::new();
|
|
52
|
+
REFERENCES.get_or_init(|| {
|
|
53
|
+
let root = schema();
|
|
54
|
+
let mut names = Vec::new();
|
|
55
|
+
collect_references(root, &mut names);
|
|
56
|
+
names
|
|
57
|
+
.into_iter()
|
|
58
|
+
.map(|reference| {
|
|
59
|
+
let mut target = Some(root);
|
|
60
|
+
let mut current = reference.clone();
|
|
61
|
+
// Follow chains of references; give up on cycles.
|
|
62
|
+
for _ in 0..64 {
|
|
63
|
+
target = current
|
|
64
|
+
.strip_prefix('#')
|
|
65
|
+
.and_then(|pointer| root.pointer(pointer));
|
|
66
|
+
match target
|
|
67
|
+
.and_then(|value| value.get("$ref"))
|
|
68
|
+
.and_then(Value::as_str)
|
|
69
|
+
{
|
|
70
|
+
Some(next) => current = next.to_owned(),
|
|
71
|
+
None => break,
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
let resolved = target.filter(|value| value.get("$ref").is_none());
|
|
75
|
+
(reference, resolved)
|
|
76
|
+
})
|
|
77
|
+
.collect()
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fn collect_references(schema: &Value, names: &mut Vec<String>) {
|
|
82
|
+
match schema {
|
|
83
|
+
Value::Object(object) => {
|
|
84
|
+
if let Some(reference) = object.get("$ref").and_then(Value::as_str) {
|
|
85
|
+
names.push(reference.to_owned());
|
|
86
|
+
}
|
|
87
|
+
for value in object.values() {
|
|
88
|
+
collect_references(value, names);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
Value::Array(items) => items
|
|
92
|
+
.iter()
|
|
93
|
+
.for_each(|item| collect_references(item, names)),
|
|
94
|
+
_ => {}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// Validate raw IR JSON against the bundled v1 schema and reference rules.
|
|
99
|
+
pub fn validate_program(program: &Value) -> Result<(), IrValidationError> {
|
|
100
|
+
let schema = schema();
|
|
101
|
+
let mut validator = Validator { root: schema };
|
|
102
|
+
validator.check(schema, program, &Context::default())?;
|
|
103
|
+
validate_references(program)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/// One step of a JSON path. Composite segments render with their own `/`.
|
|
107
|
+
#[derive(Clone, Copy)]
|
|
108
|
+
enum Segment<'a> {
|
|
109
|
+
Name(&'a str),
|
|
110
|
+
Index(usize),
|
|
111
|
+
/// `{name}/{index}`
|
|
112
|
+
Indexed(&'a str, usize),
|
|
113
|
+
/// `{name}/{index}/{field}`
|
|
114
|
+
IndexedField(&'a str, usize, &'a str),
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
impl fmt::Display for Segment<'_> {
|
|
118
|
+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
119
|
+
match self {
|
|
120
|
+
Self::Name(name) => f.write_str(name),
|
|
121
|
+
Self::Index(index) => write!(f, "{index}"),
|
|
122
|
+
Self::Indexed(name, index) => write!(f, "{name}/{index}"),
|
|
123
|
+
Self::IndexedField(name, index, field) => write!(f, "{name}/{index}/{field}"),
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/// Where validation is: a chain of borrowed path segments up to the root and
|
|
129
|
+
/// the nearest enclosing semantic node. Strings are built only for errors.
|
|
130
|
+
#[derive(Clone, Copy, Default)]
|
|
131
|
+
struct Context<'a> {
|
|
132
|
+
parent: Option<&'a Context<'a>>,
|
|
133
|
+
segment: Option<Segment<'a>>,
|
|
134
|
+
node: Option<&'a str>,
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
impl<'a> Context<'a> {
|
|
138
|
+
fn error(&self, message: impl Into<String>) -> IrValidationError {
|
|
139
|
+
let mut segments = Vec::new();
|
|
140
|
+
let mut node = None;
|
|
141
|
+
let mut current = Some(self);
|
|
142
|
+
while let Some(context) = current {
|
|
143
|
+
if let Some(segment) = context.segment {
|
|
144
|
+
segments.push(segment);
|
|
145
|
+
}
|
|
146
|
+
node = node.or(context.node);
|
|
147
|
+
current = context.parent;
|
|
148
|
+
}
|
|
149
|
+
let path = if segments.is_empty() {
|
|
150
|
+
"/".into()
|
|
151
|
+
} else {
|
|
152
|
+
segments
|
|
153
|
+
.iter()
|
|
154
|
+
.rev()
|
|
155
|
+
.fold(String::new(), |path, segment| format!("{path}/{segment}"))
|
|
156
|
+
};
|
|
157
|
+
IrValidationError {
|
|
158
|
+
node: node.map(str::to_owned),
|
|
159
|
+
path,
|
|
160
|
+
message: message.into(),
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
fn child(&'a self, segment: Segment<'a>) -> Context<'a> {
|
|
165
|
+
Context {
|
|
166
|
+
parent: Some(self),
|
|
167
|
+
segment: Some(segment),
|
|
168
|
+
node: None,
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/// Same path, reported against semantic node `id`.
|
|
173
|
+
fn within(&'a self, id: &'a str) -> Context<'a> {
|
|
174
|
+
Context {
|
|
175
|
+
parent: Some(self),
|
|
176
|
+
segment: None,
|
|
177
|
+
node: Some(id),
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
struct Validator<'s> {
|
|
183
|
+
root: &'s Value,
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
impl<'s> Validator<'s> {
|
|
187
|
+
fn resolve(&self, schema: &'s Value) -> Result<&'s Value, IrValidationError> {
|
|
188
|
+
let Some(reference) = schema.get("$ref").and_then(Value::as_str) else {
|
|
189
|
+
return Ok(schema);
|
|
190
|
+
};
|
|
191
|
+
// The bundled schema's references are resolved once; anything else
|
|
192
|
+
// (or an unresolvable reference, for its exact error) walks the pointer.
|
|
193
|
+
if std::ptr::eq(self.root, crate::validate::schema()) {
|
|
194
|
+
if let Some(Some(resolved)) = references().get(reference) {
|
|
195
|
+
return Ok(resolved);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
let pointer = reference.strip_prefix('#').ok_or_else(|| {
|
|
199
|
+
Context::default().error(format!("unsupported schema reference {reference}"))
|
|
200
|
+
})?;
|
|
201
|
+
let target = self.root.pointer(pointer).ok_or_else(|| {
|
|
202
|
+
Context::default().error(format!("dangling schema reference {reference}"))
|
|
203
|
+
})?;
|
|
204
|
+
self.resolve(target)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
fn check(
|
|
208
|
+
&mut self,
|
|
209
|
+
schema: &'s Value,
|
|
210
|
+
value: &Value,
|
|
211
|
+
context: &Context<'_>,
|
|
212
|
+
) -> Result<(), IrValidationError> {
|
|
213
|
+
let schema = self.resolve(schema)?;
|
|
214
|
+
let Some(rules) = schema.as_object() else {
|
|
215
|
+
return Err(context.error("malformed schema"));
|
|
216
|
+
};
|
|
217
|
+
// A semantic node names itself; nested errors are reported against it.
|
|
218
|
+
let scoped;
|
|
219
|
+
let context = if let (Some(id), Some(_)) = (
|
|
220
|
+
value.get("id").and_then(Value::as_str),
|
|
221
|
+
value.get("kind").and_then(Value::as_str),
|
|
222
|
+
) {
|
|
223
|
+
scoped = context.within(id);
|
|
224
|
+
&scoped
|
|
225
|
+
} else {
|
|
226
|
+
context
|
|
227
|
+
};
|
|
228
|
+
for (keyword, rule) in rules {
|
|
229
|
+
match keyword.as_str() {
|
|
230
|
+
"$schema" | "$id" | "$defs" | "title" | "description" | "$ref" => {}
|
|
231
|
+
"type" => check_type(rule, value, context)?,
|
|
232
|
+
"const" => {
|
|
233
|
+
if value != rule {
|
|
234
|
+
return Err(context.error(format!("expected {rule}, found {value}")));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
"enum" => {
|
|
238
|
+
let options = rule.as_array().map(Vec::as_slice).unwrap_or_default();
|
|
239
|
+
if !options.contains(value) {
|
|
240
|
+
return Err(context.error(format!("{value} is not one of {rule}")));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
"minLength" => {
|
|
244
|
+
if let Some(text) = value.as_str() {
|
|
245
|
+
if (text.chars().count() as u64) < rule.as_u64().unwrap_or(0) {
|
|
246
|
+
return Err(context.error("must not be empty"));
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
"minimum" | "maximum" | "exclusiveMinimum" => {
|
|
251
|
+
if let (Some(number), Some(bound)) = (value.as_f64(), rule.as_f64()) {
|
|
252
|
+
let ok = match keyword.as_str() {
|
|
253
|
+
"minimum" => number >= bound,
|
|
254
|
+
"maximum" => number <= bound,
|
|
255
|
+
_ => number > bound,
|
|
256
|
+
};
|
|
257
|
+
if !ok {
|
|
258
|
+
return Err(
|
|
259
|
+
context.error(format!("{number} violates {keyword} {bound}"))
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
"minItems" | "maxItems" => {
|
|
265
|
+
if let (Some(items), Some(bound)) = (value.as_array(), rule.as_u64()) {
|
|
266
|
+
let ok = if keyword == "minItems" {
|
|
267
|
+
items.len() as u64 >= bound
|
|
268
|
+
} else {
|
|
269
|
+
items.len() as u64 <= bound
|
|
270
|
+
};
|
|
271
|
+
if !ok {
|
|
272
|
+
return Err(
|
|
273
|
+
context.error(format!("array length violates {keyword} {bound}"))
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
"required" => {
|
|
279
|
+
if let Some(object) = value.as_object() {
|
|
280
|
+
for field in rule
|
|
281
|
+
.as_array()
|
|
282
|
+
.into_iter()
|
|
283
|
+
.flatten()
|
|
284
|
+
.filter_map(Value::as_str)
|
|
285
|
+
{
|
|
286
|
+
if !object.contains_key(field) {
|
|
287
|
+
return Err(
|
|
288
|
+
context.error(format!("missing required field '{field}'"))
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
"properties" | "additionalProperties" => {}
|
|
295
|
+
"items" | "prefixItems" => {}
|
|
296
|
+
"oneOf" => self.check_one_of(rule, value, context)?,
|
|
297
|
+
other => {
|
|
298
|
+
return Err(context.error(format!("schema uses unsupported keyword '{other}'")));
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if let Some(object) = value.as_object() {
|
|
303
|
+
self.check_object(rules, object, context)?;
|
|
304
|
+
}
|
|
305
|
+
if let Some(items) = value.as_array() {
|
|
306
|
+
let prefix = rules.get("prefixItems").and_then(Value::as_array);
|
|
307
|
+
for (index, item) in items.iter().enumerate() {
|
|
308
|
+
let item_schema = prefix
|
|
309
|
+
.and_then(|prefix| prefix.get(index))
|
|
310
|
+
.or_else(|| rules.get("items"));
|
|
311
|
+
if let Some(item_schema) = item_schema {
|
|
312
|
+
if item_schema == &Value::Bool(false) {
|
|
313
|
+
return Err(context.error(format!("unexpected array item {index}")));
|
|
314
|
+
}
|
|
315
|
+
self.check(item_schema, item, &context.child(Segment::Index(index)))?;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
Ok(())
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
fn check_object(
|
|
323
|
+
&mut self,
|
|
324
|
+
rules: &'s Map<String, Value>,
|
|
325
|
+
object: &Map<String, Value>,
|
|
326
|
+
context: &Context<'_>,
|
|
327
|
+
) -> Result<(), IrValidationError> {
|
|
328
|
+
let properties = rules.get("properties").and_then(Value::as_object);
|
|
329
|
+
let additional = rules.get("additionalProperties");
|
|
330
|
+
for (field, value) in object {
|
|
331
|
+
if let Some(schema) = properties.and_then(|properties| properties.get(field)) {
|
|
332
|
+
self.check(schema, value, &context.child(Segment::Name(field)))?;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
match additional {
|
|
336
|
+
Some(Value::Bool(false)) => {
|
|
337
|
+
return Err(context
|
|
338
|
+
.child(Segment::Name(field))
|
|
339
|
+
.error(format!("unknown field '{field}'")));
|
|
340
|
+
}
|
|
341
|
+
Some(schema @ Value::Object(_)) => {
|
|
342
|
+
self.check(schema, value, &context.child(Segment::Name(field)))?;
|
|
343
|
+
}
|
|
344
|
+
_ => {}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
Ok(())
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/// `oneOf` with a discriminator (`kind`, then `operation`) selects its
|
|
351
|
+
/// branch by the instance's tag so the reported error is the branch's own
|
|
352
|
+
/// field error, not a generic "no branch matched".
|
|
353
|
+
fn check_one_of(
|
|
354
|
+
&mut self,
|
|
355
|
+
rule: &'s Value,
|
|
356
|
+
value: &Value,
|
|
357
|
+
context: &Context<'_>,
|
|
358
|
+
) -> Result<(), IrValidationError> {
|
|
359
|
+
let branches = rule
|
|
360
|
+
.as_array()
|
|
361
|
+
.ok_or_else(|| context.error("malformed oneOf"))?
|
|
362
|
+
.iter()
|
|
363
|
+
.map(|branch| self.resolve(branch))
|
|
364
|
+
.collect::<Result<Vec<_>, _>>()?;
|
|
365
|
+
let mut candidates = branches.clone();
|
|
366
|
+
// Branches whose declared JSON type cannot match are never the
|
|
367
|
+
// intended form (e.g. `null` beside a motion plan).
|
|
368
|
+
let typed: Vec<_> = candidates
|
|
369
|
+
.iter()
|
|
370
|
+
.copied()
|
|
371
|
+
.filter(|branch| {
|
|
372
|
+
branch
|
|
373
|
+
.get("type")
|
|
374
|
+
.is_none_or(|rule| type_matches(rule, value))
|
|
375
|
+
})
|
|
376
|
+
.collect();
|
|
377
|
+
if !typed.is_empty() {
|
|
378
|
+
candidates = typed;
|
|
379
|
+
}
|
|
380
|
+
for tag in ["kind", "operation"] {
|
|
381
|
+
let tagged = candidates
|
|
382
|
+
.iter()
|
|
383
|
+
.filter(|branch| tag_constant(branch, tag).is_some())
|
|
384
|
+
.count();
|
|
385
|
+
if tagged == 0 {
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
let Some(instance) = value.get(tag) else {
|
|
389
|
+
if tagged == candidates.len() {
|
|
390
|
+
return Err(context.error(format!("missing required field '{tag}'")));
|
|
391
|
+
}
|
|
392
|
+
continue;
|
|
393
|
+
};
|
|
394
|
+
let matching: Vec<_> = candidates
|
|
395
|
+
.iter()
|
|
396
|
+
.copied()
|
|
397
|
+
.filter(|branch| tag_constant(branch, tag) == Some(instance))
|
|
398
|
+
.collect();
|
|
399
|
+
if matching.is_empty() && tagged == candidates.len() {
|
|
400
|
+
return Err(context
|
|
401
|
+
.child(Segment::Name(tag))
|
|
402
|
+
.error(format!("unsupported {tag} {instance}")));
|
|
403
|
+
}
|
|
404
|
+
if !matching.is_empty() {
|
|
405
|
+
candidates = matching;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
let mut first_error = None;
|
|
409
|
+
let mut matched = 0;
|
|
410
|
+
for branch in &candidates {
|
|
411
|
+
match self.check(branch, value, context) {
|
|
412
|
+
Ok(()) => matched += 1,
|
|
413
|
+
Err(error) => {
|
|
414
|
+
first_error.get_or_insert(error);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
match matched {
|
|
419
|
+
1 => Ok(()),
|
|
420
|
+
0 => Err(if candidates.len() == 1 {
|
|
421
|
+
first_error.expect("error")
|
|
422
|
+
} else {
|
|
423
|
+
context.error(format!(
|
|
424
|
+
"value matches none of the allowed forms ({})",
|
|
425
|
+
first_error.map(|error| error.message).unwrap_or_default()
|
|
426
|
+
))
|
|
427
|
+
}),
|
|
428
|
+
_ => Err(context.error("value is ambiguous between several allowed forms")),
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/// `/properties/{tag}/const` of a schema branch (the discriminator value).
|
|
434
|
+
fn tag_constant<'s>(branch: &'s Value, tag: &str) -> Option<&'s Value> {
|
|
435
|
+
branch.get("properties")?.get(tag)?.get("const")
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
fn check_type(rule: &Value, value: &Value, context: &Context<'_>) -> Result<(), IrValidationError> {
|
|
439
|
+
if type_matches(rule, value) {
|
|
440
|
+
Ok(())
|
|
441
|
+
} else {
|
|
442
|
+
Err(context.error(format!("expected {rule}, found {}", type_name(value))))
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
fn type_matches(rule: &Value, value: &Value) -> bool {
|
|
447
|
+
let matches = |name: &str| match name {
|
|
448
|
+
"object" => value.is_object(),
|
|
449
|
+
"array" => value.is_array(),
|
|
450
|
+
"string" => value.is_string(),
|
|
451
|
+
"number" => value.as_f64().is_some_and(f64::is_finite),
|
|
452
|
+
"integer" => value.is_i64() || value.is_u64(),
|
|
453
|
+
"boolean" => value.is_boolean(),
|
|
454
|
+
"null" => value.is_null(),
|
|
455
|
+
_ => false,
|
|
456
|
+
};
|
|
457
|
+
match rule {
|
|
458
|
+
Value::String(name) => matches(name),
|
|
459
|
+
Value::Array(names) => names.iter().filter_map(Value::as_str).any(matches),
|
|
460
|
+
_ => false,
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
fn type_name(value: &Value) -> &'static str {
|
|
465
|
+
match value {
|
|
466
|
+
Value::Null => "null",
|
|
467
|
+
Value::Bool(_) => "boolean",
|
|
468
|
+
Value::Number(_) => "number",
|
|
469
|
+
Value::String(_) => "string",
|
|
470
|
+
Value::Array(_) => "array",
|
|
471
|
+
Value::Object(_) => "object",
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/// Cross-references the schema cannot express: declared state names, binding
|
|
476
|
+
/// targets, `forEach` scopes and `item` expressions. (Unique node identities
|
|
477
|
+
/// are checked on the typed program, see `RuntimeLoadError::DuplicateNodeIdentity`.)
|
|
478
|
+
fn validate_references(program: &Value) -> Result<(), IrValidationError> {
|
|
479
|
+
let mut states: HashMap<&str, &Value> = HashMap::new();
|
|
480
|
+
let mut scopes: Vec<(&str, &str)> = Vec::new();
|
|
481
|
+
for (index, state) in program["states"]
|
|
482
|
+
.as_array()
|
|
483
|
+
.into_iter()
|
|
484
|
+
.flatten()
|
|
485
|
+
.enumerate()
|
|
486
|
+
{
|
|
487
|
+
let name = state["name"].as_str().unwrap_or_default();
|
|
488
|
+
if states.insert(name, &state["initial"]).is_some() {
|
|
489
|
+
let root = Context::default();
|
|
490
|
+
return Err(root
|
|
491
|
+
.child(Segment::IndexedField("states", index, "name"))
|
|
492
|
+
.error(format!("duplicate state '{name}'")));
|
|
493
|
+
}
|
|
494
|
+
if let Some(scope) = state["scope"].as_str() {
|
|
495
|
+
scopes.push((name, scope));
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
let mut walker = References {
|
|
499
|
+
states: &states,
|
|
500
|
+
for_each: Vec::new(),
|
|
501
|
+
all_for_each: HashSet::new(),
|
|
502
|
+
};
|
|
503
|
+
let root = &program["root"];
|
|
504
|
+
let top = Context::default();
|
|
505
|
+
let path = top.child(Segment::Name("root"));
|
|
506
|
+
let context = match root["id"].as_str() {
|
|
507
|
+
Some(id) => path.within(id),
|
|
508
|
+
None => path,
|
|
509
|
+
};
|
|
510
|
+
walker.node(&root["child"], &context.child(Segment::Name("child")))?;
|
|
511
|
+
for (state, scope) in scopes {
|
|
512
|
+
if !walker.all_for_each.contains(scope) {
|
|
513
|
+
return Err(top.child(Segment::Name("states")).error(format!(
|
|
514
|
+
"state '{state}' is scoped to unknown forEach '{scope}'"
|
|
515
|
+
)));
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
Ok(())
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
struct References<'a> {
|
|
522
|
+
states: &'a HashMap<&'a str, &'a Value>,
|
|
523
|
+
for_each: Vec<&'a str>,
|
|
524
|
+
all_for_each: HashSet<&'a str>,
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
impl<'a> References<'a> {
|
|
528
|
+
fn node(&mut self, node: &'a Value, context: &Context<'_>) -> Result<(), IrValidationError> {
|
|
529
|
+
let id = node["id"].as_str().unwrap_or_default();
|
|
530
|
+
let context = context.within(id);
|
|
531
|
+
let kind = node["kind"].as_str().unwrap_or_default();
|
|
532
|
+
match kind {
|
|
533
|
+
"textField" => {
|
|
534
|
+
self.state(
|
|
535
|
+
&node["state"],
|
|
536
|
+
&context.child(Segment::Name("state")),
|
|
537
|
+
Some("string"),
|
|
538
|
+
)?;
|
|
539
|
+
}
|
|
540
|
+
"radioGroup" => {
|
|
541
|
+
self.state(&node["state"], &context.child(Segment::Name("state")), None)?;
|
|
542
|
+
let mut seen = HashSet::new();
|
|
543
|
+
for (index, option) in node["options"].as_array().into_iter().flatten().enumerate()
|
|
544
|
+
{
|
|
545
|
+
if !seen.insert(option["value"].to_string()) {
|
|
546
|
+
return Err(context
|
|
547
|
+
.child(Segment::IndexedField("options", index, "value"))
|
|
548
|
+
.error(format!("duplicate radio option value {}", option["value"])));
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
"toggle" => {
|
|
553
|
+
self.state(&node["state"], &context.child(Segment::Name("state")), None)?;
|
|
554
|
+
let name = node["state"].as_str().unwrap_or_default();
|
|
555
|
+
if self
|
|
556
|
+
.states
|
|
557
|
+
.get(name)
|
|
558
|
+
.is_some_and(|initial| !initial.is_boolean())
|
|
559
|
+
{
|
|
560
|
+
return Err(context
|
|
561
|
+
.child(Segment::Name("state"))
|
|
562
|
+
.error(format!("toggle state '{name}' must hold a boolean")));
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
"action" => self.action(&node["action"], &context.child(Segment::Name("action")))?,
|
|
566
|
+
_ => {}
|
|
567
|
+
}
|
|
568
|
+
for phase in ["appear", "disappear"] {
|
|
569
|
+
if let Some(action) = node["lifecycle"].get(phase) {
|
|
570
|
+
let lifecycle = context.child(Segment::Name("lifecycle"));
|
|
571
|
+
self.action(action, &lifecycle.child(Segment::Name(phase)))?;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
if let Value::Object(object) = node {
|
|
575
|
+
for (field, value) in object {
|
|
576
|
+
if !matches!(
|
|
577
|
+
field.as_str(),
|
|
578
|
+
"children" | "then" | "otherwise" | "child" | "action" | "lifecycle"
|
|
579
|
+
) {
|
|
580
|
+
self.expressions(value, &context.child(Segment::Name(field)))?;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
if kind == "forEach" {
|
|
585
|
+
self.all_for_each.insert(id);
|
|
586
|
+
self.for_each.push(id);
|
|
587
|
+
}
|
|
588
|
+
for field in ["children", "then", "otherwise"] {
|
|
589
|
+
for (index, child) in node[field].as_array().into_iter().flatten().enumerate() {
|
|
590
|
+
self.node(child, &context.child(Segment::Indexed(field, index)))?;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
if kind == "forEach" {
|
|
594
|
+
self.for_each.pop();
|
|
595
|
+
}
|
|
596
|
+
Ok(())
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
fn state(
|
|
600
|
+
&self,
|
|
601
|
+
state: &Value,
|
|
602
|
+
context: &Context<'_>,
|
|
603
|
+
string: Option<&str>,
|
|
604
|
+
) -> Result<(), IrValidationError> {
|
|
605
|
+
let name = state.as_str().unwrap_or_default();
|
|
606
|
+
let Some(initial) = self.states.get(name) else {
|
|
607
|
+
return Err(context.error(format!("references undeclared state '{name}'")));
|
|
608
|
+
};
|
|
609
|
+
if string.is_some() && !initial.is_string() {
|
|
610
|
+
return Err(context.error(format!("text binding state '{name}' must hold a string")));
|
|
611
|
+
}
|
|
612
|
+
Ok(())
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
fn action(&self, action: &Value, context: &Context<'_>) -> Result<(), IrValidationError> {
|
|
616
|
+
if let Some(state) = action.get("state") {
|
|
617
|
+
self.state(state, &context.child(Segment::Name("state")), None)?;
|
|
618
|
+
}
|
|
619
|
+
for (index, nested) in action["actions"]
|
|
620
|
+
.as_array()
|
|
621
|
+
.into_iter()
|
|
622
|
+
.flatten()
|
|
623
|
+
.enumerate()
|
|
624
|
+
{
|
|
625
|
+
self.action(nested, &context.child(Segment::Indexed("actions", index)))?;
|
|
626
|
+
}
|
|
627
|
+
if let Value::Object(object) = action {
|
|
628
|
+
for (field, value) in object {
|
|
629
|
+
if field != "actions" {
|
|
630
|
+
self.expressions(value, &context.child(Segment::Name(field)))?;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
Ok(())
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/// Every `state` and `item` expression reachable from a node field.
|
|
638
|
+
fn expressions(&self, value: &Value, context: &Context<'_>) -> Result<(), IrValidationError> {
|
|
639
|
+
match value {
|
|
640
|
+
Value::Object(object) => {
|
|
641
|
+
match object.get("kind").and_then(Value::as_str) {
|
|
642
|
+
Some("state") if object.contains_key("state") && object.len() == 2 => {
|
|
643
|
+
self.state(
|
|
644
|
+
&object["state"],
|
|
645
|
+
&context.child(Segment::Name("state")),
|
|
646
|
+
None,
|
|
647
|
+
)?;
|
|
648
|
+
}
|
|
649
|
+
Some("item") => {
|
|
650
|
+
let target = object["forEach"].as_str().unwrap_or_default();
|
|
651
|
+
if !self.for_each.iter().any(|id| *id == target) {
|
|
652
|
+
return Err(context
|
|
653
|
+
.child(Segment::Name("forEach"))
|
|
654
|
+
.error(format!("item expression outside its forEach '{target}'")));
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
_ => {}
|
|
658
|
+
}
|
|
659
|
+
for (field, nested) in object {
|
|
660
|
+
if field != "value"
|
|
661
|
+
|| object.get("kind").and_then(Value::as_str) != Some("literal")
|
|
662
|
+
{
|
|
663
|
+
self.expressions(nested, &context.child(Segment::Name(field)))?;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
Ok(())
|
|
667
|
+
}
|
|
668
|
+
Value::Array(items) => {
|
|
669
|
+
for (index, item) in items.iter().enumerate() {
|
|
670
|
+
self.expressions(item, &context.child(Segment::Index(index)))?;
|
|
671
|
+
}
|
|
672
|
+
Ok(())
|
|
673
|
+
}
|
|
674
|
+
_ => Ok(()),
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
}
|