@mun-ui/ui 0.0.0-stage → 0.2.0
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 +104 -2
- package/bin/compile.mjs +38 -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 +521 -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/lib.rs +2676 -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 +310 -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 +1073 -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 +782 -0
- package/native/mun-runtime/src/kinetics.rs +500 -0
- package/native/mun-runtime/src/layout.rs +173 -0
- package/native/mun-runtime/src/layout_cache.rs +209 -0
- package/native/mun-runtime/src/lib.rs +49 -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 +7078 -0
- package/native/mun-runtime/src/scene.rs +530 -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/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/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/scroll_view.rs +403 -0
- package/native/mun-runtime/tests/text_editing.rs +491 -0
- package/package.json +210 -3
- package/schemas/README.md +19 -0
- package/schemas/semantic-ui-ir-v1.schema.json +1972 -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 +21 -0
- package/templates/project/src/App.mun +13 -0
- package/templates/project/src/index.css +21 -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,1073 @@
|
|
|
1
|
+
//! Runtime-owned keyed collections and item state scopes.
|
|
2
|
+
//!
|
|
3
|
+
//! The semantic IR describes `forEach` templates. Before layout, the runtime
|
|
4
|
+
//! materializes one instance of the template per collection item:
|
|
5
|
+
//!
|
|
6
|
+
//! * instance identity = template node identity + `[`encoded key`]` (one
|
|
7
|
+
//! bracketed segment per enclosing `forEach`; `[` never occurs in compiler
|
|
8
|
+
//! identities because key segments and `.id(_:)` keys are percent-encoded),
|
|
9
|
+
//! for every node in the instance, so focus, editors, scroll offsets,
|
|
10
|
+
//! retained identity, transitions and FLIP follow the *key*, never the index;
|
|
11
|
+
//! * item expressions are substituted with the item's values;
|
|
12
|
+
//! * View-local state declared inside the template (`UiState::scope`) is renamed
|
|
13
|
+
//! to per-key instance storage, created on first appearance of the key and
|
|
14
|
+
//! released when the key leaves its collection.
|
|
15
|
+
//!
|
|
16
|
+
//! Duplicate keys are rejected explicitly; they are never aliased.
|
|
17
|
+
use std::{
|
|
18
|
+
collections::{BTreeMap, HashMap, HashSet},
|
|
19
|
+
rc::Rc,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
use serde_json::Value;
|
|
23
|
+
|
|
24
|
+
use crate::ir::{UiAction, UiCollectionOperation, UiExpression, UiFilterOperator, UiNode, UiState};
|
|
25
|
+
|
|
26
|
+
/// Static facts about item-scoped state derived from the IR once at load.
|
|
27
|
+
#[derive(Clone, Debug, Default)]
|
|
28
|
+
pub(crate) struct ScopeModel {
|
|
29
|
+
/// Template state name -> declaring `forEach` template id.
|
|
30
|
+
scoped_states: HashMap<String, String>,
|
|
31
|
+
/// Template state name -> initial value.
|
|
32
|
+
initials: HashMap<String, Value>,
|
|
33
|
+
/// States read by any `forEach` collection expression (structural deps).
|
|
34
|
+
pub(crate) structural_states: HashSet<String>,
|
|
35
|
+
/// Template states that can change retained structure: read by a
|
|
36
|
+
/// conditional's condition, a `.id(_:)` identity key or a `forEach`
|
|
37
|
+
/// collection. Edits to any other state cannot insert, remove, replace or
|
|
38
|
+
/// reparent retained nodes.
|
|
39
|
+
structure_states: HashSet<String>,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
impl ScopeModel {
|
|
43
|
+
pub(crate) fn new(states: &[UiState], template: &UiNode) -> Self {
|
|
44
|
+
let mut model = Self::default();
|
|
45
|
+
for state in states {
|
|
46
|
+
if let Some(scope) = &state.scope {
|
|
47
|
+
model
|
|
48
|
+
.scoped_states
|
|
49
|
+
.insert(state.name.clone(), scope.clone());
|
|
50
|
+
model
|
|
51
|
+
.initials
|
|
52
|
+
.insert(state.name.clone(), state.initial.clone());
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
collect_structural_states(template, &mut model.structural_states);
|
|
56
|
+
model.structure_states = model.structural_states.clone();
|
|
57
|
+
collect_structure_states(template, &mut model.structure_states);
|
|
58
|
+
model
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// Whether a concrete state (template name or per-key instance) can change
|
|
62
|
+
/// the retained node structure.
|
|
63
|
+
pub(crate) fn affects_structure(&self, state: &str) -> bool {
|
|
64
|
+
let template = state
|
|
65
|
+
.split_once('[')
|
|
66
|
+
.map_or(state, |(template, _)| template);
|
|
67
|
+
self.structure_states.contains(template)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
pub(crate) fn add_structure_expression(&mut self, expression: &UiExpression) {
|
|
71
|
+
expression_states(expression, &mut self.structure_states);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
pub(crate) fn is_scoped(&self, state: &str) -> bool {
|
|
75
|
+
self.scoped_states.contains_key(state)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// Whether a concrete state name is a per-key instance of scoped state.
|
|
79
|
+
pub(crate) fn is_instance(&self, name: &str) -> bool {
|
|
80
|
+
name.split_once('[')
|
|
81
|
+
.is_some_and(|(template, _)| self.is_scoped(template))
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
pub(crate) fn has_collections(&self) -> bool {
|
|
85
|
+
!self.structural_states.is_empty()
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
fn collect_structural_states(node: &UiNode, output: &mut HashSet<String>) {
|
|
90
|
+
if let UiNode::ForEach { collection, .. } = node {
|
|
91
|
+
expression_states(collection, output);
|
|
92
|
+
}
|
|
93
|
+
if let UiNode::Conditional {
|
|
94
|
+
then_nodes,
|
|
95
|
+
otherwise,
|
|
96
|
+
..
|
|
97
|
+
} = node
|
|
98
|
+
{
|
|
99
|
+
for child in then_nodes.iter().chain(otherwise) {
|
|
100
|
+
collect_structural_states(child, output);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
for child in node.children() {
|
|
104
|
+
collect_structural_states(child, output);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
fn collect_structure_states(node: &UiNode, output: &mut HashSet<String>) {
|
|
109
|
+
if let Some(key) = &node.base().identity_key {
|
|
110
|
+
expression_states(key, output);
|
|
111
|
+
}
|
|
112
|
+
if let UiNode::Conditional {
|
|
113
|
+
condition,
|
|
114
|
+
then_nodes,
|
|
115
|
+
otherwise,
|
|
116
|
+
..
|
|
117
|
+
} = node
|
|
118
|
+
{
|
|
119
|
+
expression_states(condition, output);
|
|
120
|
+
for child in then_nodes.iter().chain(otherwise) {
|
|
121
|
+
collect_structure_states(child, output);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
for child in node.children() {
|
|
125
|
+
collect_structure_states(child, output);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
fn expression_states(expression: &UiExpression, output: &mut HashSet<String>) {
|
|
130
|
+
match expression {
|
|
131
|
+
UiExpression::State { state } => {
|
|
132
|
+
output.insert(state.clone());
|
|
133
|
+
}
|
|
134
|
+
UiExpression::Not { value } | UiExpression::Stringify { value } => {
|
|
135
|
+
expression_states(value, output)
|
|
136
|
+
}
|
|
137
|
+
UiExpression::Binary { left, right, .. } => {
|
|
138
|
+
expression_states(left, output);
|
|
139
|
+
expression_states(right, output);
|
|
140
|
+
}
|
|
141
|
+
UiExpression::Conditional {
|
|
142
|
+
condition,
|
|
143
|
+
then_value,
|
|
144
|
+
otherwise,
|
|
145
|
+
} => {
|
|
146
|
+
expression_states(condition, output);
|
|
147
|
+
expression_states(then_value, output);
|
|
148
|
+
expression_states(otherwise, output);
|
|
149
|
+
}
|
|
150
|
+
UiExpression::Record { fields } => {
|
|
151
|
+
for field in fields.values() {
|
|
152
|
+
expression_states(field, output);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
UiExpression::Count { collection } => expression_states(collection, output),
|
|
156
|
+
UiExpression::Filter {
|
|
157
|
+
collection, value, ..
|
|
158
|
+
} => {
|
|
159
|
+
expression_states(collection, output);
|
|
160
|
+
expression_states(value, output);
|
|
161
|
+
}
|
|
162
|
+
UiExpression::Literal { .. } | UiExpression::Item { .. } => {}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/// Canonical, collision-free key segment: strings and finite numbers only.
|
|
167
|
+
pub fn key_segment(value: &Value) -> Option<String> {
|
|
168
|
+
match value {
|
|
169
|
+
Value::String(text) => Some(format!("s:{}", percent_encode(text))),
|
|
170
|
+
Value::Number(number) => {
|
|
171
|
+
let number = number.as_f64()?;
|
|
172
|
+
if !number.is_finite() {
|
|
173
|
+
return None;
|
|
174
|
+
}
|
|
175
|
+
let number = if number == 0.0 { 0.0 } else { number };
|
|
176
|
+
Some(format!("n:{number}"))
|
|
177
|
+
}
|
|
178
|
+
_ => None,
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
fn percent_encode(text: &str) -> String {
|
|
183
|
+
let mut output = String::with_capacity(text.len());
|
|
184
|
+
for byte in text.bytes() {
|
|
185
|
+
if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.') {
|
|
186
|
+
output.push(byte as char);
|
|
187
|
+
} else {
|
|
188
|
+
output.push_str(&format!("%{byte:02X}"));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
output
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
pub fn value_at_path<'a>(value: &'a Value, path: &[String]) -> Option<&'a Value> {
|
|
195
|
+
path.iter()
|
|
196
|
+
.try_fold(value, |current, field| current.as_object()?.get(field))
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/// Pure expression evaluation over a state map.
|
|
200
|
+
pub(crate) fn evaluate(expression: &UiExpression, state: &HashMap<String, Value>) -> Value {
|
|
201
|
+
match expression {
|
|
202
|
+
UiExpression::Literal { value } => value.clone(),
|
|
203
|
+
UiExpression::State { state: name } => state.get(name).cloned().unwrap_or(Value::Null),
|
|
204
|
+
UiExpression::Not { value } => {
|
|
205
|
+
Value::Bool(!evaluate(value, state).as_bool().unwrap_or(false))
|
|
206
|
+
}
|
|
207
|
+
UiExpression::Stringify { value } => {
|
|
208
|
+
Value::String(crate::runtime::scalar_string(&evaluate(value, state)))
|
|
209
|
+
}
|
|
210
|
+
UiExpression::Binary {
|
|
211
|
+
operator,
|
|
212
|
+
left,
|
|
213
|
+
right,
|
|
214
|
+
} => crate::runtime::evaluate_binary(
|
|
215
|
+
*operator,
|
|
216
|
+
evaluate(left, state),
|
|
217
|
+
evaluate(right, state),
|
|
218
|
+
),
|
|
219
|
+
UiExpression::Conditional {
|
|
220
|
+
condition,
|
|
221
|
+
then_value,
|
|
222
|
+
otherwise,
|
|
223
|
+
} => {
|
|
224
|
+
if evaluate(condition, state).as_bool().unwrap_or(false) {
|
|
225
|
+
evaluate(then_value, state)
|
|
226
|
+
} else {
|
|
227
|
+
evaluate(otherwise, state)
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
// Materialization substitutes every item reference; one surviving here
|
|
231
|
+
// is outside any forEach and has no value.
|
|
232
|
+
UiExpression::Item { .. } => Value::Null,
|
|
233
|
+
UiExpression::Record { fields } => Value::Object(
|
|
234
|
+
fields
|
|
235
|
+
.iter()
|
|
236
|
+
.map(|(name, field)| (name.clone(), evaluate(field, state)))
|
|
237
|
+
.collect(),
|
|
238
|
+
),
|
|
239
|
+
UiExpression::Count { collection } => match evaluate(collection, state) {
|
|
240
|
+
Value::Array(items) => Value::from(items.len()),
|
|
241
|
+
_ => Value::from(0),
|
|
242
|
+
},
|
|
243
|
+
UiExpression::Filter {
|
|
244
|
+
collection,
|
|
245
|
+
path,
|
|
246
|
+
operator,
|
|
247
|
+
value,
|
|
248
|
+
} => {
|
|
249
|
+
let expected = evaluate(value, state);
|
|
250
|
+
let Value::Array(items) = evaluate(collection, state) else {
|
|
251
|
+
return Value::Array(Vec::new());
|
|
252
|
+
};
|
|
253
|
+
Value::Array(
|
|
254
|
+
items
|
|
255
|
+
.into_iter()
|
|
256
|
+
.filter(|item| {
|
|
257
|
+
let matches = value_at_path(item, path) == Some(&expected);
|
|
258
|
+
match operator {
|
|
259
|
+
UiFilterOperator::Equal => matches,
|
|
260
|
+
UiFilterOperator::NotEqual => !matches,
|
|
261
|
+
}
|
|
262
|
+
})
|
|
263
|
+
.collect(),
|
|
264
|
+
)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
270
|
+
pub enum CollectionError {
|
|
271
|
+
/// Two items of one `forEach` (or one keyed mutation) share a key.
|
|
272
|
+
DuplicateKey { collection: String, key: String },
|
|
273
|
+
/// An item key is missing or not a string/finite number.
|
|
274
|
+
InvalidKey { collection: String },
|
|
275
|
+
/// A `forEach` collection or a mutated state is not an array.
|
|
276
|
+
NotACollection { collection: String },
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
impl std::fmt::Display for CollectionError {
|
|
280
|
+
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
281
|
+
match self {
|
|
282
|
+
Self::DuplicateKey { collection, key } => {
|
|
283
|
+
write!(
|
|
284
|
+
formatter,
|
|
285
|
+
"duplicate item key {key} in collection '{collection}'"
|
|
286
|
+
)
|
|
287
|
+
}
|
|
288
|
+
Self::InvalidKey { collection } => write!(
|
|
289
|
+
formatter,
|
|
290
|
+
"collection '{collection}' has an item whose key is not a string or finite number"
|
|
291
|
+
),
|
|
292
|
+
Self::NotACollection { collection } => {
|
|
293
|
+
write!(
|
|
294
|
+
formatter,
|
|
295
|
+
"'{collection}' is not a collection (array) value"
|
|
296
|
+
)
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
struct Scope<'a> {
|
|
303
|
+
for_each: &'a str,
|
|
304
|
+
segment: String,
|
|
305
|
+
item: Value,
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/// What one top-level `forEach` expansion depended on and created. The
|
|
309
|
+
/// expansion is a pure function of its template subtree, its evaluated
|
|
310
|
+
/// collection and the item-scoped state declarations, so when all three are
|
|
311
|
+
/// unchanged the previous instances are reused instead of re-expanded.
|
|
312
|
+
#[derive(Clone, Debug)]
|
|
313
|
+
pub(crate) struct ForEachMemo {
|
|
314
|
+
template: Rc<UiNode>,
|
|
315
|
+
collection: Rc<Value>,
|
|
316
|
+
/// Concrete per-key state instances the expansion renamed state to.
|
|
317
|
+
instances: Rc<[String]>,
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/// Top-level `forEach` id -> its last expansion.
|
|
321
|
+
pub(crate) type MaterializationMemo = HashMap<String, ForEachMemo>;
|
|
322
|
+
|
|
323
|
+
/// A previous materialization whose unchanged `forEach` expansions may be reused.
|
|
324
|
+
pub(crate) struct PreviousMaterialization<'a> {
|
|
325
|
+
pub(crate) memo: &'a MaterializationMemo,
|
|
326
|
+
pub(crate) model: &'a ScopeModel,
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/// Result of materializing the template against the current state.
|
|
330
|
+
pub(crate) struct Materialized {
|
|
331
|
+
pub(crate) root: UiNode,
|
|
332
|
+
pub(crate) memo: MaterializationMemo,
|
|
333
|
+
/// `forEach` fragments emitted empty because the previous expansion is
|
|
334
|
+
/// still exact; the caller moves the previous instances in with
|
|
335
|
+
/// [`splice_reused`] once the new materialization is committed.
|
|
336
|
+
pub(crate) reused: Vec<String>,
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
pub(crate) fn materialize(
|
|
340
|
+
template: &UiNode,
|
|
341
|
+
model: &ScopeModel,
|
|
342
|
+
state: &mut HashMap<String, Value>,
|
|
343
|
+
previous: Option<PreviousMaterialization<'_>>,
|
|
344
|
+
) -> Result<Materialized, CollectionError> {
|
|
345
|
+
// Renaming depends on which forEach declares each item-scoped state.
|
|
346
|
+
let previous = previous.filter(|previous| previous.model.scoped_states == model.scoped_states);
|
|
347
|
+
let mut expander = Expander {
|
|
348
|
+
model,
|
|
349
|
+
state,
|
|
350
|
+
live_instances: HashSet::new(),
|
|
351
|
+
previous: previous.map(|previous| previous.memo),
|
|
352
|
+
memo: MaterializationMemo::new(),
|
|
353
|
+
reused: Vec::new(),
|
|
354
|
+
recording: None,
|
|
355
|
+
};
|
|
356
|
+
let mut scopes = Vec::new();
|
|
357
|
+
let root = expander.node(template, &mut scopes)?;
|
|
358
|
+
let (memo, reused) = (expander.memo, expander.reused);
|
|
359
|
+
let live_instances = expander.live_instances;
|
|
360
|
+
// Release instance state for keys that left their collection.
|
|
361
|
+
let stale = state
|
|
362
|
+
.keys()
|
|
363
|
+
.filter(|name| !live_instances.contains(*name) && model.is_instance(name))
|
|
364
|
+
.cloned()
|
|
365
|
+
.collect::<Vec<_>>();
|
|
366
|
+
for name in stale {
|
|
367
|
+
state.remove(&name);
|
|
368
|
+
}
|
|
369
|
+
Ok(Materialized { root, memo, reused })
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/// Move the instances of every reused `forEach` fragment from `source` (the
|
|
373
|
+
/// previous materialized tree) into the empty fragment of the same id in
|
|
374
|
+
/// `target`.
|
|
375
|
+
pub(crate) fn splice_reused(target: &mut UiNode, source: &mut UiNode, reused: &[String]) {
|
|
376
|
+
for id in reused {
|
|
377
|
+
let instances = fragment_mut(source, id)
|
|
378
|
+
.map(std::mem::take)
|
|
379
|
+
.expect("a reused forEach fragment exists in the previous materialization");
|
|
380
|
+
*fragment_mut(target, id).expect("a reused forEach fragment was emitted") = instances;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
fn fragment_mut<'a>(node: &'a mut UiNode, id: &str) -> Option<&'a mut Vec<UiNode>> {
|
|
385
|
+
match node {
|
|
386
|
+
UiNode::Conditional {
|
|
387
|
+
base,
|
|
388
|
+
then_nodes,
|
|
389
|
+
otherwise,
|
|
390
|
+
..
|
|
391
|
+
} => {
|
|
392
|
+
if base.id == id {
|
|
393
|
+
return Some(then_nodes);
|
|
394
|
+
}
|
|
395
|
+
then_nodes
|
|
396
|
+
.iter_mut()
|
|
397
|
+
.chain(otherwise.iter_mut())
|
|
398
|
+
.find_map(|child| fragment_mut(child, id))
|
|
399
|
+
}
|
|
400
|
+
UiNode::Column { children, .. }
|
|
401
|
+
| UiNode::Row { children, .. }
|
|
402
|
+
| UiNode::Scroll { children, .. }
|
|
403
|
+
| UiNode::Overlay { children, .. } => children
|
|
404
|
+
.iter_mut()
|
|
405
|
+
.find_map(|child| fragment_mut(child, id)),
|
|
406
|
+
_ => None,
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
fn contains_for_each(nodes: &[UiNode]) -> bool {
|
|
411
|
+
nodes.iter().any(|node| match node {
|
|
412
|
+
UiNode::ForEach { .. } => true,
|
|
413
|
+
UiNode::Conditional {
|
|
414
|
+
then_nodes,
|
|
415
|
+
otherwise,
|
|
416
|
+
..
|
|
417
|
+
} => contains_for_each(then_nodes) || contains_for_each(otherwise),
|
|
418
|
+
_ => contains_for_each(node.children()),
|
|
419
|
+
})
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
struct Expander<'m, 's> {
|
|
423
|
+
model: &'m ScopeModel,
|
|
424
|
+
state: &'s mut HashMap<String, Value>,
|
|
425
|
+
live_instances: HashSet<String>,
|
|
426
|
+
previous: Option<&'m MaterializationMemo>,
|
|
427
|
+
memo: MaterializationMemo,
|
|
428
|
+
reused: Vec<String>,
|
|
429
|
+
/// Instances renamed while expanding the current top-level forEach.
|
|
430
|
+
recording: Option<Vec<String>>,
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
impl Expander<'_, '_> {
|
|
434
|
+
fn suffix(scopes: &[Scope<'_>]) -> String {
|
|
435
|
+
scopes
|
|
436
|
+
.iter()
|
|
437
|
+
.map(|scope| format!("[{}]", scope.segment))
|
|
438
|
+
.collect()
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
fn rename_state(&mut self, name: &str, scopes: &[Scope<'_>]) -> String {
|
|
442
|
+
let Some(declared) = self.model.scoped_states.get(name) else {
|
|
443
|
+
return name.to_owned();
|
|
444
|
+
};
|
|
445
|
+
let Some(position) = scopes.iter().position(|scope| scope.for_each == declared) else {
|
|
446
|
+
return name.to_owned();
|
|
447
|
+
};
|
|
448
|
+
let concrete = format!("{name}{}", Self::suffix(&scopes[..=position]));
|
|
449
|
+
if !self.state.contains_key(&concrete) {
|
|
450
|
+
let initial = self
|
|
451
|
+
.model
|
|
452
|
+
.initials
|
|
453
|
+
.get(name)
|
|
454
|
+
.cloned()
|
|
455
|
+
.unwrap_or(Value::Null);
|
|
456
|
+
self.state.insert(concrete.clone(), initial);
|
|
457
|
+
}
|
|
458
|
+
if let Some(recording) = &mut self.recording {
|
|
459
|
+
recording.push(concrete.clone());
|
|
460
|
+
}
|
|
461
|
+
self.live_instances.insert(concrete.clone());
|
|
462
|
+
concrete
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
fn expression(&mut self, expression: &UiExpression, scopes: &[Scope<'_>]) -> UiExpression {
|
|
466
|
+
let boxed =
|
|
467
|
+
|this: &mut Self, value: &UiExpression| Box::new(this.expression(value, scopes));
|
|
468
|
+
match expression {
|
|
469
|
+
UiExpression::Literal { .. } => expression.clone(),
|
|
470
|
+
UiExpression::State { state } => UiExpression::State {
|
|
471
|
+
state: self.rename_state(state, scopes),
|
|
472
|
+
},
|
|
473
|
+
UiExpression::Not { value } => UiExpression::Not {
|
|
474
|
+
value: boxed(self, value),
|
|
475
|
+
},
|
|
476
|
+
UiExpression::Stringify { value } => UiExpression::Stringify {
|
|
477
|
+
value: boxed(self, value),
|
|
478
|
+
},
|
|
479
|
+
UiExpression::Binary {
|
|
480
|
+
operator,
|
|
481
|
+
left,
|
|
482
|
+
right,
|
|
483
|
+
} => UiExpression::Binary {
|
|
484
|
+
operator: *operator,
|
|
485
|
+
left: boxed(self, left),
|
|
486
|
+
right: boxed(self, right),
|
|
487
|
+
},
|
|
488
|
+
UiExpression::Conditional {
|
|
489
|
+
condition,
|
|
490
|
+
then_value,
|
|
491
|
+
otherwise,
|
|
492
|
+
} => UiExpression::Conditional {
|
|
493
|
+
condition: boxed(self, condition),
|
|
494
|
+
then_value: boxed(self, then_value),
|
|
495
|
+
otherwise: boxed(self, otherwise),
|
|
496
|
+
},
|
|
497
|
+
UiExpression::Item { for_each, path } => {
|
|
498
|
+
let value = scopes
|
|
499
|
+
.iter()
|
|
500
|
+
.rev()
|
|
501
|
+
.find(|scope| scope.for_each == for_each)
|
|
502
|
+
.and_then(|scope| value_at_path(&scope.item, path))
|
|
503
|
+
.cloned()
|
|
504
|
+
.unwrap_or(Value::Null);
|
|
505
|
+
UiExpression::Literal { value }
|
|
506
|
+
}
|
|
507
|
+
UiExpression::Record { fields } => UiExpression::Record {
|
|
508
|
+
fields: fields
|
|
509
|
+
.iter()
|
|
510
|
+
.map(|(name, field)| (name.clone(), self.expression(field, scopes)))
|
|
511
|
+
.collect::<BTreeMap<_, _>>(),
|
|
512
|
+
},
|
|
513
|
+
UiExpression::Count { collection } => UiExpression::Count {
|
|
514
|
+
collection: boxed(self, collection),
|
|
515
|
+
},
|
|
516
|
+
UiExpression::Filter {
|
|
517
|
+
collection,
|
|
518
|
+
path,
|
|
519
|
+
operator,
|
|
520
|
+
value,
|
|
521
|
+
} => UiExpression::Filter {
|
|
522
|
+
collection: boxed(self, collection),
|
|
523
|
+
path: path.clone(),
|
|
524
|
+
operator: *operator,
|
|
525
|
+
value: boxed(self, value),
|
|
526
|
+
},
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
fn optional(
|
|
531
|
+
&mut self,
|
|
532
|
+
expression: &Option<UiExpression>,
|
|
533
|
+
scopes: &[Scope<'_>],
|
|
534
|
+
) -> Option<UiExpression> {
|
|
535
|
+
expression
|
|
536
|
+
.as_ref()
|
|
537
|
+
.map(|expression| self.expression(expression, scopes))
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
fn action(&mut self, action: &UiAction, scopes: &[Scope<'_>]) -> UiAction {
|
|
541
|
+
match action {
|
|
542
|
+
UiAction::ToggleState { state, transaction } => UiAction::ToggleState {
|
|
543
|
+
state: self.rename_state(state, scopes),
|
|
544
|
+
transaction: transaction.clone(),
|
|
545
|
+
},
|
|
546
|
+
UiAction::SetState {
|
|
547
|
+
state,
|
|
548
|
+
value,
|
|
549
|
+
transaction,
|
|
550
|
+
} => UiAction::SetState {
|
|
551
|
+
state: self.rename_state(state, scopes),
|
|
552
|
+
value: self.expression(value, scopes),
|
|
553
|
+
transaction: transaction.clone(),
|
|
554
|
+
},
|
|
555
|
+
UiAction::Collection {
|
|
556
|
+
state,
|
|
557
|
+
key_path,
|
|
558
|
+
operation,
|
|
559
|
+
transaction,
|
|
560
|
+
} => UiAction::Collection {
|
|
561
|
+
state: self.rename_state(state, scopes),
|
|
562
|
+
key_path: key_path.clone(),
|
|
563
|
+
operation: match operation {
|
|
564
|
+
UiCollectionOperation::Insert { index, value } => {
|
|
565
|
+
UiCollectionOperation::Insert {
|
|
566
|
+
index: self.expression(index, scopes),
|
|
567
|
+
value: self.expression(value, scopes),
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
UiCollectionOperation::Append { value } => UiCollectionOperation::Append {
|
|
571
|
+
value: self.expression(value, scopes),
|
|
572
|
+
},
|
|
573
|
+
UiCollectionOperation::Remove { key } => UiCollectionOperation::Remove {
|
|
574
|
+
key: self.expression(key, scopes),
|
|
575
|
+
},
|
|
576
|
+
UiCollectionOperation::Move { key, offset } => UiCollectionOperation::Move {
|
|
577
|
+
key: self.expression(key, scopes),
|
|
578
|
+
offset: self.expression(offset, scopes),
|
|
579
|
+
},
|
|
580
|
+
UiCollectionOperation::Update { key, path, value } => {
|
|
581
|
+
UiCollectionOperation::Update {
|
|
582
|
+
key: self.expression(key, scopes),
|
|
583
|
+
path: path.clone(),
|
|
584
|
+
value: self.expression(value, scopes),
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
},
|
|
588
|
+
transaction: transaction.clone(),
|
|
589
|
+
},
|
|
590
|
+
UiAction::Sequence {
|
|
591
|
+
actions,
|
|
592
|
+
transaction,
|
|
593
|
+
} => UiAction::Sequence {
|
|
594
|
+
actions: actions
|
|
595
|
+
.iter()
|
|
596
|
+
.map(|action| self.action(action, scopes))
|
|
597
|
+
.collect(),
|
|
598
|
+
transaction: transaction.clone(),
|
|
599
|
+
},
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
fn base(&mut self, base: &crate::ir::NodeBase, scopes: &[Scope<'_>]) -> crate::ir::NodeBase {
|
|
604
|
+
let mut output = base.clone();
|
|
605
|
+
output.id = format!("{}{}", base.id, Self::suffix(scopes));
|
|
606
|
+
output.identity_key = self.optional(&base.identity_key, scopes);
|
|
607
|
+
if let Some(layout) = &mut output.layout {
|
|
608
|
+
layout.width = layout
|
|
609
|
+
.width
|
|
610
|
+
.take()
|
|
611
|
+
.map(|value| self.expression(&value, scopes));
|
|
612
|
+
layout.height = layout
|
|
613
|
+
.height
|
|
614
|
+
.take()
|
|
615
|
+
.map(|value| self.expression(&value, scopes));
|
|
616
|
+
}
|
|
617
|
+
if let Some(visual) = &mut output.visual {
|
|
618
|
+
visual.opacity = visual
|
|
619
|
+
.opacity
|
|
620
|
+
.take()
|
|
621
|
+
.map(|value| self.expression(&value, scopes));
|
|
622
|
+
visual.translation_x = visual
|
|
623
|
+
.translation_x
|
|
624
|
+
.take()
|
|
625
|
+
.map(|value| self.expression(&value, scopes));
|
|
626
|
+
visual.translation_y = visual
|
|
627
|
+
.translation_y
|
|
628
|
+
.take()
|
|
629
|
+
.map(|value| self.expression(&value, scopes));
|
|
630
|
+
}
|
|
631
|
+
if let Some(accessibility) = &mut output.accessibility {
|
|
632
|
+
accessibility.enabled = accessibility
|
|
633
|
+
.enabled
|
|
634
|
+
.take()
|
|
635
|
+
.map(|value| self.expression(&value, scopes));
|
|
636
|
+
}
|
|
637
|
+
if let Some(lifecycle) = &mut output.lifecycle {
|
|
638
|
+
lifecycle.appear = lifecycle
|
|
639
|
+
.appear
|
|
640
|
+
.take()
|
|
641
|
+
.map(|action| self.action(&action, scopes));
|
|
642
|
+
lifecycle.disappear = lifecycle
|
|
643
|
+
.disappear
|
|
644
|
+
.take()
|
|
645
|
+
.map(|action| self.action(&action, scopes));
|
|
646
|
+
}
|
|
647
|
+
for binding in &mut output.motion {
|
|
648
|
+
binding.value = self.expression(&binding.value, scopes);
|
|
649
|
+
binding.trigger = binding
|
|
650
|
+
.trigger
|
|
651
|
+
.take()
|
|
652
|
+
.map(|value| self.expression(&value, scopes));
|
|
653
|
+
}
|
|
654
|
+
output
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
fn nodes<'t>(
|
|
658
|
+
&mut self,
|
|
659
|
+
nodes: &'t [UiNode],
|
|
660
|
+
scopes: &mut Vec<Scope<'t>>,
|
|
661
|
+
) -> Result<Vec<UiNode>, CollectionError> {
|
|
662
|
+
nodes.iter().map(|node| self.node(node, scopes)).collect()
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
fn node<'t>(
|
|
666
|
+
&mut self,
|
|
667
|
+
node: &'t UiNode,
|
|
668
|
+
scopes: &mut Vec<Scope<'t>>,
|
|
669
|
+
) -> Result<UiNode, CollectionError> {
|
|
670
|
+
Ok(match node {
|
|
671
|
+
UiNode::ForEach {
|
|
672
|
+
base,
|
|
673
|
+
collection,
|
|
674
|
+
key_path,
|
|
675
|
+
children,
|
|
676
|
+
} => {
|
|
677
|
+
let id = format!("{}{}", base.id, Self::suffix(scopes));
|
|
678
|
+
let collection_expression = self.expression(collection, scopes);
|
|
679
|
+
let evaluated = evaluate(&collection_expression, self.state);
|
|
680
|
+
// Only top-level expansions without nested forEach are pure
|
|
681
|
+
// functions of (template, collection, scoped declarations).
|
|
682
|
+
let memoized =
|
|
683
|
+
scopes.is_empty() && self.recording.is_none() && !contains_for_each(children);
|
|
684
|
+
if memoized {
|
|
685
|
+
let previous = self.previous.and_then(|memo| memo.get(&id)).filter(|memo| {
|
|
686
|
+
*memo.collection == evaluated && memo.template.as_ref() == node
|
|
687
|
+
});
|
|
688
|
+
if let Some(memo) = previous {
|
|
689
|
+
for instance in memo.instances.iter() {
|
|
690
|
+
if !self.state.contains_key(instance) {
|
|
691
|
+
let template = instance
|
|
692
|
+
.split_once('[')
|
|
693
|
+
.map_or(instance.as_str(), |(name, _)| name);
|
|
694
|
+
let initial = self
|
|
695
|
+
.model
|
|
696
|
+
.initials
|
|
697
|
+
.get(template)
|
|
698
|
+
.cloned()
|
|
699
|
+
.unwrap_or(Value::Null);
|
|
700
|
+
self.state.insert(instance.clone(), initial);
|
|
701
|
+
}
|
|
702
|
+
self.live_instances.insert(instance.clone());
|
|
703
|
+
}
|
|
704
|
+
self.memo.insert(id.clone(), memo.clone());
|
|
705
|
+
self.reused.push(id.clone());
|
|
706
|
+
return Ok(UiNode::Conditional {
|
|
707
|
+
base: crate::ir::NodeBase {
|
|
708
|
+
id,
|
|
709
|
+
..Default::default()
|
|
710
|
+
},
|
|
711
|
+
condition: UiExpression::Literal {
|
|
712
|
+
value: Value::Bool(true),
|
|
713
|
+
},
|
|
714
|
+
then_nodes: Vec::new(),
|
|
715
|
+
otherwise: Vec::new(),
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
self.recording = Some(Vec::new());
|
|
719
|
+
}
|
|
720
|
+
let recorded_collection = memoized.then(|| Rc::new(evaluated.clone()));
|
|
721
|
+
let items = match evaluated {
|
|
722
|
+
Value::Array(items) => items,
|
|
723
|
+
Value::Null => Vec::new(),
|
|
724
|
+
_ => return Err(CollectionError::NotACollection { collection: id }),
|
|
725
|
+
};
|
|
726
|
+
let mut seen = HashSet::new();
|
|
727
|
+
let mut instances = Vec::with_capacity(items.len() * children.len());
|
|
728
|
+
for item in items {
|
|
729
|
+
let segment = value_at_path(&item, key_path)
|
|
730
|
+
.and_then(key_segment)
|
|
731
|
+
.ok_or_else(|| CollectionError::InvalidKey {
|
|
732
|
+
collection: id.clone(),
|
|
733
|
+
})?;
|
|
734
|
+
if !seen.insert(segment.clone()) {
|
|
735
|
+
return Err(CollectionError::DuplicateKey {
|
|
736
|
+
collection: id,
|
|
737
|
+
key: segment,
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
scopes.push(Scope {
|
|
741
|
+
for_each: &base.id,
|
|
742
|
+
segment,
|
|
743
|
+
item,
|
|
744
|
+
});
|
|
745
|
+
let instance = self.nodes(children, scopes);
|
|
746
|
+
scopes.pop();
|
|
747
|
+
instances.extend(instance?);
|
|
748
|
+
}
|
|
749
|
+
if let Some(collection) = recorded_collection {
|
|
750
|
+
let recorded = self.recording.take().unwrap_or_default();
|
|
751
|
+
self.memo.insert(
|
|
752
|
+
id.clone(),
|
|
753
|
+
ForEachMemo {
|
|
754
|
+
template: Rc::new(node.clone()),
|
|
755
|
+
collection,
|
|
756
|
+
instances: recorded.into(),
|
|
757
|
+
},
|
|
758
|
+
);
|
|
759
|
+
}
|
|
760
|
+
// Materialized as a transparent fragment: like a conditional's
|
|
761
|
+
// active branch, it contributes children without a layout box.
|
|
762
|
+
UiNode::Conditional {
|
|
763
|
+
base: crate::ir::NodeBase {
|
|
764
|
+
id,
|
|
765
|
+
..Default::default()
|
|
766
|
+
},
|
|
767
|
+
condition: UiExpression::Literal {
|
|
768
|
+
value: Value::Bool(true),
|
|
769
|
+
},
|
|
770
|
+
then_nodes: instances,
|
|
771
|
+
otherwise: Vec::new(),
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
UiNode::Column { base, children } => UiNode::Column {
|
|
775
|
+
base: self.base(base, scopes),
|
|
776
|
+
children: self.nodes(children, scopes)?,
|
|
777
|
+
},
|
|
778
|
+
UiNode::Row { base, children } => UiNode::Row {
|
|
779
|
+
base: self.base(base, scopes),
|
|
780
|
+
children: self.nodes(children, scopes)?,
|
|
781
|
+
},
|
|
782
|
+
UiNode::Scroll {
|
|
783
|
+
base,
|
|
784
|
+
axis,
|
|
785
|
+
children,
|
|
786
|
+
} => UiNode::Scroll {
|
|
787
|
+
base: self.base(base, scopes),
|
|
788
|
+
axis: *axis,
|
|
789
|
+
children: self.nodes(children, scopes)?,
|
|
790
|
+
},
|
|
791
|
+
UiNode::Overlay {
|
|
792
|
+
base,
|
|
793
|
+
alignment,
|
|
794
|
+
children,
|
|
795
|
+
} => UiNode::Overlay {
|
|
796
|
+
base: self.base(base, scopes),
|
|
797
|
+
alignment: *alignment,
|
|
798
|
+
children: self.nodes(children, scopes)?,
|
|
799
|
+
},
|
|
800
|
+
UiNode::Conditional {
|
|
801
|
+
base,
|
|
802
|
+
condition,
|
|
803
|
+
then_nodes,
|
|
804
|
+
otherwise,
|
|
805
|
+
} => UiNode::Conditional {
|
|
806
|
+
base: self.base(base, scopes),
|
|
807
|
+
condition: self.expression(condition, scopes),
|
|
808
|
+
then_nodes: self.nodes(then_nodes, scopes)?,
|
|
809
|
+
otherwise: self.nodes(otherwise, scopes)?,
|
|
810
|
+
},
|
|
811
|
+
UiNode::Text { base, value } => UiNode::Text {
|
|
812
|
+
base: self.base(base, scopes),
|
|
813
|
+
value: self.expression(value, scopes),
|
|
814
|
+
},
|
|
815
|
+
UiNode::Panel { base, shape } => UiNode::Panel {
|
|
816
|
+
base: self.base(base, scopes),
|
|
817
|
+
shape: *shape,
|
|
818
|
+
},
|
|
819
|
+
UiNode::TextField {
|
|
820
|
+
base,
|
|
821
|
+
state,
|
|
822
|
+
placeholder,
|
|
823
|
+
secure,
|
|
824
|
+
} => UiNode::TextField {
|
|
825
|
+
base: self.base(base, scopes),
|
|
826
|
+
state: self.rename_state(state, scopes),
|
|
827
|
+
placeholder: placeholder.clone(),
|
|
828
|
+
secure: *secure,
|
|
829
|
+
},
|
|
830
|
+
UiNode::Toggle { base, state, label } => UiNode::Toggle {
|
|
831
|
+
base: self.base(base, scopes),
|
|
832
|
+
state: self.rename_state(state, scopes),
|
|
833
|
+
label: label.clone(),
|
|
834
|
+
},
|
|
835
|
+
UiNode::Progress {
|
|
836
|
+
base,
|
|
837
|
+
value,
|
|
838
|
+
total,
|
|
839
|
+
label,
|
|
840
|
+
} => UiNode::Progress {
|
|
841
|
+
base: self.base(base, scopes),
|
|
842
|
+
value: self.expression(value, scopes),
|
|
843
|
+
total: total.as_ref().map(|total| self.expression(total, scopes)),
|
|
844
|
+
label: label.clone(),
|
|
845
|
+
},
|
|
846
|
+
UiNode::Spacer { base, min_length } => UiNode::Spacer {
|
|
847
|
+
base: self.base(base, scopes),
|
|
848
|
+
min_length: *min_length,
|
|
849
|
+
},
|
|
850
|
+
UiNode::Divider { base } => UiNode::Divider {
|
|
851
|
+
base: self.base(base, scopes),
|
|
852
|
+
},
|
|
853
|
+
UiNode::RadioGroup {
|
|
854
|
+
base,
|
|
855
|
+
state,
|
|
856
|
+
options,
|
|
857
|
+
} => UiNode::RadioGroup {
|
|
858
|
+
base: self.base(base, scopes),
|
|
859
|
+
state: self.rename_state(state, scopes),
|
|
860
|
+
options: options.clone(),
|
|
861
|
+
},
|
|
862
|
+
UiNode::Action {
|
|
863
|
+
base,
|
|
864
|
+
label,
|
|
865
|
+
action,
|
|
866
|
+
} => UiNode::Action {
|
|
867
|
+
base: self.base(base, scopes),
|
|
868
|
+
label: label.clone(),
|
|
869
|
+
action: self.action(action, scopes),
|
|
870
|
+
},
|
|
871
|
+
})
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/// Apply one evaluated keyed mutation to a collection value. Items are found by
|
|
876
|
+
/// key; a mutation producing duplicate or invalid keys fails without effect.
|
|
877
|
+
pub(crate) fn apply_operation(
|
|
878
|
+
collection_name: &str,
|
|
879
|
+
collection: &Value,
|
|
880
|
+
key_path: &[String],
|
|
881
|
+
operation: &EvaluatedOperation,
|
|
882
|
+
) -> Result<Value, CollectionError> {
|
|
883
|
+
let mut items = match collection {
|
|
884
|
+
Value::Array(items) => items.clone(),
|
|
885
|
+
Value::Null => Vec::new(),
|
|
886
|
+
_ => {
|
|
887
|
+
return Err(CollectionError::NotACollection {
|
|
888
|
+
collection: collection_name.to_owned(),
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
};
|
|
892
|
+
let position = |items: &[Value], key: &Value| {
|
|
893
|
+
let wanted = key_segment(key)?;
|
|
894
|
+
items.iter().position(|item| {
|
|
895
|
+
value_at_path(item, key_path)
|
|
896
|
+
.and_then(key_segment)
|
|
897
|
+
.as_deref()
|
|
898
|
+
== Some(&wanted)
|
|
899
|
+
})
|
|
900
|
+
};
|
|
901
|
+
match operation {
|
|
902
|
+
EvaluatedOperation::Insert { index, value } => {
|
|
903
|
+
let index = index.clamp(0.0, items.len() as f64) as usize;
|
|
904
|
+
items.insert(index, value.clone());
|
|
905
|
+
}
|
|
906
|
+
EvaluatedOperation::Append { value } => items.push(value.clone()),
|
|
907
|
+
EvaluatedOperation::Remove { key } => {
|
|
908
|
+
if let Some(index) = position(&items, key) {
|
|
909
|
+
items.remove(index);
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
EvaluatedOperation::Move { key, offset } => {
|
|
913
|
+
if let Some(index) = position(&items, key) {
|
|
914
|
+
let target = (index as f64 + offset.trunc())
|
|
915
|
+
.clamp(0.0, items.len().saturating_sub(1) as f64)
|
|
916
|
+
as usize;
|
|
917
|
+
let item = items.remove(index);
|
|
918
|
+
items.insert(target, item);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
EvaluatedOperation::Update { key, path, value } => {
|
|
922
|
+
if let Some(index) = position(&items, key) {
|
|
923
|
+
let mut cursor = &mut items[index];
|
|
924
|
+
for (depth, field) in path.iter().enumerate() {
|
|
925
|
+
let Some(object) = cursor.as_object_mut() else {
|
|
926
|
+
break;
|
|
927
|
+
};
|
|
928
|
+
if depth + 1 == path.len() {
|
|
929
|
+
object.insert(field.clone(), value.clone());
|
|
930
|
+
break;
|
|
931
|
+
}
|
|
932
|
+
cursor = object
|
|
933
|
+
.entry(field.clone())
|
|
934
|
+
.or_insert_with(|| Value::Object(Default::default()));
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
let mut seen = HashSet::new();
|
|
940
|
+
for item in &items {
|
|
941
|
+
let segment = value_at_path(item, key_path)
|
|
942
|
+
.and_then(key_segment)
|
|
943
|
+
.ok_or_else(|| CollectionError::InvalidKey {
|
|
944
|
+
collection: collection_name.to_owned(),
|
|
945
|
+
})?;
|
|
946
|
+
if !seen.insert(segment.clone()) {
|
|
947
|
+
return Err(CollectionError::DuplicateKey {
|
|
948
|
+
collection: collection_name.to_owned(),
|
|
949
|
+
key: segment,
|
|
950
|
+
});
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
Ok(Value::Array(items))
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
pub(crate) enum EvaluatedOperation {
|
|
957
|
+
Insert {
|
|
958
|
+
index: f64,
|
|
959
|
+
value: Value,
|
|
960
|
+
},
|
|
961
|
+
Append {
|
|
962
|
+
value: Value,
|
|
963
|
+
},
|
|
964
|
+
Remove {
|
|
965
|
+
key: Value,
|
|
966
|
+
},
|
|
967
|
+
Move {
|
|
968
|
+
key: Value,
|
|
969
|
+
offset: f64,
|
|
970
|
+
},
|
|
971
|
+
Update {
|
|
972
|
+
key: Value,
|
|
973
|
+
path: Vec<String>,
|
|
974
|
+
value: Value,
|
|
975
|
+
},
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
impl EvaluatedOperation {
|
|
979
|
+
pub(crate) fn evaluate(
|
|
980
|
+
operation: &UiCollectionOperation,
|
|
981
|
+
state: &HashMap<String, Value>,
|
|
982
|
+
) -> Self {
|
|
983
|
+
let number =
|
|
984
|
+
|expression: &UiExpression| evaluate(expression, state).as_f64().unwrap_or(0.0);
|
|
985
|
+
match operation {
|
|
986
|
+
UiCollectionOperation::Insert { index, value } => Self::Insert {
|
|
987
|
+
index: number(index),
|
|
988
|
+
value: evaluate(value, state),
|
|
989
|
+
},
|
|
990
|
+
UiCollectionOperation::Append { value } => Self::Append {
|
|
991
|
+
value: evaluate(value, state),
|
|
992
|
+
},
|
|
993
|
+
UiCollectionOperation::Remove { key } => Self::Remove {
|
|
994
|
+
key: evaluate(key, state),
|
|
995
|
+
},
|
|
996
|
+
UiCollectionOperation::Move { key, offset } => Self::Move {
|
|
997
|
+
key: evaluate(key, state),
|
|
998
|
+
offset: number(offset),
|
|
999
|
+
},
|
|
1000
|
+
UiCollectionOperation::Update { key, path, value } => Self::Update {
|
|
1001
|
+
key: evaluate(key, state),
|
|
1002
|
+
path: path.clone(),
|
|
1003
|
+
value: evaluate(value, state),
|
|
1004
|
+
},
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
#[cfg(test)]
|
|
1010
|
+
mod tests {
|
|
1011
|
+
use super::*;
|
|
1012
|
+
|
|
1013
|
+
#[test]
|
|
1014
|
+
fn key_segments_are_typed_and_collision_free() {
|
|
1015
|
+
assert_eq!(key_segment(&Value::from("a")).unwrap(), "s:a");
|
|
1016
|
+
assert_ne!(key_segment(&Value::from("1")), key_segment(&Value::from(1)));
|
|
1017
|
+
assert_eq!(
|
|
1018
|
+
key_segment(&Value::from(-0.0)),
|
|
1019
|
+
key_segment(&Value::from(0))
|
|
1020
|
+
);
|
|
1021
|
+
assert_eq!(
|
|
1022
|
+
key_segment(&Value::from("a#b/c:option:1")).unwrap(),
|
|
1023
|
+
"s:a%23b%2Fc%3Aoption%3A1"
|
|
1024
|
+
);
|
|
1025
|
+
assert!(key_segment(&Value::Bool(true)).is_none());
|
|
1026
|
+
assert!(key_segment(&Value::Null).is_none());
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
#[test]
|
|
1030
|
+
fn keyed_operations_address_items_by_key_and_reject_duplicates() {
|
|
1031
|
+
let path = vec!["id".to_owned()];
|
|
1032
|
+
let items = serde_json::json!([{"id": "a"}, {"id": "b"}, {"id": "c"}]);
|
|
1033
|
+
let moved = apply_operation(
|
|
1034
|
+
"rows",
|
|
1035
|
+
&items,
|
|
1036
|
+
&path,
|
|
1037
|
+
&EvaluatedOperation::Move {
|
|
1038
|
+
key: Value::from("c"),
|
|
1039
|
+
offset: -5.0,
|
|
1040
|
+
},
|
|
1041
|
+
)
|
|
1042
|
+
.unwrap();
|
|
1043
|
+
assert_eq!(
|
|
1044
|
+
moved,
|
|
1045
|
+
serde_json::json!([{"id": "c"}, {"id": "a"}, {"id": "b"}])
|
|
1046
|
+
);
|
|
1047
|
+
let duplicate = apply_operation(
|
|
1048
|
+
"rows",
|
|
1049
|
+
&items,
|
|
1050
|
+
&path,
|
|
1051
|
+
&EvaluatedOperation::Append {
|
|
1052
|
+
value: serde_json::json!({"id": "a"}),
|
|
1053
|
+
},
|
|
1054
|
+
);
|
|
1055
|
+
assert_eq!(
|
|
1056
|
+
duplicate,
|
|
1057
|
+
Err(CollectionError::DuplicateKey {
|
|
1058
|
+
collection: "rows".into(),
|
|
1059
|
+
key: "s:a".into()
|
|
1060
|
+
})
|
|
1061
|
+
);
|
|
1062
|
+
let missing = apply_operation(
|
|
1063
|
+
"rows",
|
|
1064
|
+
&items,
|
|
1065
|
+
&path,
|
|
1066
|
+
&EvaluatedOperation::Insert {
|
|
1067
|
+
index: 0.0,
|
|
1068
|
+
value: serde_json::json!({"title": "no key"}),
|
|
1069
|
+
},
|
|
1070
|
+
);
|
|
1071
|
+
assert!(matches!(missing, Err(CollectionError::InvalidKey { .. })));
|
|
1072
|
+
}
|
|
1073
|
+
}
|