sevenz_rust2/
error.rs

1use std::{borrow::Cow, fmt::Display};
2
3/// The error type of the crate.
4#[derive(Debug)]
5pub enum Error {
6    BadSignature([u8; 6]),
7    UnsupportedVersion { major: u8, minor: u8 },
8    ChecksumVerificationFailed,
9    NextHeaderCrcMismatch,
10    Io(std::io::Error, Cow<'static, str>),
11    FileOpen(std::io::Error, String),
12    Other(Cow<'static, str>),
13    BadTerminatedStreamsInfo(u8),
14    BadTerminatedUnpackInfo,
15    BadTerminatedPackInfo(u8),
16    BadTerminatedSubStreamsInfo,
17    BadTerminatedheader(u8),
18    ExternalUnsupported,
19    UnsupportedCompressionMethod(String),
20    MaxMemLimited { max_kb: usize, actaul_kb: usize },
21    PasswordRequired,
22    Unsupported(Cow<'static, str>),
23    MaybeBadPassword(std::io::Error),
24    FileNotFound,
25}
26
27impl From<std::io::Error> for Error {
28    fn from(value: std::io::Error) -> Self {
29        Self::io(value)
30    }
31}
32
33impl Error {
34    #[inline]
35    pub fn other<S: Into<Cow<'static, str>>>(s: S) -> Self {
36        Self::Other(s.into())
37    }
38
39    #[inline]
40    pub fn unsupported<S: Into<Cow<'static, str>>>(s: S) -> Self {
41        Self::Unsupported(s.into())
42    }
43
44    #[inline]
45    pub fn io(e: std::io::Error) -> Self {
46        Self::io_msg(e, "")
47    }
48
49    #[inline]
50    pub fn io_msg(e: std::io::Error, msg: impl Into<Cow<'static, str>>) -> Self {
51        Self::Io(e, msg.into())
52    }
53
54    pub fn bad_password(e: std::io::Error, encryped: bool) -> Self {
55        if encryped {
56            Self::MaybeBadPassword(e)
57        } else {
58            Self::io(e)
59        }
60    }
61
62    #[cfg(not(target_arch = "wasm32"))]
63    #[inline]
64    pub(crate) fn file_open(e: std::io::Error, filename: impl Into<Cow<'static, str>>) -> Self {
65        Self::Io(e, filename.into())
66    }
67
68    pub(crate) fn maybe_bad_password(self, encryped: bool) -> Self {
69        if !encryped {
70            return self;
71        }
72        match self {
73            Self::Io(e, s) if s.is_empty() => Self::MaybeBadPassword(e),
74            _ => self,
75        }
76    }
77}
78
79impl Display for Error {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        std::fmt::Debug::fmt(&self, f)
82    }
83}
84
85impl std::error::Error for Error {}