| 1 |
use core::fmt; |
| 2 |
#[cfg(feature = "std")] |
| 3 |
use std::io; |
| 4 |
use crate::stub_io::Read; |
| 5 |
|
| 6 |
pub type LhaResult<T, R> = Result<T, LhaError<<R as Read>::Error>>; |
| 7 |
|
| 8 |
/// `delharc` error type. |
| 9 |
/// |
| 10 |
/// With `std` feature enabled `E` is [`std::io::Error`] and |
| 11 |
/// `LhaError` can be converted to [`std::io::Error`] using [`From`] or [`Into`]. |
| 12 |
#[derive(Debug, PartialEq, Eq)] |
| 13 |
#[non_exhaustive] |
| 14 |
pub enum LhaError<E> { |
| 15 |
/// I/O error. |
| 16 |
Io(E), |
| 17 |
/// When parsing LHA header. |
| 18 |
HeaderParse(&'static str), |
| 19 |
/// When decompressing a file. |
| 20 |
Decompress(&'static str), |
| 21 |
/// Checksum mismatch. |
| 22 |
Checksum(&'static str), |
| 23 |
} |
| 24 |
|
| 25 |
impl<E: fmt::Display> fmt::Display for LhaError<E> { |
| 26 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 27 |
use LhaError::*; |
| 28 |
match self { |
| 29 |
Io(e) => e.fmt(f), |
| 30 |
HeaderParse(e) => write!(f, "while parsing LHA header: {}", e), |
| 31 |
Decompress(e) => write!(f, "while decompressing: {}", e), |
| 32 |
Checksum(e) => write!(f, "checksum: {}", e), |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
#[cfg(feature = "std")] |
| 38 |
impl<E: std::error::Error + 'static> std::error::Error for LhaError<E> { |
| 39 |
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 40 |
use LhaError::*; |
| 41 |
match self { |
| 42 |
Io(e) => Some(e), |
| 43 |
_ => None |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
#[cfg(feature = "std")] |
| 49 |
impl From<LhaError<io::Error>> for io::Error { |
| 50 |
fn from(err: LhaError<io::Error>) -> Self { |
| 51 |
use LhaError::*; |
| 52 |
use io::{Error, ErrorKind}; |
| 53 |
match err { |
| 54 |
Io(e) => e, |
| 55 |
HeaderParse(e) => Error::new(ErrorKind::InvalidData, e), |
| 56 |
Decompress(e) => Error::new(ErrorKind::InvalidData, e), |
| 57 |
Checksum(e) => Error::new(ErrorKind::InvalidData, e), |
| 58 |
} |
| 59 |
} |
| 60 |
} |