fluidattacks-blends-domain 0.2.0

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
//! Counterpart of `blends/syntax/builders/parameter.py` (the `StackGraph`
//! parameter wiring and annotation references are excluded: the migration
//! covers only AST, Syntax and CFG).

use alloc::string::String;

use crate::syntax::builders::utils::register_symbol_in_scope;
use crate::syntax::SyntaxNode;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::NodeId;

#[allow(
    clippy::too_many_arguments,
    reason = "mirrors the Python build_parameter_node inputs"
)]
pub fn build_parameter_node(
    args: &mut SyntaxGraphArgs<'_>,
    n_id: NodeId,
    variable: Option<String>,
    variable_type: Option<String>,
    value_id: Option<NodeId>,
    c_ids: &[NodeId],
    variable_id: Option<NodeId>,
    modifier: Option<String>,
) -> Result<NodeId, SyntaxGraphError> {
    let node_id = variable_id.unwrap_or(n_id);
    let variable = variable.filter(|value| !value.is_empty());

    args.syntax_graph.add_node(
        node_id,
        SyntaxNode::Parameter {
            variable: variable.clone(),
            variable_type: variable_type.filter(|value| !value.is_empty()),
            value_id,
            parameter_mode: modifier.filter(|value| !value.is_empty()),
        },
    );

    if let Some(value_id) = value_id {
        let built = args.generic(value_id)?;
        args.syntax_graph.add_ast_edge(node_id, built);
    }

    for c_id in c_ids {
        let built = args.generic(*c_id)?;
        args.syntax_graph.add_ast_edge(node_id, built);
    }

    if let Some(variable) = variable {
        register_symbol_in_scope(args.syntax_graph, args.metadata, variable, node_id);
    }

    Ok(node_id)
}