fluidattacks-blends-domain 0.44.2

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
use crate::ast::node_utils::{children, AstNodeAndId};
use crate::ast::{AstGraph, AstNode};
use crate::syntax::_node_builders::core::SyntaxNodeBuildError;
use crate::syntax::core_nodes::{CoreSyntaxNodeWithId, TryExpression};
use alloc::borrow::ToOwned;
use alloc::vec::Vec;

/// Builds an error propagation (`expr?`), building the propagated expression
/// with `build_syntax_node`. Comments around the `?` are ignored.
/// Fails unless the node is exactly an expression followed by `?`.
pub fn build_try_expression(
    graph: &AstGraph,
    node: &AstNode,
    build_syntax_node: impl Fn(AstNodeAndId) -> Result<CoreSyntaxNodeWithId, SyntaxNodeBuildError>,
) -> Result<TryExpression, SyntaxNodeBuildError> {
    children(graph, node)
        .map_err(SyntaxNodeBuildError::ResolutionError)
        .map(|all_children| {
            all_children
                .into_iter()
                .filter(|(child, _)| {
                    !matches!(child.kind.as_str(), "line_comment" | "block_comment")
                })
                .collect::<Vec<_>>()
        })
        .and_then(|code_children| match code_children.as_slice() {
            [expression, (operator, _)] if operator.kind == "?" => Ok(*expression),
            _ => Err(SyntaxNodeBuildError::Error(
                "A TryExpression must be an expression followed by `?`".to_owned(),
            )),
        })
        .and_then(build_syntax_node)
        .map(|expression| TryExpression { expression })
}

#[cfg(test)]
mod tests;