fluidattacks-blends-domain 0.20.0

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

use crate::query::adj_ast;
use crate::{CodeGraph, NodeId};

#[must_use]
pub fn pair_declaration_value_ids<G: CodeGraph>(
    graph: &G,
    value_field_id: Option<NodeId>,
    count: usize,
) -> Vec<Option<NodeId>> {
    let Some(value_field_id) = value_field_id else {
        return vec![None; count];
    };

    let exprs: Vec<NodeId> = adj_ast(graph, value_field_id, Some(1), &[])
        .into_iter()
        .filter(|c_id| graph.label_type(*c_id) != Some(","))
        .collect();
    if exprs.len() == count {
        return exprs.into_iter().map(Some).collect();
    }

    vec![Some(unwrap_single_expression_list(graph, value_field_id)); count]
}

/// The single expression of a one-element `expression_list`, else `id` itself.
#[must_use]
pub fn unwrap_single_expression_list<G: CodeGraph>(graph: &G, id: NodeId) -> NodeId {
    if graph.label_type(id) == Some("expression_list") {
        if let [only] = *adj_ast(graph, id, None, &[]).as_slice() {
            return only;
        }
    }
    id
}