async_zip/
error.rs

1// Copyright (c) 2021 Harry [Majored] [hello@majored.pw]
2// MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
3
4//! A module which holds relevant error reporting structures/types.
5
6use std::fmt::{Display, Formatter};
7use thiserror::Error;
8
9/// A Result type alias over ZipError to minimise repetition.
10pub type Result<V> = std::result::Result<V, ZipError>;
11
12#[derive(Debug, PartialEq, Eq)]
13pub enum Zip64ErrorCase {
14    TooManyFiles,
15    LargeFile,
16}
17
18impl Display for Zip64ErrorCase {
19    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20        match self {
21            Self::TooManyFiles => write!(f, "More than 65536 files in archive"),
22            Self::LargeFile => write!(f, "File is larger than 4 GiB"),
23        }
24    }
25}
26
27/// An enum of possible errors and their descriptions.
28#[non_exhaustive]
29#[derive(Debug, Error)]
30pub enum ZipError {
31    #[error("feature not supported: '{0}'")]
32    FeatureNotSupported(&'static str),
33    #[error("compression not supported: {0}")]
34    CompressionNotSupported(u16),
35    #[error("host attribute compatibility not supported: {0}")]
36    AttributeCompatibilityNotSupported(u16),
37    #[error("attempted to read a ZIP64 file whilst on a 32-bit target")]
38    TargetZip64NotSupported,
39    #[error("attempted to write a ZIP file with force_no_zip64 when ZIP64 is needed: {0}")]
40    Zip64Needed(Zip64ErrorCase),
41    #[error("end of file has not been reached")]
42    EOFNotReached,
43    #[error("extra fields exceeded maximum size")]
44    ExtraFieldTooLarge,
45    #[error("comment exceeded maximum size")]
46    CommentTooLarge,
47    #[error("filename exceeded maximum size")]
48    FileNameTooLarge,
49    #[error("attempted to convert non-UTF8 bytes to a string/str")]
50    StringNotUtf8,
51
52    #[error("unable to locate the end of central directory record")]
53    UnableToLocateEOCDR,
54    #[error("extra field size was indicated to be {0} but fewer than {0} bytes remain")]
55    InvalidExtraFieldHeader(u16),
56    #[error("zip64 extended information field was incomplete")]
57    Zip64ExtendedFieldIncomplete,
58    #[error("an extra field with id {0:#x} was duplicated in the header")]
59    DuplicateExtraFieldHeader(u16),
60
61    #[error("an upstream reader returned an error: {0}")]
62    UpstreamReadError(#[from] std::io::Error),
63    #[error("a computed CRC32 value did not match the expected value")]
64    CRC32CheckError,
65    #[error("entry index was out of bounds")]
66    EntryIndexOutOfBounds,
67    #[error("Encountered an unexpected header (actual: {0:#x}, expected: {1:#x}).")]
68    UnexpectedHeaderError(u32, u32),
69
70    #[error("Info-ZIP Unicode Comment Extra Field was incomplete")]
71    InfoZipUnicodeCommentFieldIncomplete,
72    #[error("Info-ZIP Unicode Path Extra Field was incomplete")]
73    InfoZipUnicodePathFieldIncomplete,
74
75    #[error("the end of central directory offset ({0:#x}) did not match the actual offset ({1:#x})")]
76    InvalidEndOfCentralDirectoryOffset(u64, u64),
77    #[error("the zip64 end of central directory locator was not found")]
78    MissingZip64EndOfCentralDirectoryLocator,
79    #[error("the zip64 end of central directory locator offset ({0:#x}) did not match the actual offset ({1:#x})")]
80    InvalidZip64EndOfCentralDirectoryLocatorOffset(u64, u64),
81    #[error(
82        "zip64 extended information field was too long: expected {expected} bytes, but {actual} bytes were provided"
83    )]
84    Zip64ExtendedInformationFieldTooLong { expected: usize, actual: usize },
85}