use crate::{
query::adj_ast,
syntax::{
builders::switch_section::build_switch_section_node, SyntaxGraphArgs, SyntaxGraphError,
},
utilities::text_nodes::node_to_str,
CodeGraph, NodeId,
};
use alloc::string::String;
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let child_ids = adj_ast(graph, n_id, Some(1), &[]);
let arrow_index = child_ids
.iter()
.position(|&c_id| graph.label_type(c_id) == Some("->"));
let cut = arrow_index.unwrap_or(1).min(child_ids.len());
let condition_ids = child_ids.get(..cut).unwrap_or_default();
let joined: String = condition_ids
.iter()
.map(|&c_id| node_to_str(graph, c_id))
.collect();
let case_value = if joined.is_empty() {
String::from("Default")
} else {
joined
};
let body_id = child_ids
.last()
.copied()
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
build_switch_section_node(args, n_id, case_value, &[body_id]).map(Some)
}