refactor: simplify from_str logic slightly
This commit is contained in:
parent
0b0d4a960f
commit
d54c9636ca
1 changed files with 4 additions and 7 deletions
|
|
@ -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() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue