use crate::{
query::{adj_ast, get_node_by_path, label_text, match_ast_d},
syntax::{
builders::import_statement::{build_import_node, ImportElement},
SyntaxGraphArgs, SyntaxGraphError,
},
utilities::text_nodes::node_to_str,
NodeId,
};
use alloc::string::String;
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let clause = match_ast_d(graph, n_id, "namespace_use_clause", None);
let name_n_id =
clause.and_then(|exp_n_id| adj_ast(graph, exp_n_id, Some(1), &[]).first().copied());
let (expression, alias) = if let (Some(exp_n_id), Some(name_id)) = (clause, name_n_id) {
let alias = get_node_by_path(graph, exp_n_id, &["namespace_aliasing_clause", "name"])
.and_then(|alias_n_id| label_text(graph, alias_n_id).map(String::from));
(node_to_str(graph, name_id), alias)
} else {
(String::new(), None)
};
build_import_node(
args,
n_id,
&ImportElement {
expression: Some(expression),
alias,
..ImportElement::default()
},
)
.map(Some)
}