fluidattacks-blends-domain 0.40.0

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
use crate::ast::node_utils::ensure_only_one_child;
use crate::ast::AstGraph;
use crate::ast::AstNode;
use crate::syntax::_node_builders::core::SyntaxNodeBuildError;
use crate::syntax::core_nodes::{BoolLiteral, NumberLiteral, StringLiteral};
use crate::utilities::text_nodes::node_to_str_result;
use alloc::borrow::ToOwned;
use alloc::format;

pub fn str_to_bool(raw: &str) -> Result<bool, SyntaxNodeBuildError> {
    let value = raw.to_lowercase();
    match value.as_str() {
        "false" => Ok(false),
        "true" => Ok(true),
        _ => Err(SyntaxNodeBuildError::Error(format!(
            "Cannot decode `{value}` into a boolean"
        ))),
    }
}
pub fn build_boolean_literal(node: &AstNode) -> Result<BoolLiteral, SyntaxNodeBuildError> {
    node.text.as_deref().map_or_else(
        || {
            Err(SyntaxNodeBuildError::Error(
                "A bool node must have a value".to_owned(),
            ))
        },
        |raw| str_to_bool(raw).map(|b| BoolLiteral { value: b }),
    )
}

pub fn build_boolean_literal_from_child(
    graph: &AstGraph,
    node: &AstNode,
) -> Result<BoolLiteral, SyntaxNodeBuildError> {
    // commonly a boolean_literal node does not have the value, its child holds it
    ensure_only_one_child(graph, node)
        .map_err(SyntaxNodeBuildError::ResolutionError)
        .map(|n| n.0)
        .and_then(build_boolean_literal)
}

pub fn build_number_literal(node: &AstNode) -> Result<NumberLiteral, SyntaxNodeBuildError> {
    node.text.as_ref().map_or_else(
        || {
            Err(SyntaxNodeBuildError::Error(
                "A number node must have a value".to_owned(),
            ))
        },
        |raw| Ok(NumberLiteral { value: raw.clone() }),
    )
}

pub fn build_string_literal(
    graph: &AstGraph,
    node: &AstNode,
) -> Result<StringLiteral, SyntaxNodeBuildError> {
    node_to_str_result(graph, node)
        .map_err(SyntaxNodeBuildError::ResolutionError)
        .map(|value| StringLiteral { value })
}