fluidattacks-blends-domain 0.2.0

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
use crate::query::{adj_ast, match_ast_d, match_ast_group_d};
use crate::syntax::builders::variable_declaration::build_variable_declaration_node;
use crate::syntax::fields::c_sharp::variable_declaration;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::utilities::text_nodes::node_to_str;
use crate::{CodeGraph, NodeId};

pub fn reader(
    args: &mut SyntaxGraphArgs<'_>,
    n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
    let graph = args.ast_graph;
    let decl_id = match_ast_d(graph, n_id, "variable_declaration", Some(1))
        .or_else(|| adj_ast(graph, n_id, None, &[]).into_iter().next())
        .ok_or(SyntaxGraphError::UnexpectedAstShape)?;

    let var_type = node_to_str(
        graph,
        args.required_field_alt(decl_id, variable_declaration::TYPE)?,
    );

    let var_decl_id = match_ast_d(graph, decl_id, "variable_declarator", Some(1))
        .or_else(|| adj_ast(graph, decl_id, None, &[]).into_iter().next_back())
        .ok_or(SyntaxGraphError::UnexpectedAstShape)?;

    let identifier_ids = match_ast_group_d(graph, var_decl_id, "identifier", Some(1));
    let name_id = match identifier_ids.first() {
        Some(&identifier_id) => identifier_id,
        None => adj_ast(graph, var_decl_id, None, &[])
            .into_iter()
            .next()
            .ok_or(SyntaxGraphError::UnexpectedAstShape)?,
    };
    let var_name = node_to_str(graph, name_id);

    let value_id = if identifier_ids.len() > 1 {
        identifier_ids.last().copied()
    } else {
        match_ast_d(graph, decl_id, "variable_declarator", Some(1)).and_then(|declarator_id| {
            adj_ast(graph, declarator_id, None, &[])
                .into_iter()
                .find(|child| !matches!(graph.label_type(*child), Some("identifier" | "=")))
        })
    };

    build_variable_declaration_node(args, n_id, var_name, Some(var_type), value_id, None, None)
        .map(Some)
}