use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ApfsError {
InputTooSmall,
InvalidMagic {
expected: [u8; 4],
actual: [u8; 4],
},
InvalidValue(&'static str),
ChecksumMismatch {
expected: u64,
actual: u64,
},
AddressOverflow,
Unsupported(&'static str),
Io(hadris_io::ErrorKind),
}
pub type Result<T> = core::result::Result<T, ApfsError>;
impl ApfsError {
pub fn from_io<E: hadris_io::IoError>(error: hadris_io::Error<E>) -> Self {
Self::Io(error.kind())
}
}
impl fmt::Display for ApfsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InputTooSmall => f.write_str("APFS input too small"),
Self::InvalidMagic { expected, actual } => {
write!(f, "invalid APFS magic {actual:?}, expected {expected:?}",)
}
Self::InvalidValue(name) => write!(f, "invalid APFS value: {name}"),
Self::ChecksumMismatch { expected, actual } => write!(
f,
"APFS checksum mismatch: expected {expected:#x}, got {actual:#x}"
),
Self::AddressOverflow => f.write_str("APFS address calculation overflowed"),
Self::Unsupported(feature) => write!(f, "unsupported APFS feature: {feature}"),
Self::Io(kind) => write!(f, "APFS I/O error: {kind:?}"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for ApfsError {}