fluidattacks-blends-domain 0.17.1

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
use crate::{
    query::{adj_ast, label_text},
    syntax::{
        builders::method_invocation::{build_method_invocation_node, DirectChildren},
        fields::Field,
        SyntaxGraphArgs, SyntaxGraphError,
    },
    utilities::text_nodes::node_to_str,
    CodeGraph, NodeId,
};
use alloc::borrow::ToOwned;
use alloc::string::String;
use alloc::vec::Vec;

const FUNCTION: Field<false> = Field::new("function_id");
const OBJECT: Field<false> = Field::new("object_id");
const NAME: Field<true> = Field::new("name_id");
const ARGUMENT: Field<true> = Field::new("arguments_id");

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

    let expr_id = args
        .optional_field_alt(n_id, FUNCTION)
        .or_else(|| args.optional_field_alt(n_id, OBJECT));

    let expr = if let Some(id) = expr_id {
        if graph.label_type(n_id) == Some("member_call_expression") {
            let raw_expr = node_to_str(graph, id);
            let method_name_n_id = args.required_field_alt(n_id, NAME)?;
            let method_name = label_text(graph, method_name_n_id)
                .map_or_else(|| node_to_str(graph, method_name_n_id), str::to_owned);

            let mut exp_tokens: Vec<&str> = raw_expr
                .split("->")
                .map(|part| part.split_once('(').map_or(part, |(head, _)| head))
                .collect();
            exp_tokens.push(method_name.as_str());
            exp_tokens.join("->")
        } else {
            node_to_str(graph, id)
        }
    } else {
        String::new()
    };

    let args_node = args.required_field_alt(n_id, ARGUMENT)?;
    let arguments_id = adj_ast(graph, args_node, Some(1), &[])
        .iter()
        .any(|&c_id| !matches!(graph.label_type(c_id), Some("(" | "," | ")")))
        .then_some(args_node);

    let direct_children = DirectChildren {
        expression_id: expr_id,
        arguments_id,
        ..DirectChildren::default()
    };

    build_method_invocation_node(args, n_id, expr, direct_children, None).map(Some)
}