From 31e05e8ad7860a7c71f9e81e880f8aa10ca7d234 Mon Sep 17 00:00:00 2001 From: "Luke I. Wilson" Date: Mon, 12 Aug 2019 00:52:58 -0500 Subject: [PATCH] Errors use owned data --- src/computer.rs | 18 +++++++++--------- src/lexer.rs | 6 +++--- src/lib.rs | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/computer.rs b/src/computer.rs index 4fcac33..ea55413 100644 --- a/src/computer.rs +++ b/src/computer.rs @@ -42,11 +42,11 @@ pub trait Num { /// | UnrecognizedIdentifier | When an identifier could not be resolved: it was not found in the Computer's variables. | /// | UnrecognizedFunctionIdentifier | When the identifier could not be found in the Computer's functions. | #[derive(Debug, Clone, PartialEq)] -pub enum ComputeError<'a> { +pub enum ComputeError { InvalidFactorial, - VariableIsConstant(&'a str), - UnrecognizedIdentifier(&'a str), - UnrecognizedFunctionIdentifier(&'a str), + VariableIsConstant(String), + UnrecognizedIdentifier(String), + UnrecognizedFunctionIdentifier(String), } use self::ComputeError::*; @@ -116,12 +116,12 @@ impl<'fun, T: Num + Clone + PartialOrd + Neg + Add + Sub } } - fn compute_expr<'a>(&mut self, expr: &Expr<'a, T>) -> Result> { // TODO: a lot of .to_owned() happens here to compare &'a str to Strings: there must be a more efficient way + fn compute_expr<'a>(&mut self, expr: &Expr<'a, T>) -> Result { // TODO: a lot of .to_owned() happens here to compare &'a str to Strings: there must be a more efficient way match expr { Expr::Constant(num) => Ok(num.clone()), Expr::Identifier(id) => match self.variables.get(id.to_owned()) { Some(value) => Ok(value.0.clone()), - None => Err(UnrecognizedIdentifier(id)), + None => Err(UnrecognizedIdentifier(id.to_string())), }, Expr::Neg(expr) => Ok(-self.compute_expr(expr)?), Expr::BinOp(op, lexpr, rexpr) => { @@ -141,13 +141,13 @@ impl<'fun, T: Num + Clone + PartialOrd + Neg + Add + Sub let value = self.compute_expr(&expr)?; match self.functions.get(id.to_owned()) { Some(func) => Ok(func(value)), - None => Err(UnrecognizedFunctionIdentifier(id)), + None => Err(UnrecognizedFunctionIdentifier(id.to_string())), } } Expr::Assignment(id, expr) => { let value = self.compute_expr(&expr)?; if self.variables.contains_key(id.to_owned()) && self.variables.get(id.to_owned()).unwrap().1 == true { - return Err(VariableIsConstant(id)); + return Err(VariableIsConstant(id.to_string())); } self.variables.insert((*id).to_owned(), (value.clone(), false)); Ok(value) @@ -180,7 +180,7 @@ impl<'fun, T: Num + Clone + PartialOrd + Neg + Add + Sub /// // Using this function to create the result from the `Expr`. /// let result = computer.compute(&ast).unwrap(); /// ``` - pub fn compute<'a>(&mut self, expr: &Expr<'a, T>) -> Result> { + pub fn compute<'a>(&mut self, expr: &Expr<'a, T>) -> Result { let val = self.compute_expr(expr); match &val { Ok(n) => { diff --git a/src/lexer.rs b/src/lexer.rs index 5afa70a..d2fc548 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -29,9 +29,9 @@ pub enum Token<'a, T> { /// | InvalidCharacter | If the input contains any characters not recognized by the lexer to be numbers or characters, ex: 'ƒ' | /// | InvalidNumber | A number entered invalidly: '2.34.2' or '..3' | #[derive(Debug, Clone, PartialEq)] -pub enum LexerError<'a> { +pub enum LexerError { InvalidCharacter(char), - InvalidNumber(&'a str), + InvalidNumber(String), } /// Turn a string into a vector of tokens. This function generally takes the most time, @@ -81,7 +81,7 @@ where match (&src[start_idx..=end_idx]).parse::() { Ok(num) => tokens.push(Token::Number(num)), - _ => return Err(LexerError::InvalidNumber(&src[start_idx..=end_idx])), + _ => return Err(LexerError::InvalidNumber(src[start_idx..=end_idx].to_owned())), } } else if c.is_alphabetic() { let start_idx = i; diff --git a/src/lib.rs b/src/lib.rs index 3fcaa41..9e3cfc5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -95,9 +95,9 @@ impl Num for f64 { /// [parser::ParserError](parser/enum.ParserError.html), and [lexer::LexerError](lexer/enum.LexerError.html). Produced when using `eval` helper functions. #[derive(Debug, Clone)] pub enum EvalError<'a, T: Clone + std::fmt::Debug> { - ComputeError(computer::ComputeError<'a>), + ComputeError(computer::ComputeError), ParserError(parser::ParserError<'a, T>), - LexerError(lexer::LexerError<'a>), + LexerError(lexer::LexerError), } /// Turn an expression inside a string into a number.