use crate::query::match_ast;
use crate::syntax::builders::if_statement::build_if_node;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::NodeId;
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let matched = match_ast(args.ast_graph, n_id, &["if", "else"], None);
let has_if = matched.get("if").copied().flatten().is_some();
let has_else = matched.get("else").copied().flatten().is_some();
if has_if && has_else && matched.len() == 5 {
let condition = matched
.get("__1__")
.copied()
.flatten()
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
let true_id = matched.get("__0__").copied().flatten();
let false_id = matched.get("__2__").copied().flatten();
return build_if_node(args, n_id, condition, true_id, false_id, None).map(Some);
}
let condition = matched
.get("__0__")
.copied()
.flatten()
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
build_if_node(args, n_id, condition, None, None, None).map(Some)
}