fluidattacks-blends-domain 0.22.0

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
use crate::{
    query::{adj_ast, label_text, match_ast_group_d},
    syntax::{
        builders::string_literal::build_string_literal_node, SyntaxGraphArgs, SyntaxGraphError,
    },
    utilities::text_nodes::node_to_str,
    CodeGraph, NodeId,
};
use alloc::borrow::ToOwned;
use alloc::vec::Vec;

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

    let literal_text = label_text(graph, n_id)
        .filter(|text| !text.is_empty())
        .map_or_else(|| node_to_str(graph, n_id), str::to_owned);

    let mut int_subs: Vec<NodeId> = match_ast_group_d(graph, n_id, "interpolation", Some(1))
        .into_iter()
        .filter_map(|id| adj_ast(graph, id, Some(1), &[]).get(1).copied())
        .collect();

    let vars_content = adj_ast(graph, n_id, Some(1), &[]);
    for window in vars_content.windows(2) {
        let &[node_id, next_node] = window else {
            continue;
        };
        if graph.label_type(node_id) == Some("string_content")
            && label_text(graph, node_id) == Some("$")
            && label_text(graph, next_node)
                .is_some_and(|text| text.starts_with(|c: char| c.is_ascii_alphabetic() || c == '_'))
        {
            int_subs.push(next_node);
        }
    }

    build_string_literal_node(args, n_id, literal_text, &int_subs).map(Some)
}