fluidattacks-blends-domain 0.37.0

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
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(())
}
/// Renders the graph as an indented tree, one node per line:
/// `id kind @line:col`, followed by the quoted text on leaves.
/// Returns an error line instead when the tree references ids missing from the graph.
#[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(),
    }
}