use core::fmt; use std::error::Error; #[derive(Debug)] pub struct LexingError { pub line: usize, pub kind: LexingErrorKind, } impl fmt::Display for LexingError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Error: {}\n on line: {}", self.kind, self.line) } } impl Error for LexingError { fn source(&self) -> Option<&(dyn Error + 'static)> { Some(&self.kind) } } #[derive(Debug)] pub enum LexingErrorKind { UnmatchedQuote, UnexpectedCharacter(char), } impl fmt::Display for LexingErrorKind { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { LexingErrorKind::UnmatchedQuote => write!(f, "Unmatched \" character"), LexingErrorKind::UnexpectedCharacter(c) => write!(f, "Unexpected character {c}"), } } } impl Error for LexingErrorKind {}