1use core::fmt;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum ApfsError {
6 InputTooSmall,
8 InvalidMagic {
10 expected: [u8; 4],
12 actual: [u8; 4],
14 },
15 InvalidValue(&'static str),
17 ChecksumMismatch {
19 expected: u64,
21 actual: u64,
23 },
24 AddressOverflow,
26 Unsupported(&'static str),
28 Io(hadris_io::ErrorKind),
30}
31
32pub type Result<T> = core::result::Result<T, ApfsError>;
34
35impl ApfsError {
36 pub fn from_io<E: hadris_io::IoError>(error: hadris_io::Error<E>) -> Self {
38 Self::Io(error.kind())
39 }
40}
41
42impl fmt::Display for ApfsError {
43 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44 match self {
45 Self::InputTooSmall => f.write_str("APFS input too small"),
46 Self::InvalidMagic { expected, actual } => {
47 write!(f, "invalid APFS magic {actual:?}, expected {expected:?}",)
48 }
49 Self::InvalidValue(name) => write!(f, "invalid APFS value: {name}"),
50 Self::ChecksumMismatch { expected, actual } => write!(
51 f,
52 "APFS checksum mismatch: expected {expected:#x}, got {actual:#x}"
53 ),
54 Self::AddressOverflow => f.write_str("APFS address calculation overflowed"),
55 Self::Unsupported(feature) => write!(f, "unsupported APFS feature: {feature}"),
56 Self::Io(kind) => write!(f, "APFS I/O error: {kind:?}"),
57 }
58 }
59}
60
61#[cfg(feature = "std")]
62impl std::error::Error for ApfsError {}