use crate::query::{adj_ast, match_ast};
use crate::syntax::builders::while_statement::build_while_statement_node;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::{CodeGraph, NodeId};
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let match_childs = match_ast(graph, n_id, &["block", "binary_expression"], None);
let mut block_id = match_childs
.get("block")
.copied()
.flatten()
.or_else(|| adj_ast(graph, n_id, None, &[]).into_iter().next_back())
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
if graph.label_type(block_id) == Some("expression_statement") {
block_id = adj_ast(graph, block_id, None, &[])
.into_iter()
.next()
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
}
let condition_id = match_childs.get("binary_expression").copied().flatten();
build_while_statement_node(args, n_id, block_id, condition_id).map(Some)
}