refactor: use Node::from from parsed Floats
this is a little nicer, hopefully it has no perf penalty
This commit is contained in:
parent
0afc7aab25
commit
564f95aa32
1 changed files with 10 additions and 17 deletions
|
|
@ -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),
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue