Skip to main content

voce_validator/
ir.rs

1//! Serde-deserializable IR model for validation.
2//!
3//! These structs mirror the FlatBuffers JSON canonical format.
4//! They are used by the validator to traverse the IR tree, build
5//! indexes, and run validation passes. Fields that validation
6//! passes don't inspect are captured as `serde_json::Value` to
7//! ensure round-trip fidelity without requiring exhaustive typing.
8
9use serde::Deserialize;
10
11/// The top-level IR document.
12#[derive(Debug, Clone, Deserialize)]
13pub struct VoceIr {
14    #[serde(default)]
15    pub schema_version_major: i32,
16    #[serde(default)]
17    pub schema_version_minor: i32,
18    pub root: Option<ViewRoot>,
19    pub routes: Option<RouteMap>,
20    pub theme: Option<ThemeNode>,
21    pub alternate_themes: Option<Vec<ThemeNode>>,
22    pub auth: Option<AuthContextNode>,
23    pub i18n: Option<I18nConfig>,
24}
25
26/// ViewRoot — document root, one per route.
27#[derive(Debug, Clone, Deserialize)]
28pub struct ViewRoot {
29    pub node_id: Option<String>,
30    pub children: Option<Vec<ChildNode>>,
31    pub document_language: Option<String>,
32    pub text_direction: Option<String>,
33    pub semantic_nodes: Option<Vec<SemanticNode>>,
34    pub metadata: Option<PageMetadata>,
35}
36
37/// ChildNode — wrapper for the union type.
38/// FlatBuffers JSON format: `{ "value_type": "TypeName", "value": { ... } }`
39#[derive(Debug, Clone, Deserialize)]
40pub struct ChildNode {
41    pub value_type: Option<String>,
42    #[serde(default)]
43    pub value: serde_json::Value,
44}
45
46// ─── Layout Nodes ───────────────────────────────────────────────
47
48#[derive(Debug, Clone, Deserialize)]
49pub struct Container {
50    pub node_id: Option<String>,
51    pub children: Option<Vec<ChildNode>>,
52    pub layout: Option<String>,
53    pub semantic_node_id: Option<String>,
54}
55
56#[derive(Debug, Clone, Deserialize)]
57pub struct Surface {
58    pub node_id: Option<String>,
59    pub children: Option<Vec<ChildNode>>,
60    pub decorative: Option<bool>,
61    pub semantic_node_id: Option<String>,
62}
63
64#[derive(Debug, Clone, Deserialize)]
65pub struct TextNode {
66    pub node_id: Option<String>,
67    pub content: Option<String>,
68    pub content_binding: Option<serde_json::Value>,
69    pub localized_content: Option<LocalizedString>,
70    pub heading_level: Option<i8>,
71    pub semantic_node_id: Option<String>,
72}
73
74#[derive(Debug, Clone, Deserialize)]
75pub struct MediaNode {
76    pub node_id: Option<String>,
77    pub src: Option<String>,
78    pub alt: Option<String>,
79    pub decorative: Option<bool>,
80    pub semantic_node_id: Option<String>,
81}
82
83// ─── State Nodes ────────────────────────────────────────────────
84
85#[derive(Debug, Clone, Deserialize)]
86pub struct StateMachine {
87    pub node_id: Option<String>,
88    pub name: Option<String>,
89    pub states: Option<Vec<State>>,
90    pub transitions: Option<Vec<Transition>>,
91}
92
93#[derive(Debug, Clone, Deserialize)]
94pub struct State {
95    pub name: Option<String>,
96    #[serde(default)]
97    pub initial: bool,
98    #[serde(default)]
99    pub terminal: bool,
100}
101
102#[derive(Debug, Clone, Deserialize)]
103pub struct Transition {
104    pub event: Option<String>,
105    pub from: Option<String>,
106    pub to: Option<String>,
107    pub guard: Option<String>,
108    pub effect: Option<String>,
109}
110
111#[derive(Debug, Clone, Deserialize)]
112pub struct DataNode {
113    pub node_id: Option<String>,
114    pub name: Option<String>,
115    pub source: Option<serde_json::Value>,
116    pub auth_required: Option<bool>,
117    pub loading_state_machine: Option<String>,
118    pub cache_tags: Option<Vec<String>>,
119}
120
121#[derive(Debug, Clone, Deserialize)]
122pub struct ComputeNode {
123    pub node_id: Option<String>,
124    pub inputs: Option<Vec<ComputeInput>>,
125    pub expression: Option<String>,
126}
127
128#[derive(Debug, Clone, Deserialize)]
129pub struct ComputeInput {
130    pub name: Option<String>,
131    pub source_node_id: Option<String>,
132    pub field_path: Option<String>,
133}
134
135#[derive(Debug, Clone, Deserialize)]
136pub struct EffectNode {
137    pub node_id: Option<String>,
138    pub effect_type: Option<String>,
139    pub idempotent: Option<bool>,
140}
141
142#[derive(Debug, Clone, Deserialize)]
143pub struct ContextNode {
144    pub node_id: Option<String>,
145    pub name: Option<String>,
146}
147
148// ─── Motion Nodes ───────────────────────────────────────────────
149
150#[derive(Debug, Clone, Deserialize)]
151pub struct AnimationTransition {
152    pub node_id: Option<String>,
153    pub target_node_id: Option<String>,
154    pub trigger_state_machine: Option<String>,
155    pub trigger_event: Option<String>,
156    pub reduced_motion: Option<ReducedMotion>,
157    pub duration: Option<serde_json::Value>,
158}
159
160#[derive(Debug, Clone, Deserialize)]
161pub struct Sequence {
162    pub node_id: Option<String>,
163    pub steps: Option<Vec<SequenceStep>>,
164    pub reduced_motion: Option<ReducedMotion>,
165}
166
167#[derive(Debug, Clone, Deserialize)]
168pub struct SequenceStep {
169    pub transition_id: Option<String>,
170    #[serde(default)]
171    pub parallel: bool,
172}
173
174#[derive(Debug, Clone, Deserialize)]
175pub struct GestureHandler {
176    pub node_id: Option<String>,
177    pub target_node_id: Option<String>,
178    pub gesture_type: Option<String>,
179    pub trigger_event: Option<String>,
180    pub trigger_state_machine: Option<String>,
181    pub keyboard_key: Option<String>,
182}
183
184#[derive(Debug, Clone, Deserialize)]
185pub struct ScrollBinding {
186    pub node_id: Option<String>,
187    pub target_node_id: Option<String>,
188    pub reduced_motion: Option<ReducedMotion>,
189}
190
191#[derive(Debug, Clone, Deserialize)]
192pub struct PhysicsBody {
193    pub node_id: Option<String>,
194    pub target_node_id: Option<String>,
195    #[serde(default)]
196    pub interruptible: bool,
197}
198
199#[derive(Debug, Clone, Deserialize)]
200pub struct ReducedMotion {
201    pub strategy: Option<String>,
202    pub simplified_properties: Option<Vec<serde_json::Value>>,
203    pub reduced_duration: Option<serde_json::Value>,
204}
205
206// ─── Navigation Nodes ───────────────────────────────────────────
207
208#[derive(Debug, Clone, Deserialize)]
209pub struct RouteMap {
210    pub node_id: Option<String>,
211    pub routes: Option<Vec<RouteEntry>>,
212    pub not_found_route: Option<String>,
213}
214
215#[derive(Debug, Clone, Deserialize)]
216pub struct RouteEntry {
217    pub path: Option<String>,
218    pub name: Option<String>,
219    pub view_root_id: Option<String>,
220    pub guard: Option<RouteGuard>,
221    pub children: Option<Vec<RouteEntry>>,
222}
223
224#[derive(Debug, Clone, Deserialize)]
225pub struct RouteGuard {
226    #[serde(default)]
227    pub requires_auth: bool,
228    pub required_roles: Option<Vec<String>>,
229    pub redirect_on_fail: Option<String>,
230}
231
232// ─── A11y Nodes ─────────────────────────────────────────────────
233
234#[derive(Debug, Clone, Deserialize)]
235pub struct SemanticNode {
236    pub node_id: Option<String>,
237    pub role: Option<String>,
238    pub label: Option<String>,
239    pub labelled_by: Option<String>,
240    pub described_by: Option<String>,
241    pub controls: Option<String>,
242    pub heading_level: Option<i8>,
243    pub tab_index: Option<i32>,
244}
245
246#[derive(Debug, Clone, Deserialize)]
247pub struct LiveRegion {
248    pub node_id: Option<String>,
249    pub target_node_id: Option<String>,
250    pub politeness: Option<String>,
251}
252
253#[derive(Debug, Clone, Deserialize)]
254pub struct FocusTrap {
255    pub node_id: Option<String>,
256    pub container_node_id: Option<String>,
257    pub initial_focus_node_id: Option<String>,
258    pub escape_behavior: Option<String>,
259    pub escape_state_machine: Option<String>,
260    pub escape_event: Option<String>,
261}
262
263// ─── Theming Nodes ──────────────────────────────────────────────
264
265#[derive(Debug, Clone, Deserialize)]
266pub struct ThemeNode {
267    pub node_id: Option<String>,
268    pub name: Option<String>,
269    pub colors: Option<serde_json::Value>,
270    pub typography: Option<serde_json::Value>,
271}
272
273#[derive(Debug, Clone, Deserialize)]
274pub struct PersonalizationSlot {
275    pub node_id: Option<String>,
276    pub variants: Option<Vec<serde_json::Value>>,
277}
278
279#[derive(Debug, Clone, Deserialize)]
280pub struct ResponsiveRule {
281    pub node_id: Option<String>,
282    pub breakpoints: Option<Vec<serde_json::Value>>,
283}
284
285// ─── Data Nodes ─────────────────────────────────────────────────
286
287#[derive(Debug, Clone, Deserialize)]
288pub struct ActionNode {
289    pub node_id: Option<String>,
290    pub source: Option<serde_json::Value>,
291    pub method: Option<String>,
292    pub auth_required: Option<bool>,
293    pub csrf_protected: Option<bool>,
294    pub invalidates: Option<Vec<String>>,
295    pub invalidate_tags: Option<Vec<String>>,
296}
297
298#[derive(Debug, Clone, Deserialize)]
299pub struct SubscriptionNode {
300    pub node_id: Option<String>,
301    pub target_data_node_id: Option<String>,
302}
303
304#[derive(Debug, Clone, Deserialize)]
305pub struct AuthContextNode {
306    pub node_id: Option<String>,
307    pub provider: Option<String>,
308    pub login_action_id: Option<String>,
309    pub logout_action_id: Option<String>,
310}
311
312#[derive(Debug, Clone, Deserialize)]
313pub struct ContentSlot {
314    pub node_id: Option<String>,
315    pub content_key: Option<String>,
316    pub cache_strategy: Option<String>,
317}
318
319#[derive(Debug, Clone, Deserialize)]
320pub struct RichTextNode {
321    pub node_id: Option<String>,
322    pub blocks: Option<Vec<serde_json::Value>>,
323}
324
325// ─── Form Nodes ─────────────────────────────────────────────────
326
327#[derive(Debug, Clone, Deserialize)]
328pub struct FormNode {
329    pub node_id: Option<String>,
330    pub fields: Option<Vec<FormField>>,
331    pub submission: Option<FormSubmission>,
332    pub semantic_node_id: Option<String>,
333}
334
335#[derive(Debug, Clone, Deserialize)]
336pub struct FormField {
337    pub name: Option<String>,
338    pub field_type: Option<String>,
339    pub label: Option<String>,
340    pub validations: Option<Vec<ValidationRule>>,
341    pub semantic_node_id: Option<String>,
342    pub autocomplete: Option<String>,
343    pub description: Option<String>,
344}
345
346#[derive(Debug, Clone, Deserialize)]
347pub struct ValidationRule {
348    pub rule_type: Option<String>,
349    pub value: Option<String>,
350    pub message: Option<String>,
351}
352
353#[derive(Debug, Clone, Deserialize)]
354pub struct FormSubmission {
355    pub action_node_id: Option<String>,
356    #[serde(default)]
357    pub progressive: bool,
358}
359
360// ─── SEO ────────────────────────────────────────────────────────
361
362#[derive(Debug, Clone, Deserialize)]
363pub struct PageMetadata {
364    pub title: Option<String>,
365    pub title_template: Option<String>,
366    pub description: Option<String>,
367    pub canonical_url: Option<String>,
368    pub open_graph: Option<OpenGraphData>,
369    pub twitter_card: Option<serde_json::Value>,
370    pub alternates: Option<Vec<AlternateLink>>,
371    pub structured_data: Option<Vec<serde_json::Value>>,
372}
373
374#[derive(Debug, Clone, Deserialize)]
375pub struct OpenGraphData {
376    pub title: Option<String>,
377    pub description: Option<String>,
378    pub image: Option<String>,
379}
380
381#[derive(Debug, Clone, Deserialize)]
382pub struct AlternateLink {
383    pub hreflang: Option<String>,
384    pub href: Option<String>,
385}
386
387// ─── i18n ───────────────────────────────────────────────────────
388
389#[derive(Debug, Clone, Deserialize)]
390pub struct LocalizedString {
391    pub message_key: Option<String>,
392    pub default_value: Option<String>,
393    pub parameters: Option<Vec<serde_json::Value>>,
394    pub description: Option<String>,
395}
396
397#[derive(Debug, Clone, Deserialize)]
398pub struct I18nConfig {
399    pub default_locale: Option<String>,
400    pub supported_locales: Option<Vec<String>>,
401    pub mode: Option<String>,
402}
403
404// ─── Node Extraction Helper ─────────────────────────────────────
405
406impl ChildNode {
407    /// Try to extract the node_id from the inner value, regardless of type.
408    pub fn node_id(&self) -> Option<String> {
409        self.value
410            .get("node_id")
411            .and_then(|v| v.as_str())
412            .map(String::from)
413    }
414
415    /// Get the type discriminator.
416    pub fn type_name(&self) -> &str {
417        self.value_type.as_deref().unwrap_or("NONE")
418    }
419
420    /// Does this node type have children?
421    pub fn children(&self) -> Option<Vec<ChildNode>> {
422        self.value
423            .get("children")
424            .and_then(|v| serde_json::from_value(v.clone()).ok())
425    }
426
427    /// Get the semantic_node_id if present.
428    pub fn semantic_node_id(&self) -> Option<String> {
429        self.value
430            .get("semantic_node_id")
431            .and_then(|v| v.as_str())
432            .map(String::from)
433    }
434
435    /// Deserialize the inner value as a specific type.
436    pub fn as_type<T: serde::de::DeserializeOwned>(&self) -> Option<T> {
437        serde_json::from_value(self.value.clone()).ok()
438    }
439}