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_msg(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_msg(e: std::io::Error, msg: impl Into<Cow<'static, str>>) -> Self {
75 Self::Io(e, msg.into())
76 }
77
78 pub(crate) fn bad_password(e: std::io::Error, encryped: bool) -> Self {
79 if encryped {
80 Self::MaybeBadPassword(e)
81 } else {
82 Self::io_msg(e, "")
83 }
84 }
85
86 #[cfg(not(target_arch = "wasm32"))]
87 #[inline]
88 pub(crate) fn file_open(e: std::io::Error, filename: impl Into<Cow<'static, str>>) -> Self {
89 Self::Io(e, filename.into())
90 }
91
92 pub(crate) fn maybe_bad_password(self, encryped: bool) -> Self {
93 if !encryped {
94 return self;
95 }
96 match self {
97 Self::Io(e, s) if s.is_empty() => Self::MaybeBadPassword(e),
98 _ => self,
99 }
100 }
101}
102
103impl Display for Error {
104 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105 std::fmt::Debug::fmt(&self, f)
106 }
107}
108
109impl std::error::Error for Error {}