sevenz_rust/
error.rs

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