diff options
Diffstat (limited to 'src/error.rs')
| -rw-r--r-- | src/error.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..6d7aba4 --- /dev/null +++ b/src/error.rs | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | use std::error::Error; | ||
| 2 | use std::fmt::Display; | ||
| 3 | use std::io; | ||
| 4 | |||
| 5 | pub type ArchiveResult<R, E> = Result<R, ArchiveError<E>>; | ||
| 6 | |||
| 7 | #[derive(Debug)] | ||
| 8 | pub enum ArchiveError<E: Error> { | ||
| 9 | IO(io::Error), | ||
| 10 | Driver { name: &'static str, error: E }, | ||
| 11 | } | ||
| 12 | |||
| 13 | impl<E: Error> From<io::Error> for ArchiveError<E> { | ||
| 14 | fn from(value: io::Error) -> Self { | ||
| 15 | Self::IO(value) | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | impl<E: Error> Display for ArchiveError<E> { | ||
| 20 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 21 | match self { | ||
| 22 | ArchiveError::IO(error) => write!(f, "{error}"), | ||
| 23 | ArchiveError::Driver { name, error } => write!(f, "{name}: {error}"), | ||
| 24 | } | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | impl<E: Error> Error for ArchiveError<E> { | ||
| 29 | fn source(&self) -> Option<&(dyn Error + 'static)> { | ||
| 30 | match self { | ||
| 31 | Self::IO(error) => Some(error), | ||
| 32 | _ => None, | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
