1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! Reference resolution pass.
//!
//! Validates that all cross-node references (string IDs) point to
//! existing nodes in the IR tree.
use crate::errors::{Diagnostic, Severity, ValidationResult};
use crate::index::NodeIndex;
use crate::ir::{ChildNode, VoceIr};
use crate::passes::ValidationPass;
pub struct ReferencesPass;
impl ValidationPass for ReferencesPass {
fn name(&self) -> &'static str {
"references"
}
fn run(&self, ir: &VoceIr, index: &NodeIndex, result: &mut ValidationResult) {
let root = match &ir.root {
Some(r) => r,
None => return,
};
// Check semantic_node_id references on the root's children
if let Some(ref children) = root.children {
self.check_children(children, "/root/children", index, result);
}
// Semantic nodes are referenced by visual nodes via semantic_node_id
// (checked in the children walk above). No outbound refs to check here.
}
}
impl ReferencesPass {
fn check_children(
&self,
children: &[ChildNode],
parent_path: &str,
index: &NodeIndex,
result: &mut ValidationResult,
) {
for (i, child) in children.iter().enumerate() {
let path = format!("{parent_path}/{i}");
// REF001: semantic_node_id must resolve
if let Some(ref sem_id) = child.semantic_node_id() {
if !sem_id.is_empty() && !index.contains(sem_id) {
result.diagnostics.push(Diagnostic {
severity: Severity::Error,
code: "REF001".to_string(),
message: format!(
"semantic_node_id \"{sem_id}\" does not reference an existing node"
),
node_path: path.clone(),
pass: self.name().to_string(),
hint: None,
});
}
}
// Type-specific reference checks
match child.type_name() {
"AnimationTransition" => {
if let Some(anim) = child.as_type::<crate::ir::AnimationTransition>() {
self.check_ref(
&anim.target_node_id,
"target_node_id",
"REF005",
&path,
index,
result,
);
}
}
"GestureHandler" => {
if let Some(gh) = child.as_type::<crate::ir::GestureHandler>() {
self.check_ref(
&gh.target_node_id,
"target_node_id",
"REF005",
&path,
index,
result,
);
}
}
"ScrollBinding" => {
if let Some(sb) = child.as_type::<crate::ir::ScrollBinding>() {
self.check_ref(
&sb.target_node_id,
"target_node_id",
"REF006",
&path,
index,
result,
);
}
}
"PhysicsBody" => {
if let Some(pb) = child.as_type::<crate::ir::PhysicsBody>() {
self.check_ref(
&pb.target_node_id,
"target_node_id",
"REF005",
&path,
index,
result,
);
}
}
"LiveRegion" => {
if let Some(lr) = child.as_type::<crate::ir::LiveRegion>() {
self.check_ref(
&lr.target_node_id,
"target_node_id",
"REF005",
&path,
index,
result,
);
}
}
"FocusTrap" => {
if let Some(ft) = child.as_type::<crate::ir::FocusTrap>() {
self.check_ref(
&ft.container_node_id,
"container_node_id",
"REF007",
&path,
index,
result,
);
// initial_focus is a warning, not error
if let Some(ref focus_id) = ft.initial_focus_node_id {
if !focus_id.is_empty() && !index.contains(focus_id) {
result.diagnostics.push(Diagnostic {
severity: Severity::Warning,
code: "REF008".to_string(),
message: format!(
"initial_focus_node_id \"{focus_id}\" does not reference an existing node"
),
node_path: path.clone(),
pass: self.name().to_string(),
hint: None,
});
}
}
}
}
"SubscriptionNode" => {
if let Some(sub) = child.as_type::<crate::ir::SubscriptionNode>() {
self.check_ref(
&sub.target_data_node_id,
"target_data_node_id",
"REF005",
&path,
index,
result,
);
}
}
"ComputeNode" => {
if let Some(cn) = child.as_type::<crate::ir::ComputeNode>() {
if let Some(ref inputs) = cn.inputs {
for (j, input) in inputs.iter().enumerate() {
if let Some(ref src_id) = input.source_node_id {
if !src_id.is_empty() && !index.contains(src_id) {
result.diagnostics.push(Diagnostic {
severity: Severity::Error,
code: "REF009".to_string(),
message: format!(
"ComputeInput source_node_id \"{src_id}\" does not exist"
),
node_path: format!("{path}/inputs/{j}"),
pass: self.name().to_string(),
hint: None,
});
}
}
}
}
}
}
_ => {}
}
// Recurse
if let Some(grandchildren) = child.children() {
self.check_children(&grandchildren, &format!("{path}/children"), index, result);
}
}
}
/// Check a single optional reference field.
fn check_ref(
&self,
field: &Option<String>,
field_name: &str,
code: &str,
path: &str,
index: &NodeIndex,
result: &mut ValidationResult,
) {
if let Some(target_id) = field {
if !target_id.is_empty() && !index.contains(target_id) {
result.diagnostics.push(Diagnostic {
severity: Severity::Error,
code: code.to_string(),
message: format!(
"{field_name} \"{target_id}\" does not reference an existing node"
),
node_path: path.to_string(),
pass: self.name().to_string(),
hint: None,
});
}
}
}
}