diff options
Diffstat (limited to 'src/error.rs')
| -rw-r--r-- | src/error.rs | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/src/error.rs b/src/error.rs index 6d7aba4..9252762 100644 --- a/src/error.rs +++ b/src/error.rs | |||
| @@ -2,25 +2,27 @@ use std::error::Error; | |||
| 2 | use std::fmt::Display; | 2 | use std::fmt::Display; |
| 3 | use std::io; | 3 | use std::io; |
| 4 | 4 | ||
| 5 | pub type ArchiveResult<R, E> = Result<R, ArchiveError<E>>; | 5 | pub type ArchiveResult<T, E> = Result<T, ArchiveError<E>>; |
| 6 | 6 | ||
| 7 | #[derive(Debug)] | 7 | #[derive(Debug)] |
| 8 | pub enum ArchiveError<E: Error> { | 8 | pub enum ArchiveError<E: Error> { |
| 9 | IO(io::Error), | 9 | IO { error: io::Error }, |
| 10 | Driver { name: &'static str, error: E }, | 10 | Serde { message: String }, |
| 11 | Archivator { module: String, error: E }, | ||
| 11 | } | 12 | } |
| 12 | 13 | ||
| 13 | impl<E: Error> From<io::Error> for ArchiveError<E> { | 14 | impl<E: Error> From<io::Error> for ArchiveError<E> { |
| 14 | fn from(value: io::Error) -> Self { | 15 | fn from(value: io::Error) -> Self { |
| 15 | Self::IO(value) | 16 | Self::IO { error: value } |
| 16 | } | 17 | } |
| 17 | } | 18 | } |
| 18 | 19 | ||
| 19 | impl<E: Error> Display for ArchiveError<E> { | 20 | impl<E: Error> Display for ArchiveError<E> { |
| 20 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | 21 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 21 | match self { | 22 | match self { |
| 22 | ArchiveError::IO(error) => write!(f, "{error}"), | 23 | Self::IO { error } => writeln!(f, "IO: {error}"), |
| 23 | ArchiveError::Driver { name, error } => write!(f, "{name}: {error}"), | 24 | Self::Serde { message } => writeln!(f, "Serde: {message}"), |
| 25 | Self::Archivator { module, error } => writeln!(f, "{module}: {error}"), | ||
| 24 | } | 26 | } |
| 25 | } | 27 | } |
| 26 | } | 28 | } |
| @@ -28,8 +30,25 @@ impl<E: Error> Display for ArchiveError<E> { | |||
| 28 | impl<E: Error> Error for ArchiveError<E> { | 30 | impl<E: Error> Error for ArchiveError<E> { |
| 29 | fn source(&self) -> Option<&(dyn Error + 'static)> { | 31 | fn source(&self) -> Option<&(dyn Error + 'static)> { |
| 30 | match self { | 32 | match self { |
| 31 | Self::IO(error) => Some(error), | 33 | Self::IO { error } => Some(error), |
| 32 | _ => None, | 34 | Self::Serde { .. } => None, |
| 35 | Self::Archivator { module, error } => None, | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | impl<E: Error> serde::ser::Error for ArchiveError<E> { | ||
| 41 | fn custom<T: Display>(msg: T) -> Self { | ||
| 42 | Self::Serde { | ||
| 43 | message: msg.to_string(), | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | impl<E: Error> serde::de::Error for ArchiveError<E> { | ||
| 49 | fn custom<T: Display>(msg: T) -> Self { | ||
| 50 | Self::Serde { | ||
| 51 | message: msg.to_string(), | ||
| 33 | } | 52 | } |
| 34 | } | 53 | } |
| 35 | } | 54 | } |
