| 1 | /// Enum of different kinds of errors. |
| 2 | #[derive(Debug, Clone)] |
| 3 | pub enum ErrorKind { |
| 4 | Parse, |
| 5 | MissingValues, |
| 6 | UnsupportedFormatSpec, |
| 7 | } |
| 8 | |
| 9 | /// Error struct which implements [Error](std::error::Error) trait. |
| 10 | #[derive(Debug)] |
| 11 | pub struct Error { |
| 12 | message: String, |
| 13 | kind: ErrorKind, |
| 14 | } |
| 15 | |
| 16 | impl std::fmt::Display for Error { |
| 17 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 18 | write!(f, "{}", self.message) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | impl std::error::Error for Error {} |
| 23 | |
| 24 | impl Error { |
| 25 | /// Create new parse error. |
| 26 | pub(crate) fn new_parse(message: String) -> Self { |
| 27 | Self { |
| 28 | message, |
| 29 | kind: ErrorKind::Parse, |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /// Create new missing values error. |
| 34 | pub(crate) fn new_values(message: String) -> Self { |
| 35 | Self { |
| 36 | message, |
| 37 | kind: ErrorKind::MissingValues, |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /// Create new unsupported format spec error. |
| 42 | pub(crate) fn new_ufs(message: String) -> Self { |
| 43 | Self { |
| 44 | message, |
| 45 | kind: ErrorKind::UnsupportedFormatSpec, |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// Returns error message. |
| 50 | pub fn message(&self) -> String { |
| 51 | self.message.clone() |
| 52 | } |
| 53 | |
| 54 | /// Returns `ErrorKind` |
| 55 | pub fn kind(&self) -> ErrorKind { |
| 56 | self.kind.clone() |
| 57 | } |
| 58 | } |