use crate::{
query::{adj_ast, match_ast_d, match_ast_group_d},
syntax::{
builders::if_statement::build_if_node,
fields::{swift::if_statement, Field},
SyntaxGraphArgs, SyntaxGraphError,
},
CodeGraph, NodeId,
};
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
const CONDITION: Field<false> = Field::new("condition_id");
let graph = args.ast_graph;
let mut condition_id = args
.optional_field_alt(n_id, CONDITION)
.or_else(|| match_ast_d(graph, n_id, "simple_identifier", Some(1)))
.or_else(|| match_ast_d(graph, n_id, "call_expression", Some(1)))
.or_else(|| adj_ast(graph, n_id, Some(1), &[]).first().copied())
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
let initializer_id = args.optional_field_alt(n_id, if_statement::BOUND_IDENTIFIER);
if graph.label_type(condition_id) == Some("value_binding_pattern") {
if let Some(bound_id) = match_ast_d(graph, n_id, "try_expression", Some(1))
.or_else(|| match_ast_d(graph, n_id, "call_expression", Some(1)))
{
condition_id = bound_id;
}
}
let statements = match_ast_group_d(graph, n_id, "statements", Some(1));
let (true_id, mut false_id) = match statements.as_slice() {
[true_stmt, false_stmt] => (Some(*true_stmt), Some(*false_stmt)),
[true_stmt] => (Some(*true_stmt), None),
_ => (None, None),
};
if let Some(false_statement) = match_ast_d(graph, n_id, "if_statement", Some(1)) {
false_id = Some(false_statement);
}
build_if_node(args, n_id, condition_id, true_id, false_id, initializer_id).map(Some)
}