Deprecate Error::description.

This commit is contained in:
Stephen Chung 2020-03-14 23:39:45 +08:00
parent b87dc1b281
commit b9e4040635
2 changed files with 16 additions and 35 deletions

View File

@ -20,18 +20,7 @@ pub enum LexError {
InputError(String), InputError(String),
} }
impl Error for LexError { 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 fmt::Display for LexError { impl fmt::Display for LexError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 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::MalformedNumber(s) => write!(f, "Invalid number: '{}'", s),
Self::MalformedChar(s) => write!(f, "Invalid character: '{}'", s), Self::MalformedChar(s) => write!(f, "Invalid character: '{}'", s),
Self::InputError(s) => write!(f, "{}", 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 { pub fn position(&self) -> Position {
self.1 self.1
} }
}
impl Error for ParseError { pub(crate) fn desc(&self) -> &str {
fn description(&self) -> &str {
match self.0 { match self.0 {
ParseErrorType::BadInput(ref p) => p, ParseErrorType::BadInput(ref p) => p,
ParseErrorType::InputPastEndOfFile => "Script is incomplete", ParseErrorType::InputPastEndOfFile => "Script is incomplete",
@ -128,39 +115,35 @@ impl Error for ParseError {
ParseErrorType::AssignmentToConstant(_) => "Cannot assign to a constant variable." ParseErrorType::AssignmentToConstant(_) => "Cannot assign to a constant variable."
} }
} }
fn cause(&self) -> Option<&dyn Error> {
None
}
} }
impl Error for ParseError {}
impl fmt::Display for ParseError { impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 { match self.0 {
ParseErrorType::BadInput(ref s) ParseErrorType::BadInput(ref s)
| ParseErrorType::MalformedIndexExpr(ref s) | ParseErrorType::MalformedIndexExpr(ref s)
| ParseErrorType::MalformedCallExpr(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) => { ParseErrorType::ForbiddenConstantExpr(ref s) => {
write!(f, "Expecting a constant to assign to '{}'", 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) => { ParseErrorType::FnMissingParams(ref s) => {
write!(f, "Expecting parameters for function '{}'", s)? write!(f, "Expecting parameters for function '{}'", s)?
} }
ParseErrorType::MissingRightParen(ref s) ParseErrorType::MissingRightParen(ref s)
| ParseErrorType::MissingRightBrace(ref s) | ParseErrorType::MissingRightBrace(ref s)
| ParseErrorType::MissingRightBracket(ref s) => { | ParseErrorType::MissingRightBracket(ref s) => write!(f, "{} for {}", self.desc(), s)?,
write!(f, "{} for {}", self.description(), s)?
}
ParseErrorType::AssignmentToConstant(ref s) if s.is_empty() => { ParseErrorType::AssignmentToConstant(ref s) if s.is_empty() => {
write!(f, "{}", self.description())? write!(f, "{}", self.desc())?
} }
ParseErrorType::AssignmentToConstant(ref s) => { ParseErrorType::AssignmentToConstant(ref s) => {
write!(f, "Cannot assign to constant '{}'", s)? write!(f, "Cannot assign to constant '{}'", s)?
} }
_ => write!(f, "{}", self.description())?, _ => write!(f, "{}", self.desc())?,
} }
if !self.1.is_eof() { if !self.1.is_eof() {

View File

@ -61,10 +61,10 @@ pub enum EvalAltResult {
Return(Dynamic, Position), Return(Dynamic, Position),
} }
impl Error for EvalAltResult { impl EvalAltResult {
fn description(&self) -> &str { pub(crate) fn desc(&self) -> &str {
match self { match self {
Self::ErrorParsing(p) => p.description(), Self::ErrorParsing(p) => p.desc(),
Self::ErrorFunctionNotFound(_, _) => "Function not found", Self::ErrorFunctionNotFound(_, _) => "Function not found",
Self::ErrorFunctionArgsMismatch(_, _, _, _) => { Self::ErrorFunctionArgsMismatch(_, _, _, _) => {
"Function call with wrong number of arguments" "Function call with wrong number of arguments"
@ -101,15 +101,13 @@ impl Error for EvalAltResult {
Self::Return(_, _) => "[Not Error] Function returns value", Self::Return(_, _) => "[Not Error] Function returns value",
} }
} }
fn cause(&self) -> Option<&dyn Error> {
None
}
} }
impl Error for EvalAltResult {}
impl fmt::Display for EvalAltResult { impl fmt::Display for EvalAltResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let desc = self.description(); let desc = self.desc();
match self { match self {
Self::ErrorFunctionNotFound(s, pos) => write!(f, "{}: '{}' ({})", desc, s, pos), Self::ErrorFunctionNotFound(s, pos) => write!(f, "{}: '{}' ({})", desc, s, pos),