1use std::fmt;
2use std::io;
3
4#[derive(Debug)]
5#[non_exhaustive]
6pub enum ApeError {
7 Io(io::Error),
8 InvalidFormat(&'static str),
9 InvalidChecksum,
10 UnsupportedVersion(u16),
11 DecodingError(&'static str),
12}
13
14impl fmt::Display for ApeError {
15 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16 match self {
17 ApeError::Io(e) => write!(f, "I/O error: {}", e),
18 ApeError::InvalidFormat(msg) => write!(f, "invalid format: {}", msg),
19 ApeError::InvalidChecksum => write!(f, "invalid checksum"),
20 ApeError::UnsupportedVersion(v) => write!(f, "unsupported version: {}", v),
21 ApeError::DecodingError(msg) => write!(f, "decoding error: {}", msg),
22 }
23 }
24}
25
26impl std::error::Error for ApeError {
27 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
28 match self {
29 ApeError::Io(e) => Some(e),
30 _ => None,
31 }
32 }
33}
34
35impl From<io::Error> for ApeError {
36 fn from(e: io::Error) -> Self {
37 ApeError::Io(e)
38 }
39}
40
41pub type ApeResult<T> = Result<T, ApeError>;