refactor: use Node::from from parsed Floats

this is a little nicer, hopefully it has no perf penalty
This commit is contained in:
electria 2026-10-04 16:34:21 -07:00
commit 564f95aa32
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -1,9 +1,9 @@
use core::f64; use core::f64;
use std::str::FromStr; use std::str::FromStr;
use rug::{Float, float::ParseFloatError, ops::CompleteRound}; use rug::{Float, float::ParseFloatError};
use crate::nodes::{Function, Node, Nodes, Operator, PREC}; use crate::nodes::{Function, Node, Nodes, Operator};
impl Node { impl Node {
fn from_name(s: &str) -> Result<Self, String> { fn from_name(s: &str) -> Result<Self, String> {
@ -124,15 +124,10 @@ impl FromStr for Nodes {
&& !(c == 'e' && filtered_string.chars().nth(i + 1).is_some_and(|c| c == '-')) && !(c == 'e' && filtered_string.chars().nth(i + 1).is_some_and(|c| c == '-'))
&& !(c == '-' && filtered_string.chars().nth(i - 1).is_some_and(|c| c == 'e')) && !(c == '-' && filtered_string.chars().nth(i - 1).is_some_and(|c| c == 'e'))
{ {
nodes.push(Node::Number( nodes.push(Node::from(
Float::parse(&filtered_string[start_index..i]) Float::parse(&filtered_string[start_index..i]).map_err(|e| {
.map_err(|e| { Error::NumberParsing(filtered_string[start_index..i].to_owned(), e)
Error::NumberParsing( })?,
filtered_string[start_index..i].to_owned(),
e,
)
})?
.complete(PREC),
)); ));
state = ParsingState::Start; state = ParsingState::Start;
@ -182,12 +177,10 @@ impl FromStr for Nodes {
if matches!(nodes.last(), Some(Node::ParenthesisEnd)) { if matches!(nodes.last(), Some(Node::ParenthesisEnd)) {
nodes.push(Node::Operator(Default::default())); nodes.push(Node::Operator(Default::default()));
} }
nodes.push(Node::Number( nodes.push(Node::from(
Float::parse(&filtered_string[start_index..i]) Float::parse(&filtered_string[start_index..i]).map_err(|e| {
.map_err(|e| {
Error::NumberParsing(filtered_string[start_index..i].to_owned(), e) Error::NumberParsing(filtered_string[start_index..i].to_owned(), e)
})? })?,
.complete(PREC),
)); ));
} }
} }