1use std::{borrow::Cow, fmt::Display};
2
3#[derive(Debug)]
5pub enum Error {
6 BadSignature([u8; 6]),
8 UnsupportedVersion {
10 major: u8,
12 minor: u8,
14 },
15 ChecksumVerificationFailed,
17 NextHeaderCrcMismatch,
19 Io(std::io::Error, Cow<'static, str>),
21 FileOpen(std::io::Error, String),
23 Other(Cow<'static, str>),
25 BadTerminatedStreamsInfo(u8),
27 BadTerminatedUnpackInfo,
29 BadTerminatedPackInfo(u8),
31 BadTerminatedSubStreamsInfo,
33 BadTerminatedHeader(u8),
35 ExternalUnsupported,
37 UnsupportedCompressionMethod(String),
39 MaxMemLimited {
41 max_kb: usize,
43 actaul_kb: usize,
45 },
46 PasswordRequired,
48 Unsupported(Cow<'static, str>),
50 MaybeBadPassword(std::io::Error),
52 FileNotFound,
54}
55
56impl From<std::io::Error> for Error {
57 fn from(value: std::io::Error) -> Self {
58 Self::io(value)
59 }
60}
61
62impl Error {
63 #[inline]
64 pub(crate) fn other<S: Into<Cow<'static, str>>>(s: S) -> Self {
65 Self::Other(s.into())
66 }
67
68 #[inline]
69 pub(crate) fn unsupported<S: Into<Cow<'static, str>>>(s: S) -> Self {
70 Self::Unsupported(s.into())
71 }
72
73 #[inline]
74 pub(crate) fn io(e: std::io::Error) -> Self {
75 Self::io_msg(e, "")
76 }
77
78 #[inline]
79 pub(crate) fn io_msg(e: std::io::Error, msg: impl Into<Cow<'static, str>>) -> Self {
80 Self::Io(e, msg.into())
81 }
82
83 pub(crate) fn bad_password(e: std::io::Error, encryped: bool) -> Self {
84 if encryped {
85 Self::MaybeBadPassword(e)
86 } else {
87 Self::io(e)
88 }
89 }
90
91 #[cfg(not(target_arch = "wasm32"))]
92 #[inline]
93 pub(crate) fn file_open(e: std::io::Error, filename: impl Into<Cow<'static, str>>) -> Self {
94 Self::Io(e, filename.into())
95 }
96
97 pub(crate) fn maybe_bad_password(self, encryped: bool) -> Self {
98 if !encryped {
99 return self;
100 }
101 match self {
102 Self::Io(e, s) if s.is_empty() => Self::MaybeBadPassword(e),
103 _ => self,
104 }
105 }
106}
107
108impl Display for Error {
109 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110 std::fmt::Debug::fmt(&self, f)
111 }
112}
113
114impl std::error::Error for Error {}