refactor: simplify from_str logic slightly

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

View file

@ -14,7 +14,7 @@ pub enum Error {
NumberParsing(String, ParseFloatError),
}
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug, Default, PartialEq)]
enum ParsingState {
#[default]
Start,
@ -37,9 +37,8 @@ impl FromStr for Nodes {
let filtered_string: String = s.chars().filter(is_ignored_char).collect();
for (i, c) in filtered_string.char_indices() {
let mut start = false;
match state {
ParsingState::Start => start = true,
ParsingState::Start => {}
ParsingState::ReadingFunctionName(start_index) => {
if !c.is_alphabetic() {
nodes.push(Node::Function(
@ -49,13 +48,12 @@ impl FromStr for Nodes {
));
state = ParsingState::Start;
start = true;
}
}
ParsingState::ReadingNumber(start_index) => {
if !is_numeric_char(c) {
nodes.push(Node::Number(
Float::parse_radix(&filtered_string[start_index..i], 10)
Float::parse(&filtered_string[start_index..i])
.map_err(|e| {
Error::NumberParsing(
filtered_string[start_index..i].to_owned(),
@ -66,12 +64,11 @@ impl FromStr for Nodes {
));
state = ParsingState::Start;
start = true;
}
}
}
if start {
if state == ParsingState::Start {
if is_numeric_char(c) {
state = ParsingState::ReadingNumber(i);
} else if c.is_alphabetic() {