use crate::{
query::{adj_ast, match_ast_d},
syntax::{
builders::switch_section::build_switch_section_node, SyntaxGraphArgs, SyntaxGraphError,
},
utilities::text_nodes::node_to_str,
NodeId,
};
use alloc::vec::Vec;
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let statements_id = match_ast_d(graph, n_id, "statements", Some(1))
.or_else(|| adj_ast(graph, n_id, Some(1), &[]).last().copied())
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
let child_ids: Vec<NodeId> = adj_ast(graph, statements_id, Some(1), &[]);
let val_id = match_ast_d(graph, n_id, "switch_pattern", Some(1))
.or_else(|| adj_ast(graph, n_id, Some(1), &[]).first().copied())
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
let case_expr = node_to_str(graph, val_id);
build_switch_section_node(args, n_id, case_expr, &child_ids).map(Some)
}