use crate::errors::{Diagnostic, Severity, ValidationResult};
use crate::index::NodeIndex;
use crate::ir::{ChildNode, VoceIr};
use crate::passes::ValidationPass;
pub struct StructuralPass;
impl ValidationPass for StructuralPass {
fn name(&self) -> &'static str {
"structural"
}
fn run(&self, ir: &VoceIr, index: &NodeIndex, result: &mut ValidationResult) {
if ir.root.is_none() {
result.diagnostics.push(Diagnostic {
severity: Severity::Error,
code: "STR001".to_string(),
message: "Document is missing a root ViewRoot".to_string(),
node_path: "/".to_string(),
pass: self.name().to_string(),
hint: None,
});
return; }
let root = ir.root.as_ref().unwrap();
if root.node_id.is_none() || root.node_id.as_deref() == Some("") {
result.diagnostics.push(Diagnostic {
severity: Severity::Error,
code: "STR001".to_string(),
message: "ViewRoot is missing a node_id".to_string(),
node_path: "/root".to_string(),
pass: self.name().to_string(),
hint: None,
});
}
for (id, path1, path2) in &index.duplicates {
result.diagnostics.push(Diagnostic {
severity: Severity::Error,
code: "STR002".to_string(),
message: format!("Duplicate node_id \"{id}\" found at {path1} and {path2}"),
node_path: path2.clone(),
pass: self.name().to_string(),
hint: None,
});
}
if let Some(ref children) = root.children {
self.check_children(children, "/root/children", result);
}
}
}
impl StructuralPass {
fn check_children(
&self,
children: &[ChildNode],
parent_path: &str,
result: &mut ValidationResult,
) {
for (i, child) in children.iter().enumerate() {
let path = format!("{parent_path}/{i}");
if child.node_id().is_none() {
result.diagnostics.push(Diagnostic {
severity: Severity::Error,
code: "STR002".to_string(),
message: format!("{} node is missing a node_id", child.type_name()),
node_path: path.clone(),
pass: "structural".to_string(),
hint: None,
});
}
if child.type_name() == "TextNode" {
if let Some(text) = child.as_type::<crate::ir::TextNode>() {
let has_content = text.content.as_ref().is_some_and(|c| !c.is_empty());
let has_binding = text.content_binding.is_some();
let has_localized = text.localized_content.is_some();
if !has_content && !has_binding && !has_localized {
result.diagnostics.push(Diagnostic {
severity: Severity::Error,
code: "STR004".to_string(),
message:
"TextNode has no content, content_binding, or localized_content"
.to_string(),
node_path: path.clone(),
pass: "structural".to_string(),
hint: None,
});
}
}
}
if child.type_name() == "MediaNode" {
if let Some(media) = child.as_type::<crate::ir::MediaNode>() {
if media.src.as_ref().is_none_or(|s| s.is_empty()) {
result.diagnostics.push(Diagnostic {
severity: Severity::Error,
code: "STR005".to_string(),
message: "MediaNode is missing src".to_string(),
node_path: path.clone(),
pass: "structural".to_string(),
hint: None,
});
}
}
}
if let Some(grandchildren) = child.children() {
self.check_children(&grandchildren, &format!("{path}/children"), result);
}
}
}
}