| 1 | use std::{convert, fmt, io}; |
| 2 | |
| 3 | /// A list specifying general categories of LZ4F error. |
| 4 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
| 5 | #[non_exhaustive] |
| 6 | #[allow(missing_docs)] |
| 7 | pub enum ErrorKind { |
| 8 | Generic, |
| 9 | MaxBlockSizeInvalid, |
| 10 | BlockModeInvalid, |
| 11 | ContentChecksumFlagInvalid, |
| 12 | CompressionLevelInvalid, |
| 13 | HeaderVersionWrong, |
| 14 | BlockChecksumInvalid, |
| 15 | ReservedFlagSet, |
| 16 | AllocationFailed, |
| 17 | SrcSizeTooLarge, |
| 18 | DstMaxSizeTooSmall, |
| 19 | FrameHeaderIncomplete, |
| 20 | FrameTypeUnknown, |
| 21 | FrameSizeWrong, |
| 22 | SrcPtrWrong, |
| 23 | DecompressionFailed, |
| 24 | HeaderChecksumInvalid, |
| 25 | ContentChecksumInvalid, |
| 26 | FrameDecodingAlreadyStarted, |
| 27 | } |
| 28 | |
| 29 | impl fmt::Display for ErrorKind { |
| 30 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> { |
| 31 | <Self as fmt::Debug>::fmt(self, f) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /// The error type for LZ4F operations. |
| 36 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
| 37 | pub enum Error { |
| 38 | Lz4f(ErrorKind), |
| 39 | Common(crate::ErrorKind), |
| 40 | } |
| 41 | |
| 42 | impl Error { |
| 43 | pub(super) const fn new(kind: ErrorKind) -> Self { |
| 44 | Self::Lz4f(kind) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl convert::From<Error> for io::Error { |
| 49 | fn from(err: Error) -> Self { |
| 50 | Self::new(io::ErrorKind::Other, err) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl convert::From<crate::Error> for Error { |
| 55 | fn from(err: crate::Error) -> Self { |
| 56 | Self::Common(err.kind()) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl fmt::Display for Error { |
| 61 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> { |
| 62 | match self { |
| 63 | Self::Lz4f(kind) => <ErrorKind as fmt::Display>::fmt(kind, f), |
| 64 | Self::Common(kind) => <crate::ErrorKind as fmt::Display>::fmt(kind, f), |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl std::error::Error for Error {} |
| 70 | |
| 71 | /// A specialized [`Result`] type for LZ4F operations. |
| 72 | /// |
| 73 | /// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html |
| 74 | pub type Result<T> = std::result::Result<T, Error>; |