fluidattacks-blends-domain 0.3.0

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
use crate::query::{adj_ast, match_ast_d};
use crate::syntax::builders::variable_declaration::build_variable_declaration_node;
use crate::syntax::fields::c_sharp::variable_declaration;
use crate::syntax::readers::c_sharp::common::access_modifiers;
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 modifiers = access_modifiers(graph, n_id);
    let var_id = match_ast_d(graph, n_id, "variable_declaration", Some(1)).unwrap_or(n_id);

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

    let var_decl_id = match_ast_d(graph, var_id, "variable_declarator", Some(1))
        .or_else(|| adj_ast(graph, var_id, None, &[]).into_iter().next())
        .ok_or(SyntaxGraphError::UnexpectedAstShape)?;
    let var_name = match_ast_d(graph, var_decl_id, "identifier", Some(1)).map_or_else(
        || node_to_str(graph, var_decl_id),
        |id| node_to_str(graph, id),
    );

    let value_id = match_ast_d(graph, var_id, "variable_declarator", Some(1)).and_then(|decl_id| {
        adj_ast(graph, decl_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,
        modifiers,
    )
    .map(Some)
}