diff --git a/src/nodes/from_str.rs b/src/nodes/from_str.rs index a589612..4608e48 100644 --- a/src/nodes/from_str.rs +++ b/src/nodes/from_str.rs @@ -22,6 +22,16 @@ enum ParsingState { ReadingNumber(usize), } +impl Node { + fn from_name(s: &str) -> Result { + Ok(match s { + "c" => Self::Number(Float::with_val(PREC, 299_792_458)), + "A" => Self::Number(Float::with_val(PREC, 6.02214076e23)), + f => Self::Function(f.parse()?), + }) + } +} + impl FromStr for Nodes { type Err = Error; fn from_str(s: &str) -> Result { @@ -55,11 +65,9 @@ impl FromStr for Nodes { ParsingState::Start => {} ParsingState::ReadingFunctionName(start_index) => { if !c.is_alphabetic() { - nodes.push(Node::Function( - filtered_string[start_index..i].parse().map_err(|()| { - Error::InvalidFunction(filtered_string[start_index..i].to_owned()) - })?, - )); + nodes.push(Node::from_name(&filtered_string[start_index..i]).map_err( + |()| Error::InvalidFunction(filtered_string[start_index..i].to_owned()), + )?); state = ParsingState::Start; } @@ -107,11 +115,11 @@ impl FromStr for Nodes { match state { ParsingState::Start => {} ParsingState::ReadingFunctionName(start_index) => { - nodes.push(Node::Function( - filtered_string[start_index..i].parse().map_err(|()| { + nodes.push( + Node::from_name(&filtered_string[start_index..i]).map_err(|()| { Error::InvalidFunction(filtered_string[start_index..i].to_owned()) })?, - )); + ); } ParsingState::ReadingNumber(start_index) => { nodes.push(Node::Number(