fluidattacks-blends-domain 0.23.0

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
use crate::{
    query::{match_ast_d, match_ast_group, match_ast_group_d},
    syntax::{
        builders::method_declaration::{
            build_method_declaration_node, MethodDirectChildren, MethodListChildren,
        },
        fields::Field,
        readers::kotlin::class_declaration::extract_modifiers,
        SyntaxGraphArgs, SyntaxGraphError,
    },
    utilities::text_nodes::node_to_str,
    NodeId,
};
use alloc::vec::Vec;

const NAME: Field<false> = Field::new("name_id");

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

    let name = args
        .optional_field_alt(n_id, NAME)
        .map(|name_id| node_to_str(graph, name_id));

    let block_id = match_ast_d(graph, n_id, "function_body", Some(1))
        .or_else(|| match_ast_d(graph, n_id, "statements", Some(1)));

    let parameters_id = match_ast_d(graph, n_id, "function_value_parameters", Some(1));

    let modifiers_group = match_ast_group_d(graph, n_id, "modifiers", Some(1));
    let keep_modifiers = modifiers_group.first().is_none_or(|&first_modifier_id| {
        match_ast_group(graph, first_modifier_id, &["annotation"], Some(1))
            .get("annotation")
            .is_some_and(|ids| !ids.is_empty())
    });
    let modifiers_ids = if keep_modifiers {
        modifiers_group
    } else {
        Vec::new()
    };

    let (_, access_modifiers) = extract_modifiers(graph, n_id);

    let direct_children = MethodDirectChildren {
        block_id,
        parameters_id,
        ..MethodDirectChildren::default()
    };
    let list_children = MethodListChildren {
        modifiers_ids,
        ..MethodListChildren::default()
    };

    build_method_declaration_node(
        args,
        n_id,
        name,
        &direct_children,
        &list_children,
        access_modifiers,
    )
    .map(Some)
}