Skip to main content

zipatch_rs/
error.rs

1/// All failures returned by parsing or applying a `ZiPatch` stream.
2#[non_exhaustive]
3#[derive(Debug, thiserror::Error)]
4pub enum ZiPatchError {
5    /// Underlying I/O failure from the patch source or target filesystem.
6    #[error("I/O error: {0}")]
7    Io(#[from] std::io::Error),
8
9    /// The 12-byte `ZiPatch` magic header was missing or did not match.
10    #[error("invalid magic bytes")]
11    InvalidMagic,
12
13    /// A 4-byte chunk tag was not recognised by the parser.
14    #[error("unknown chunk tag: {0:?}")]
15    UnknownChunkTag([u8; 4]),
16
17    /// A SQPK sub-command byte was not recognised by the parser.
18    #[error("unknown SQPK command: {0:#x}")]
19    UnknownSqpkCommand(u8),
20
21    /// A chunk's recorded CRC32 did not match the computed CRC32.
22    #[error("CRC32 mismatch on chunk {tag:?}: expected {expected:#010x}, got {actual:#010x}")]
23    ChecksumMismatch {
24        /// Tag of the chunk whose checksum failed.
25        tag: [u8; 4],
26        /// CRC32 stored in the patch file.
27        expected: u32,
28        /// CRC32 computed over the actual chunk bytes.
29        actual: u32,
30    },
31
32    /// DEFLATE decompression of a `SqpkFile` block failed.
33    #[error("decompression error")]
34    Decompress(#[source] std::io::Error),
35
36    /// A field value failed a parser invariant (e.g. negative size).
37    #[error("invalid field: {context}")]
38    InvalidField {
39        /// Human-readable description of which field was invalid.
40        context: &'static str,
41    },
42
43    /// A chunk declared a size larger than the parser's maximum.
44    #[error("chunk size {0} exceeds maximum")]
45    OversizedChunk(usize),
46
47    /// A `SqpkFile` operation byte was not recognised.
48    #[error("unknown SqpkFile operation: {0:#02x}")]
49    UnknownFileOperation(u8),
50
51    /// A UTF-8 decode failed when reading a path or name field.
52    #[error("UTF-8 decode error")]
53    Utf8Error(#[from] std::string::FromUtf8Error),
54
55    /// A `binrw` parser produced an error; wraps the underlying cause.
56    #[error("binrw parse error: {0}")]
57    BinrwError(#[from] binrw::Error),
58
59    /// A `SqpkFile` carried a negative `file_offset` that cannot be applied.
60    #[error("negative file_offset in SqpkFile: {0}")]
61    NegativeFileOffset(i64),
62
63    /// Stream ended without an `EOF_` chunk; download or copy was truncated.
64    #[error("patch file ended without EOF_ chunk (truncated download?)")]
65    TruncatedPatch,
66}