use crate::{
query::{adj_ast, match_ast, match_ast_group_d},
syntax::{
builders::{
reserved_word::build_reserved_word_node, try_statement::build_try_statement_node,
},
SyntaxGraphArgs, SyntaxGraphError,
},
NodeId,
};
use alloc::borrow::ToOwned;
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let childs = match_ast(graph, n_id, &["call", "assignment"], Some(1));
let block_node = if let Some(id) = childs.get("assignment").copied().flatten() {
id
} else {
let direct_children = adj_ast(graph, n_id, Some(1), &[]);
if direct_children.len() < 2 {
return Ok(Some(build_reserved_word_node(
args,
n_id,
"begin".to_owned(),
)));
}
*direct_children
.get(1)
.ok_or(SyntaxGraphError::UnexpectedAstShape)?
};
let try_block_id = childs.get("call").copied().flatten();
let catch_block = match_ast_group_d(graph, n_id, "rescue", Some(1));
build_try_statement_node(args, n_id, block_node, &catch_block, try_block_id, None).map(Some)
}