use std::collections::{HashMap, HashSet};
use crate::ir::{ChildNode, VoceIr};
#[derive(Debug, Clone)]
pub struct NodeEntry {
pub path: String,
pub type_name: String,
pub parent_id: Option<String>,
}
#[derive(Debug, Default)]
pub struct NodeIndex {
nodes: HashMap<String, NodeEntry>,
pub duplicates: Vec<(String, String, String)>, by_type: HashMap<String, Vec<String>>,
}
impl NodeIndex {
pub fn build(ir: &VoceIr) -> Self {
let mut index = NodeIndex::default();
if let Some(ref root) = ir.root {
if let Some(ref id) = root.node_id {
index.insert(id.clone(), "/root", "ViewRoot", None);
}
if let Some(ref sems) = root.semantic_nodes {
for (i, sem) in sems.iter().enumerate() {
if let Some(ref id) = sem.node_id {
let path = format!("/root/semantic_nodes/{i}");
index.insert(id.clone(), &path, "SemanticNode", root.node_id.clone());
}
}
}
if let Some(ref children) = root.children {
index.walk_children(children, "/root/children", root.node_id.clone());
}
}
index
}
fn walk_children(
&mut self,
children: &[ChildNode],
parent_path: &str,
parent_id: Option<String>,
) {
for (i, child) in children.iter().enumerate() {
let path = format!("{parent_path}/{i}");
let type_name = child.type_name().to_string();
if let Some(id) = child.node_id() {
self.insert(id.clone(), &path, &type_name, parent_id.clone());
if let Some(grandchildren) = child.children() {
let child_path = format!("{path}/children");
self.walk_children(&grandchildren, &child_path, Some(id));
}
}
}
}
fn insert(&mut self, id: String, path: &str, type_name: &str, parent_id: Option<String>) {
if let Some(existing) = self.nodes.get(&id) {
self.duplicates
.push((id.clone(), existing.path.clone(), path.to_string()));
}
self.nodes.insert(
id.clone(),
NodeEntry {
path: path.to_string(),
type_name: type_name.to_string(),
parent_id,
},
);
self.by_type
.entry(type_name.to_string())
.or_default()
.push(id);
}
pub fn get(&self, id: &str) -> Option<&NodeEntry> {
self.nodes.get(id)
}
pub fn contains(&self, id: &str) -> bool {
self.nodes.contains_key(id)
}
pub fn by_type(&self, type_name: &str) -> &[String] {
self.by_type.get(type_name).map_or(&[], |v| v.as_slice())
}
pub fn all_ids(&self) -> HashSet<&str> {
self.nodes.keys().map(|s| s.as_str()).collect()
}
pub fn path(&self, id: &str) -> &str {
self.nodes.get(id).map_or("<unknown>", |e| e.path.as_str())
}
}