| 1 | //! The errors. |
| 2 | |
| 3 | use std::{error, fmt}; |
| 4 | |
| 5 | /// An error. |
| 6 | #[derive(Debug)] |
| 7 | pub struct Error { |
| 8 | line: usize, |
| 9 | column: usize, |
| 10 | message: String, |
| 11 | } |
| 12 | |
| 13 | impl Error { |
| 14 | /// Create an error. |
| 15 | #[inline] |
| 16 | pub fn new<T: Into<String>>((line, column): (usize, usize), message: T) -> Self { |
| 17 | Error { |
| 18 | line, |
| 19 | column, |
| 20 | message: message.into(), |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl error::Error for Error { |
| 26 | #[inline] |
| 27 | fn description(&self) -> &str { |
| 28 | &self.message |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl fmt::Display for Error { |
| 33 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 34 | if self.line > 0 && self.column > 0 { |
| 35 | write!( |
| 36 | formatter, |
| 37 | "{} (line {}, column {})", |
| 38 | self.message, self.line, self.column, |
| 39 | ) |
| 40 | } else if self.line > 0 { |
| 41 | write!(formatter, "{} (line {})", self.message, self.line) |
| 42 | } else { |
| 43 | self.message.fmt(formatter) |
| 44 | } |
| 45 | } |
| 46 | } |