feat: add constants
This commit is contained in:
parent
e7a9358bd0
commit
d27a59df17
1 changed files with 16 additions and 8 deletions
|
|
@ -22,6 +22,16 @@ enum ParsingState {
|
|||
ReadingNumber(usize),
|
||||
}
|
||||
|
||||
impl Node {
|
||||
fn from_name(s: &str) -> Result<Self, ()> {
|
||||
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<Self, Self::Err> {
|
||||
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue