From 69d8d111baeb93abd9e09bb3da54a1b77e6a878e Mon Sep 17 00:00:00 2001 From: electria Date: Tue, 6 Oct 2026 15:49:16 -0700 Subject: [PATCH] refactor: improve eval error handling (end) now most of the panics come from Vec::remove, which can be fixed with Vec::try_remove when it's stabolized --- src/nodes/eval.rs | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/nodes/eval.rs b/src/nodes/eval.rs index d813e7c..cd37cb3 100644 --- a/src/nodes/eval.rs +++ b/src/nodes/eval.rs @@ -86,9 +86,11 @@ fn collapse_toplevel(nodes: &mut Vec, mut end: usize) -> Result<(), Error> n => return Err(Error::InvalidOperand(n)), }; end -= 1; - match &mut nodes[start..end][operator] { - Node::Number(n) => n.pow_from(lhs), - n => return Err(Error::InvalidOperand(n.clone())), + match nodes.get_mut(start + operator) { + None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)), + + Some(Node::Number(n)) => n.pow_from(lhs), + Some(n) => return Err(Error::InvalidOperand(n.clone())), }; nodes.remove(start + operator - 1); @@ -104,9 +106,11 @@ fn collapse_toplevel(nodes: &mut Vec, mut end: usize) -> Result<(), Error> n => return Err(Error::InvalidOperand(n)), }; end -= 1; - match &mut nodes[start..end][operator] { - Node::Number(n) => n.div_from(lhs), - n => return Err(Error::InvalidOperand(n.clone())), + match nodes.get_mut(start + operator) { + None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)), + + Some(Node::Number(n)) => n.div_from(lhs), + Some(n) => return Err(Error::InvalidOperand(n.clone())), }; nodes.remove(start + operator - 1); @@ -122,9 +126,11 @@ fn collapse_toplevel(nodes: &mut Vec, mut end: usize) -> Result<(), Error> n => return Err(Error::InvalidOperand(n)), }; end -= 1; - match &mut nodes[start..end][operator] { - Node::Number(n) => n.mul_from(lhs), - n => return Err(Error::InvalidOperand(n.clone())), + match nodes.get_mut(start + operator) { + None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)), + + Some(Node::Number(n)) => n.mul_from(lhs), + Some(n) => return Err(Error::InvalidOperand(n.clone())), }; nodes.remove(start + operator - 1); @@ -140,9 +146,11 @@ fn collapse_toplevel(nodes: &mut Vec, mut end: usize) -> Result<(), Error> n => return Err(Error::InvalidOperand(n)), }; end -= 1; - match &mut nodes[start..end][operator] { - Node::Number(n) => n.sub_from(lhs), - n => return Err(Error::InvalidOperand(n.clone())), + match nodes.get_mut(start + operator) { + None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)), + + Some(Node::Number(n)) => n.sub_from(lhs), + Some(n) => return Err(Error::InvalidOperand(n.clone())), }; nodes.remove(start + operator - 1); @@ -158,9 +166,11 @@ fn collapse_toplevel(nodes: &mut Vec, mut end: usize) -> Result<(), Error> n => return Err(Error::InvalidOperand(n)), }; end -= 1; - match &mut nodes[start..end][operator] { - Node::Number(n) => n.add_from(lhs), - n => return Err(Error::InvalidOperand(n.clone())), + match nodes.get_mut(start + operator) { + None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)), + + Some(Node::Number(n)) => n.add_from(lhs), + Some(n) => return Err(Error::InvalidOperand(n.clone())), }; nodes.remove(start + operator - 1);