refactor: sort all from_str-like impls together
This commit is contained in:
parent
c4a1d670ca
commit
b9fc3c1eec
2 changed files with 41 additions and 52 deletions
|
|
@ -2,7 +2,7 @@ use std::str::FromStr;
|
||||||
|
|
||||||
use rug::{Float, float::ParseFloatError, ops::CompleteRound};
|
use rug::{Float, float::ParseFloatError, ops::CompleteRound};
|
||||||
|
|
||||||
use crate::nodes::{Node, Nodes, PREC};
|
use crate::nodes::{Function, Node, Nodes, Operator, PREC};
|
||||||
|
|
||||||
#[derive(Clone, Debug, thiserror::Error)]
|
#[derive(Clone, Debug, thiserror::Error)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
|
@ -14,6 +14,45 @@ pub enum Error {
|
||||||
NumberParsing(String, ParseFloatError),
|
NumberParsing(String, ParseFloatError),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Node {
|
||||||
|
fn from_name(s: &str) -> Result<Self, Error> {
|
||||||
|
Ok(match s {
|
||||||
|
"c" => Self::Number(Float::with_val(PREC, 299_792_458)),
|
||||||
|
"A" => Self::Number(Float::with_val(PREC, 6.02214076e23)),
|
||||||
|
_ => Self::Function(s.parse()?),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl TryFrom<char> for Operator {
|
||||||
|
type Error = ();
|
||||||
|
fn try_from(value: char) -> Result<Self, Self::Error> {
|
||||||
|
match value {
|
||||||
|
'+' => Ok(Self::Plus),
|
||||||
|
'-' => Ok(Self::Minus),
|
||||||
|
'*' => Ok(Self::Mult),
|
||||||
|
'/' => Ok(Self::Div),
|
||||||
|
'^' => Ok(Self::Exp),
|
||||||
|
_ => Err(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl FromStr for Function {
|
||||||
|
type Err = Error;
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
match s {
|
||||||
|
"sin" => Ok(Self::Sine),
|
||||||
|
"asin" => Ok(Self::ArcSine),
|
||||||
|
"cos" => Ok(Self::Cosine),
|
||||||
|
"acos" => Ok(Self::ArcCosine),
|
||||||
|
"tan" => Ok(Self::Tangent),
|
||||||
|
"atan" => Ok(Self::ArcTangent),
|
||||||
|
"sec" => Ok(Self::Secant),
|
||||||
|
"csc" => Ok(Self::CoSecant),
|
||||||
|
_ => Err(Error::UnrecognizedName(s.to_owned())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, PartialEq)]
|
#[derive(Clone, Debug, Default, PartialEq)]
|
||||||
enum ParsingState {
|
enum ParsingState {
|
||||||
#[default]
|
#[default]
|
||||||
|
|
@ -21,17 +60,6 @@ enum ParsingState {
|
||||||
ReadingFunctionName(usize),
|
ReadingFunctionName(usize),
|
||||||
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> {
|
||||||
|
|
@ -66,11 +94,7 @@ 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::from_name(&filtered_string[start_index..i]).map_err(
|
nodes.push(Node::from_name(&filtered_string[start_index..i])?);
|
||||||
|()| {
|
|
||||||
Error::UnrecognizedName(filtered_string[start_index..i].to_owned())
|
|
||||||
},
|
|
||||||
)?);
|
|
||||||
|
|
||||||
state = ParsingState::Start;
|
state = ParsingState::Start;
|
||||||
}
|
}
|
||||||
|
|
@ -120,11 +144,7 @@ impl FromStr for Nodes {
|
||||||
match state {
|
match state {
|
||||||
ParsingState::Start => {}
|
ParsingState::Start => {}
|
||||||
ParsingState::ReadingFunctionName(start_index) => {
|
ParsingState::ReadingFunctionName(start_index) => {
|
||||||
nodes.push(
|
nodes.push(Node::from_name(&filtered_string[start_index..i])?);
|
||||||
Node::from_name(&filtered_string[start_index..i]).map_err(|()| {
|
|
||||||
Error::UnrecognizedName(filtered_string[start_index..i].to_owned())
|
|
||||||
})?,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
ParsingState::ReadingNumber(start_index) => {
|
ParsingState::ReadingNumber(start_index) => {
|
||||||
nodes.push(Node::Number(
|
nodes.push(Node::Number(
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
//! as well as the logic to parse it from a string (`Node::from_str`),
|
//! as well as the logic to parse it from a string (`Node::from_str`),
|
||||||
//! and the logic to evaluate it into a single number (`Node::evaluate`).
|
//! and the logic to evaluate it into a single number (`Node::evaluate`).
|
||||||
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use rug::Float;
|
use rug::Float;
|
||||||
|
|
||||||
mod evaluate;
|
mod evaluate;
|
||||||
|
|
@ -38,19 +36,6 @@ enum Operator {
|
||||||
Div,
|
Div,
|
||||||
Exp,
|
Exp,
|
||||||
}
|
}
|
||||||
impl TryFrom<char> for Operator {
|
|
||||||
type Error = ();
|
|
||||||
fn try_from(value: char) -> Result<Self, Self::Error> {
|
|
||||||
match value {
|
|
||||||
'+' => Ok(Self::Plus),
|
|
||||||
'-' => Ok(Self::Minus),
|
|
||||||
'*' => Ok(Self::Mult),
|
|
||||||
'/' => Ok(Self::Div),
|
|
||||||
'^' => Ok(Self::Exp),
|
|
||||||
_ => Err(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
enum Function {
|
enum Function {
|
||||||
|
|
@ -63,19 +48,3 @@ enum Function {
|
||||||
Secant,
|
Secant,
|
||||||
CoSecant,
|
CoSecant,
|
||||||
}
|
}
|
||||||
impl FromStr for Function {
|
|
||||||
type Err = ();
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
match s {
|
|
||||||
"sin" => Ok(Self::Sine),
|
|
||||||
"asin" => Ok(Self::ArcSine),
|
|
||||||
"cos" => Ok(Self::Cosine),
|
|
||||||
"acos" => Ok(Self::ArcCosine),
|
|
||||||
"tan" => Ok(Self::Tangent),
|
|
||||||
"atan" => Ok(Self::ArcTangent),
|
|
||||||
"sec" => Ok(Self::Secant),
|
|
||||||
"csc" => Ok(Self::CoSecant),
|
|
||||||
_ => Err(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue