refactor: improve eval error handling (start)

This commit is contained in:
electria 2026-10-06 15:21:30 -07:00
commit d6b775abfb
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -7,6 +7,8 @@ use crate::nodes::{Function, Node, Nodes, Operator};
#[derive(Clone, Debug, thiserror::Error)]
pub enum Error {
#[error("out of bounds: index {0}, {1}")]
OutOfBounds(Nodes, usize),
#[error("invalid operand: {0}")]
InvalidOperand(Node),
#[error("missing argument, got: {0}")]
@ -48,8 +50,15 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
})
.next()
{
match &mut nodes[start..end][index + 1] {
Node::Number(n) => match variant {
let Some(node) = nodes.get_mut(start + index + 1) else {
return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + index + 1));
};
let Node::Number(n) = node else {
return Err(Error::MissingArgument(node.clone()));
};
match variant {
Function::Sqrt => n.sqrt_mut(),
Function::Ln => n.ln_mut(),
@ -62,8 +71,6 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
Function::ArcTangent => n.atan_mut(),
Function::Secant => n.sec_mut(),
Function::CoSecant => n.csc_mut(),
},
n => return Err(Error::MissingArgument(n.clone())),
};
nodes.remove(start + index);