use crate::{
query::{adj_ast, match_ast},
syntax::{builders::annotation::build_annotation_node, 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 matches = match_ast(
graph,
n_id,
&["constructor_invocation", "user_type"],
Some(1),
);
let mut annotation_name = String::from("@");
let mut args_id = None;
if let Some(constructor_id) = matches.get("constructor_invocation").copied().flatten() {
let children = adj_ast(graph, constructor_id, Some(1), &[]);
if let Some(&name_id) = children.first() {
annotation_name.push_str(&node_to_str(graph, name_id));
args_id = children.get(1).copied();
}
} else if let Some(user_type_id) = matches.get("user_type").copied().flatten() {
annotation_name.push_str(&node_to_str(graph, user_type_id));
}
build_annotation_node(args, n_id, annotation_name, args_id).map(Some)
}