diff --git a/src/error.rs b/src/error.rs index e769d993..89d6f370 100644 --- a/src/error.rs +++ b/src/error.rs @@ -20,18 +20,7 @@ pub enum LexError { InputError(String), } -impl Error for LexError { - fn description(&self) -> &str { - match *self { - Self::UnexpectedChar(_) => "Unexpected character", - Self::UnterminatedString => "Open string is not terminated", - Self::MalformedEscapeSequence(_) => "Unexpected values in escape sequence", - Self::MalformedNumber(_) => "Unexpected characters in number", - Self::MalformedChar(_) => "Char constant not a single character", - Self::InputError(_) => "Input error", - } - } -} +impl Error for LexError {} impl fmt::Display for LexError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -41,7 +30,7 @@ impl fmt::Display for LexError { Self::MalformedNumber(s) => write!(f, "Invalid number: '{}'", s), Self::MalformedChar(s) => write!(f, "Invalid character: '{}'", s), Self::InputError(s) => write!(f, "{}", s), - _ => write!(f, "{}", self.description()), + Self::UnterminatedString => write!(f, "Open string is not terminated"), } } } @@ -104,10 +93,8 @@ impl ParseError { pub fn position(&self) -> Position { self.1 } -} -impl Error for ParseError { - fn description(&self) -> &str { + pub(crate) fn desc(&self) -> &str { match self.0 { ParseErrorType::BadInput(ref p) => p, ParseErrorType::InputPastEndOfFile => "Script is incomplete", @@ -128,39 +115,35 @@ impl Error for ParseError { ParseErrorType::AssignmentToConstant(_) => "Cannot assign to a constant variable." } } - - fn cause(&self) -> Option<&dyn Error> { - None - } } +impl Error for ParseError {} + impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self.0 { ParseErrorType::BadInput(ref s) | ParseErrorType::MalformedIndexExpr(ref s) | ParseErrorType::MalformedCallExpr(ref s) => { - write!(f, "{}", if s.is_empty() { self.description() } else { s })? + write!(f, "{}", if s.is_empty() { self.desc() } else { s })? } ParseErrorType::ForbiddenConstantExpr(ref s) => { write!(f, "Expecting a constant to assign to '{}'", s)? } - ParseErrorType::UnknownOperator(ref s) => write!(f, "{}: '{}'", self.description(), s)?, + ParseErrorType::UnknownOperator(ref s) => write!(f, "{}: '{}'", self.desc(), s)?, ParseErrorType::FnMissingParams(ref s) => { write!(f, "Expecting parameters for function '{}'", s)? } ParseErrorType::MissingRightParen(ref s) | ParseErrorType::MissingRightBrace(ref s) - | ParseErrorType::MissingRightBracket(ref s) => { - write!(f, "{} for {}", self.description(), s)? - } + | ParseErrorType::MissingRightBracket(ref s) => write!(f, "{} for {}", self.desc(), s)?, ParseErrorType::AssignmentToConstant(ref s) if s.is_empty() => { - write!(f, "{}", self.description())? + write!(f, "{}", self.desc())? } ParseErrorType::AssignmentToConstant(ref s) => { write!(f, "Cannot assign to constant '{}'", s)? } - _ => write!(f, "{}", self.description())?, + _ => write!(f, "{}", self.desc())?, } if !self.1.is_eof() { diff --git a/src/result.rs b/src/result.rs index acafecc4..faa8db7c 100644 --- a/src/result.rs +++ b/src/result.rs @@ -61,10 +61,10 @@ pub enum EvalAltResult { Return(Dynamic, Position), } -impl Error for EvalAltResult { - fn description(&self) -> &str { +impl EvalAltResult { + pub(crate) fn desc(&self) -> &str { match self { - Self::ErrorParsing(p) => p.description(), + Self::ErrorParsing(p) => p.desc(), Self::ErrorFunctionNotFound(_, _) => "Function not found", Self::ErrorFunctionArgsMismatch(_, _, _, _) => { "Function call with wrong number of arguments" @@ -101,15 +101,13 @@ impl Error for EvalAltResult { Self::Return(_, _) => "[Not Error] Function returns value", } } - - fn cause(&self) -> Option<&dyn Error> { - None - } } +impl Error for EvalAltResult {} + impl fmt::Display for EvalAltResult { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let desc = self.description(); + let desc = self.desc(); match self { Self::ErrorFunctionNotFound(s, pos) => write!(f, "{}: '{}' ({})", desc, s, pos),