feat: add constants

This commit is contained in:
electria 2026-07-15 23:42:49 -07:00
commit d27a59df17
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -22,6 +22,16 @@ enum ParsingState {
ReadingNumber(usize), 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 { impl FromStr for Nodes {
type Err = Error; type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
@ -55,11 +65,9 @@ impl FromStr for Nodes {
ParsingState::Start => {} ParsingState::Start => {}
ParsingState::ReadingFunctionName(start_index) => { ParsingState::ReadingFunctionName(start_index) => {
if !c.is_alphabetic() { if !c.is_alphabetic() {
nodes.push(Node::Function( nodes.push(Node::from_name(&filtered_string[start_index..i]).map_err(
filtered_string[start_index..i].parse().map_err(|()| { |()| Error::InvalidFunction(filtered_string[start_index..i].to_owned()),
Error::InvalidFunction(filtered_string[start_index..i].to_owned()) )?);
})?,
));
state = ParsingState::Start; state = ParsingState::Start;
} }
@ -107,11 +115,11 @@ impl FromStr for Nodes {
match state { match state {
ParsingState::Start => {} ParsingState::Start => {}
ParsingState::ReadingFunctionName(start_index) => { ParsingState::ReadingFunctionName(start_index) => {
nodes.push(Node::Function( nodes.push(
filtered_string[start_index..i].parse().map_err(|()| { Node::from_name(&filtered_string[start_index..i]).map_err(|()| {
Error::InvalidFunction(filtered_string[start_index..i].to_owned()) Error::InvalidFunction(filtered_string[start_index..i].to_owned())
})?, })?,
)); );
} }
ParsingState::ReadingNumber(start_index) => { ParsingState::ReadingNumber(start_index) => {
nodes.push(Node::Number( nodes.push(Node::Number(