fluidattacks-blends-domain 0.37.0

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

use crate::query::{adj_ast, label_text, match_ast_d};
use crate::syntax::builders::array_node::build_array_node;
use crate::syntax::builders::string_literal::build_string_literal_node;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::{CodeGraph, NodeId};

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

    if c_ids.len() == 1 {
        if let Some(literal_id) = match_ast_d(graph, n_id, "interpreted_string_literal", None) {
            if let Some(value) = label_text(graph, literal_id) {
                let value = value.to_owned();
                return build_string_literal_node(args, n_id, value, &[]).map(Some);
            }
        }
    }

    let filtered: Vec<NodeId> = c_ids
        .iter()
        .copied()
        .filter(|c_id| !matches!(graph.label_type(*c_id), Some("," | ":")))
        .collect();

    // The comma separated expressions of an assignment, a short declaration,
    // a range clause or a return build as a list.
    build_array_node(args, n_id, &filtered, None).map(Some)
}