use crate::{
query::adj_ast,
syntax::{builders::assignment::build_assignment_node, SyntaxGraphArgs, SyntaxGraphError},
utilities::text_nodes::node_to_str,
NodeId,
};
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let children = adj_ast(graph, n_id, Some(1), &[]);
let (operator, value_id, variable_id) = if children.len() >= 3 {
let op = children
.get(1)
.copied()
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
let op_text = node_to_str(graph, op);
let value = children
.get(2)
.copied()
.ok_or(SyntaxGraphError::MissingAstNode)?;
let variable = children
.first()
.copied()
.ok_or(SyntaxGraphError::MissingAstNode)?;
(Some(op_text), value, variable)
} else {
let variable = children
.first()
.copied()
.ok_or(SyntaxGraphError::MissingAstNode)?;
let value = children
.last()
.copied()
.ok_or(SyntaxGraphError::MissingAstNode)?;
(None, value, variable)
};
build_assignment_node(args, n_id, variable_id, Some(value_id), operator).map(Some)
}