fluidattacks-blends-domain 0.32.0

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

use crate::query::{adj_ast, label_text};
use crate::syntax::builders::switch_section::build_switch_section_node;
use crate::syntax::fields::Field;
use crate::syntax::readers::constants::GO_STATEMENT;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::utilities::text_nodes::node_to_str;
use crate::{CodeGraph, NodeId};

const VALUE: Field<false> = Field::new("value_id");
const TYPE: Field<false> = Field::new("type_id");

pub fn reader(
    args: &mut SyntaxGraphArgs<'_>,
    n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
    let graph = args.ast_graph;
    let childs = adj_ast(graph, n_id, None, &[]);

    let value_id = args
        .optional_field_alt(n_id, VALUE)
        .or_else(|| args.optional_field_alt(n_id, TYPE))
        .or_else(|| childs.first().copied());

    let case_expr = value_id.map_or_else(String::new, |id| {
        label_text(graph, id)
            .filter(|text| !text.is_empty())
            .map_or_else(|| node_to_str(graph, id), ToOwned::to_owned)
    });

    let execution_ids: Vec<NodeId> = childs
        .iter()
        .copied()
        .filter(|&id| {
            graph
                .label_type(id)
                .is_some_and(|kind| GO_STATEMENT.contains(&kind))
        })
        .collect();

    build_switch_section_node(args, n_id, case_expr, &execution_ids).map(Some)
}