use alloc::borrow::ToOwned;
use crate::query::{adj_ast, label_text};
use crate::syntax::builders::unary_expression::build_unary_expression_node;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::{CodeGraph, NodeId};
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let children = adj_ast(args.ast_graph, n_id, None, &[]);
let first_id = children
.first()
.copied()
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
let second_id = children
.get(1)
.copied()
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
let (operator_id, value_id) =
if matches!(args.ast_graph.label_type(first_id), Some("++" | "--")) {
(first_id, second_id)
} else {
(second_id, first_id)
};
let expression_type = label_text(args.ast_graph, operator_id)
.map_or_else(|| "UpdateExpression".to_owned(), ToOwned::to_owned);
build_unary_expression_node(args, n_id, expression_type, value_id).map(Some)
}