1use core::fmt;
2
3#[derive(Debug)]
5pub enum Error {
6 Io(hadris_io::Error),
8 InvalidMagic {
10 found: [u8; 6],
12 },
13 InvalidHexField {
15 field: &'static str,
17 },
18 InvalidFilename,
20 InvalidHeader {
22 reason: &'static str,
24 },
25 MissingTrailer,
27 ChecksumMismatch {
29 expected: u32,
31 computed: u32,
33 },
34 BufferSizeMismatch {
36 expected: usize,
38 actual: usize,
40 },
41 #[cfg(feature = "write")]
43 UnresolvedHardLink {
44 ino: u32,
46 },
47 #[cfg(feature = "write")]
49 FilenameTooLong,
50 #[cfg(feature = "write")]
52 FileTooLarge,
53}
54
55impl fmt::Display for Error {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 match self {
58 Self::Io(e) => write!(f, "I/O error: {e:?}"),
59 Self::InvalidMagic { found } => {
60 write!(
61 f,
62 "invalid CPIO magic: expected 070701 or 070702, found {:?}",
63 core::str::from_utf8(found).unwrap_or("<invalid>")
64 )
65 }
66 Self::InvalidHexField { field } => {
67 write!(f, "invalid hex field: {field}")
68 }
69 Self::InvalidFilename => write!(f, "invalid filename"),
70 Self::InvalidHeader { reason } => write!(f, "invalid CPIO header: {reason}"),
71 Self::MissingTrailer => write!(f, "missing TRAILER!!! sentinel"),
72 Self::ChecksumMismatch { expected, computed } => {
73 write!(
74 f,
75 "checksum mismatch: expected {expected:#010x}, computed {computed:#010x}"
76 )
77 }
78 Self::BufferSizeMismatch { expected, actual } => {
79 write!(
80 f,
81 "buffer size mismatch: entry is {expected} bytes, buffer is {actual} bytes"
82 )
83 }
84 #[cfg(feature = "write")]
85 Self::UnresolvedHardLink { ino } => {
86 write!(f, "unresolved hard link: inode {ino}")
87 }
88 #[cfg(feature = "write")]
89 Self::FilenameTooLong => write!(f, "filename too long"),
90 #[cfg(feature = "write")]
91 Self::FileTooLarge => write!(f, "file too large"),
92 }
93 }
94}
95
96#[cfg(feature = "std")]
97impl std::error::Error for Error {}
98
99impl<E: hadris_io::IoError> From<hadris_io::Error<E>> for Error {
100 fn from(e: hadris_io::Error<E>) -> Self {
101 Self::Io(e.erase())
102 }
103}
104
105pub type Result<T> = core::result::Result<T, Error>;