fluidattacks-blends-domain 0.6.1

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
use alloc::vec::Vec;

use crate::ast::AstGraph;
use crate::query::adj_ast;
use crate::syntax::builders::declaration_block::build_declaration_block_node;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::{CodeGraph, NodeId};

const IGNORED_LABELS: [&str; 5] = [
    "preprocessor_call",
    "endif_directive",
    "preproc_if",
    "region_directive",
    "endregion_directive",
];

const PREPROC_SKIP: [&str; 3] = ["#if", "parenthesized_expression", "#endif"];

fn has_label(graph: &AstGraph, n_id: NodeId, labels: &[&str]) -> bool {
    graph
        .label_type(n_id)
        .is_some_and(|lt| labels.contains(&lt))
}

pub fn reader(
    args: &mut SyntaxGraphArgs<'_>,
    n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
    let graph = args.ast_graph;
    // Drop the surrounding `{` `}`.
    let all = adj_ast(graph, n_id, None, &[]);
    let inner = all.get(1..all.len().saturating_sub(1)).unwrap_or(&[]);

    let mut c_ids: Vec<NodeId> = inner
        .iter()
        .copied()
        .filter(|c_id| !has_label(graph, *c_id, &IGNORED_LABELS))
        .collect();

    let preproc_children: Vec<NodeId> = inner
        .iter()
        .copied()
        .filter(|c_id| graph.label_type(*c_id) == Some("preproc_if"))
        .flat_map(|pre_id| adj_ast(graph, pre_id, None, &[]))
        .filter(|child| !has_label(graph, *child, &PREPROC_SKIP))
        .collect();
    c_ids.extend(preproc_children);

    build_declaration_block_node(args, n_id, &c_ids).map(Some)
}