fluidattacks-blends-domain 0.40.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, fields::Field, SyntaxGraphArgs,
        SyntaxGraphError,
    },
    utilities::text_nodes::node_to_str,
    NodeId,
};
use alloc::borrow::ToOwned;
use alloc::vec::Vec;

const TEXT: Field<false> = Field::new("text_id");

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

    let value = args
        .optional_field_alt(n_id, TEXT)
        .and_then(|text_id| label_text(graph, text_id))
        .map_or_else(|| node_to_str(graph, n_id), ToOwned::to_owned);

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

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