fluidattacks-blends-domain 0.19.0

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
use alloc::borrow::ToOwned;
use alloc::string::String;

use crate::query::{label_text, match_ast_d};
use crate::syntax::builders::import_statement::{build_import_node, ImportElement};
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::utilities::text_nodes::node_to_str;
use crate::NodeId;

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

    let expression = match_ast_d(graph, n_id, "interpreted_string_literal", None).map_or_else(
        || node_to_str(graph, n_id),
        |literal_id| {
            let text = label_text(graph, literal_id).unwrap_or_default();
            text.get(1..text.len().saturating_sub(1))
                .unwrap_or_default()
                .to_owned()
        },
    );

    let alias: Option<String> = match_ast_d(graph, n_id, "package_identifier", None)
        .and_then(|alias_id| label_text(graph, alias_id))
        .map(ToOwned::to_owned);

    build_import_node(
        args,
        n_id,
        &ImportElement {
            expression: Some(expression),
            alias,
            ..ImportElement::default()
        },
    )
    .map(Some)
}