use std::fmt;
use std::io;
#[derive(Debug)]
pub enum ChdError {
Io(io::Error),
InvalidMagic,
InvalidHeaderSize(u32),
UnsupportedVersion(u32),
InvalidData(String),
CrcMismatch {
expected: u16,
found: u16,
},
Huffman(chdlady_huffman::HuffmanError),
Codec(String),
HunkOutOfRange {
index: u64,
total: u64,
},
MetadataNotFound,
RequiresParent,
InvalidParent,
InvalidParameter(String),
}
impl fmt::Display for ChdError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(e) => write!(f, "I/O error: {}", e),
Self::InvalidMagic => write!(f, "invalid CHD magic signature"),
Self::InvalidHeaderSize(size) => write!(f, "invalid header size: {}", size),
Self::UnsupportedVersion(ver) => write!(f, "unsupported CHD version: {}", ver),
Self::InvalidData(msg) => write!(f, "invalid data: {}", msg),
Self::CrcMismatch { expected, found } => {
write!(
f,
"CRC-16 mismatch: expected {:#06x}, found {:#06x}",
expected, found
)
}
Self::Huffman(e) => write!(f, "Huffman error: {}", e),
Self::Codec(msg) => write!(f, "codec error: {}", msg),
Self::HunkOutOfRange { index, total } => {
write!(f, "hunk index {} out of range (total: {})", index, total)
}
Self::MetadataNotFound => write!(f, "metadata entry not found"),
Self::RequiresParent => write!(f, "parent CHD required but not provided"),
Self::InvalidParent => {
write!(f, "provided parent CHD does not match parent SHA-1")
}
Self::InvalidParameter(msg) => write!(f, "invalid parameter: {}", msg),
}
}
}
impl std::error::Error for ChdError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(e) => Some(e),
Self::Huffman(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for ChdError {
fn from(err: io::Error) -> Self {
Self::Io(err)
}
}
impl From<chdlady_huffman::HuffmanError> for ChdError {
fn from(err: chdlady_huffman::HuffmanError) -> Self {
Self::Huffman(err)
}
}