1#[derive(Debug)]
3pub struct Error {
4 inner: Box<ErrorInner>,
5}
6
7impl Error {
8 pub(crate) fn io(err: std::io::Error) -> Error {
9 Error::from(ErrorKind::IO(err))
10 }
11
12 pub(crate) fn utf8(err: std::str::Utf8Error) -> Error {
13 Error::from(ErrorKind::InvalidUtf8(err))
14 }
15
16 pub(crate) fn is_eof(&self) -> bool {
17 matches!(self.inner.kind, ErrorKind::Eof)
18 }
19
20 pub fn kind(&self) -> &ErrorKind {
22 &self.inner.kind
23 }
24}
25
26#[derive(Debug)]
27struct ErrorInner {
28 kind: ErrorKind,
29}
30
31#[derive(Debug)]
33#[non_exhaustive]
34pub enum ErrorKind {
35 MissingEndOfCentralDirectory,
37
38 MissingZip64EndOfCentralDirectory,
40
41 BufferTooSmall,
43
44 InvalidSignature { expected: u32, actual: u32 },
46
47 InvalidChecksum { expected: u32, actual: u32 },
49
50 InvalidSize { expected: u64, actual: u64 },
52
53 InvalidUtf8(std::str::Utf8Error),
55
56 InvalidInput { msg: String },
58
59 IO(std::io::Error),
61
62 Eof,
64}
65
66impl std::error::Error for Error {}
67
68impl std::fmt::Display for Error {
69 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
70 write!(f, "{}", self.inner.kind)?;
71 Ok(())
72 }
73}
74
75impl std::fmt::Display for ErrorKind {
76 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
77 match *self {
78 ErrorKind::IO(ref err) => err.fmt(f),
79 ErrorKind::MissingEndOfCentralDirectory => {
80 write!(f, "Missing end of central directory")
81 }
82 ErrorKind::MissingZip64EndOfCentralDirectory => {
83 write!(f, "Missing zip64 end of central directory")
84 }
85 ErrorKind::BufferTooSmall => {
86 write!(f, "Buffer size too small")
87 }
88 ErrorKind::Eof => {
89 write!(f, "Unexpected end of file")
90 }
91 ErrorKind::InvalidSignature { expected, actual } => {
92 write!(
93 f,
94 "Invalid signature: expected 0x{:08x}, got 0x{:08x}",
95 expected, actual
96 )
97 }
98 ErrorKind::InvalidChecksum { expected, actual } => {
99 write!(
100 f,
101 "Invalid checksum: expected 0x{:08x}, got 0x{:08x}",
102 expected, actual
103 )
104 }
105 ErrorKind::InvalidSize { expected, actual } => {
106 write!(f, "Invalid size: expected {}, got {}", expected, actual)
107 }
108 ErrorKind::InvalidUtf8(ref err) => {
109 write!(f, "Invalid UTF-8: {}", err)
110 }
111 ErrorKind::InvalidInput { ref msg } => {
112 write!(f, "Invalid input: {}", msg)
113 }
114 }
115 }
116}
117
118impl From<ErrorKind> for Error {
119 fn from(kind: ErrorKind) -> Error {
120 Error {
121 inner: Box::new(ErrorInner { kind }),
122 }
123 }
124}
125
126impl From<std::io::Error> for ErrorKind {
127 fn from(err: std::io::Error) -> ErrorKind {
128 ErrorKind::IO(err)
129 }
130}