use crate::{
ast::{
node_utils::{ensure_node, NodeResolutionError},
AstGraph,
},
CodeGraph, NodeId,
};
use alloc::borrow::ToOwned;
use alloc::string::{String, ToString};
use core::fmt::Write;
fn render_node(
graph: &AstGraph,
id: NodeId,
depth: usize,
out: &mut String,
) -> Result<(), NodeResolutionError> {
let node = ensure_node(graph, id)?;
write!(
out,
"{:indent$}{} {} @{}:{}",
"",
id.0,
node.kind,
node.line,
node.col,
indent = depth.saturating_mul(2)
)
.map_err(|err| NodeResolutionError::Error(err.to_string()))?;
if let Some(text) = &node.text {
write!(out, " {text:?}").map_err(|err| NodeResolutionError::Error(err.to_string()))?;
}
out.push('\n');
for child in graph.ast_children(id) {
render_node(graph, child, depth.saturating_add(1), out)?;
}
Ok(())
}
#[allow(dead_code, reason = "a debugging aid for inspecting parsed trees")]
pub fn render_graph(graph: &AstGraph) -> String {
let mut out = String::new();
match render_node(graph, NodeId(1), 0, &mut out) {
Ok(()) => out,
Err(_) => "[ERROR] graph is corrupt some id are not on the graph".to_owned(),
}
}