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    /// Invalid 7z signature found in file header.
7    BadSignature([u8; 6]),
8    /// Unsupported 7z format version.
9    UnsupportedVersion {
10        /// Major version number.
11        major: u8,
12        /// Minor version number.
13        minor: u8,
14    },
15    /// Checksum verification failed during decompression.
16    ChecksumVerificationFailed,
17    /// Next header CRC mismatch.
18    NextHeaderCrcMismatch,
19    /// IO error with optional context message.
20    Io(std::io::Error, Cow<'static, str>),
21    /// Error opening file.
22    FileOpen(std::io::Error, String),
23    /// Other error with description.
24    Other(Cow<'static, str>),
25    /// Bad terminated streams info.
26    BadTerminatedStreamsInfo(u8),
27    /// Bad terminated unpack info.
28    BadTerminatedUnpackInfo,
29    /// Bad terminated pack info.
30    BadTerminatedPackInfo(u8),
31    /// Bad terminated sub streams info.
32    BadTerminatedSubStreamsInfo,
33    /// Bad terminated header.
34    BadTerminatedHeader(u8),
35    /// External compression method not supported.
36    ExternalUnsupported,
37    /// Unsupported compression method.
38    UnsupportedCompressionMethod(String),
39    /// Memory limit exceeded.
40    MaxMemLimited {
41        /// Maximum allowed memory in KB.
42        max_kb: usize,
43        /// Actual required memory in KB.
44        actaul_kb: usize,
45    },
46    /// Password required for encrypted archive.
47    PasswordRequired,
48    /// Feature or operation not supported.
49    Unsupported(Cow<'static, str>),
50    /// Possibly bad password for encrypted content.
51    MaybeBadPassword(std::io::Error),
52    /// File not found.
53    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 {}