voce_validator/passes/
structural.rs1use crate::errors::{Diagnostic, Severity, ValidationResult};
7use crate::index::NodeIndex;
8use crate::ir::{ChildNode, VoceIr};
9use crate::passes::ValidationPass;
10
11pub struct StructuralPass;
12
13impl ValidationPass for StructuralPass {
14 fn name(&self) -> &'static str {
15 "structural"
16 }
17
18 fn run(&self, ir: &VoceIr, index: &NodeIndex, result: &mut ValidationResult) {
19 if ir.root.is_none() {
21 result.diagnostics.push(Diagnostic {
22 severity: Severity::Error,
23 code: "STR001".to_string(),
24 message: "Document is missing a root ViewRoot".to_string(),
25 node_path: "/".to_string(),
26 pass: self.name().to_string(),
27 hint: None,
28 });
29 return; }
31
32 let root = ir.root.as_ref().unwrap();
33
34 if root.node_id.is_none() || root.node_id.as_deref() == Some("") {
36 result.diagnostics.push(Diagnostic {
37 severity: Severity::Error,
38 code: "STR001".to_string(),
39 message: "ViewRoot is missing a node_id".to_string(),
40 node_path: "/root".to_string(),
41 pass: self.name().to_string(),
42 hint: None,
43 });
44 }
45
46 for (id, path1, path2) in &index.duplicates {
48 result.diagnostics.push(Diagnostic {
49 severity: Severity::Error,
50 code: "STR002".to_string(),
51 message: format!("Duplicate node_id \"{id}\" found at {path1} and {path2}"),
52 node_path: path2.clone(),
53 pass: self.name().to_string(),
54 hint: None,
55 });
56 }
57
58 if let Some(ref children) = root.children {
60 self.check_children(children, "/root/children", result);
61 }
62 }
63}
64
65impl StructuralPass {
66 fn check_children(
67 &self,
68 children: &[ChildNode],
69 parent_path: &str,
70 result: &mut ValidationResult,
71 ) {
72 for (i, child) in children.iter().enumerate() {
73 let path = format!("{parent_path}/{i}");
74
75 if child.node_id().is_none() {
77 result.diagnostics.push(Diagnostic {
78 severity: Severity::Error,
79 code: "STR002".to_string(),
80 message: format!("{} node is missing a node_id", child.type_name()),
81 node_path: path.clone(),
82 pass: "structural".to_string(),
83 hint: None,
84 });
85 }
86
87 if child.type_name() == "TextNode" {
89 if let Some(text) = child.as_type::<crate::ir::TextNode>() {
90 let has_content = text.content.as_ref().is_some_and(|c| !c.is_empty());
91 let has_binding = text.content_binding.is_some();
92 let has_localized = text.localized_content.is_some();
93
94 if !has_content && !has_binding && !has_localized {
95 result.diagnostics.push(Diagnostic {
96 severity: Severity::Error,
97 code: "STR004".to_string(),
98 message:
99 "TextNode has no content, content_binding, or localized_content"
100 .to_string(),
101 node_path: path.clone(),
102 pass: "structural".to_string(),
103 hint: None,
104 });
105 }
106 }
107 }
108
109 if child.type_name() == "MediaNode" {
111 if let Some(media) = child.as_type::<crate::ir::MediaNode>() {
112 if media.src.as_ref().is_none_or(|s| s.is_empty()) {
113 result.diagnostics.push(Diagnostic {
114 severity: Severity::Error,
115 code: "STR005".to_string(),
116 message: "MediaNode is missing src".to_string(),
117 node_path: path.clone(),
118 pass: "structural".to_string(),
119 hint: None,
120 });
121 }
122 }
123 }
124
125 if let Some(grandchildren) = child.children() {
127 self.check_children(&grandchildren, &format!("{path}/children"), result);
128 }
129 }
130 }
131}