| 1 | use std::{convert, error, fmt, io, result}; |
| 2 | |
| 3 | /// A list specifying general categories of LZ4 error. |
| 4 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
| 5 | #[non_exhaustive] |
| 6 | pub enum ErrorKind { |
| 7 | /// The state initialization failed for some reason. |
| 8 | InitializationFailed, |
| 9 | /// The compression failed for some reason. |
| 10 | CompressionFailed, |
| 11 | /// The decompression failed for some reason. |
| 12 | DecompressionFailed, |
| 13 | /// The frame header had an invalid value. |
| 14 | FrameHeaderInvalid, |
| 15 | /// The decompressor reached unexpected EOF. |
| 16 | CompressedDataIncomplete, |
| 17 | /// Dictionary data was not consistent during the streaming decompression. |
| 18 | DictionaryChangedDuringDecompression, |
| 19 | } |
| 20 | |
| 21 | impl fmt::Display for ErrorKind { |
| 22 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> result::Result<(), fmt::Error> { |
| 23 | <Self as fmt::Debug>::fmt(self, f) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /// The error type for LZ4 operations. |
| 28 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
| 29 | pub struct Error { |
| 30 | kind: ErrorKind, |
| 31 | } |
| 32 | |
| 33 | impl Error { |
| 34 | pub(crate) const fn new(kind: ErrorKind) -> Self { |
| 35 | Self { kind } |
| 36 | } |
| 37 | |
| 38 | /// Returns the corresponding `ErrorKind` for this error. |
| 39 | pub const fn kind(self) -> ErrorKind { |
| 40 | self.kind |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl convert::From<Error> for io::Error { |
| 45 | fn from(err: Error) -> Self { |
| 46 | Self::new(io::ErrorKind::Other, err) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl fmt::Display for Error { |
| 51 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> result::Result<(), fmt::Error> { |
| 52 | <ErrorKind as fmt::Display>::fmt(&self.kind, f) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | impl error::Error for Error {} |
| 57 | |
| 58 | /// A specialized [`Result`] type for LZ4 operations. |
| 59 | /// |
| 60 | /// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html |
| 61 | pub type Result<T> = result::Result<T, Error>; |