unrar_async/
error.rs

1/// The main error type for `unrar-async`
2#[derive(Clone, Debug, thiserror::Error)]
3pub enum Error {
4	#[error("rar error: {0}")]
5	Rar(#[from] RarError),
6	#[error("string nul error: {0}")]
7	NulString(#[from] NulError),
8	#[error("error converting to an FFI string: {0}")]
9	FFIStringConversionBytes(#[from] std::ffi::FromBytesWithNulError),
10	#[error("error converting to an FFI string: {0}")]
11	FFIStringConversionVec(#[from] std::ffi::FromVecWithNulError),
12	#[cfg(feature = "tokio")]
13	#[error("tokio error")]
14	Join,
15	#[error("nul handle returned by core")]
16	NulHandle,
17	#[error("input string too long for buffer ({0}/{1})")]
18	BufferOverflow(usize, usize),
19	#[error("failed to pack into an array for FFI: {0}")]
20	TryFromSlice(#[from] std::array::TryFromSliceError),
21	#[error("string from FFI was not valid UTF-8: {0}")]
22	UTF8(#[from] std::string::FromUtf8Error)
23}
24
25#[cfg(feature = "tokio")]
26impl From<tokio::task::JoinError> for Error {
27	#[inline]
28	fn from(_e: tokio::task::JoinError) -> Self {
29		Self::Join
30	}
31}
32
33/// The various errors that can occur while processing a `.rar` archive
34#[derive(Clone, Debug, thiserror::Error)]
35pub enum RarError {
36	#[error("Archive header damaged")]
37	ArchiveHeader,
38	#[error("File header damaged")]
39	FileHeader,
40	#[error("File checksum mismatch")]
41	FileChecksum,
42	#[error("Unknown encryption")]
43	UnknownEncryption,
44	#[error("Could not open next volume")]
45	OpenVolume,
46	#[error("Unknown archive format")]
47	UnknownFormat,
48	#[error("could not open archive")]
49	OpenArchive,
50	#[error("Not enough memory")]
51	OOM,
52	#[error("Not a RAR archive")]
53	BadArchive,
54	#[error("Could not create file")]
55	CreateFile,
56	#[error("Could not close file")]
57	CloseFile,
58	#[error("Read error")]
59	Read,
60	#[error("Write error")]
61	Write,
62	#[error("Archive comment was truncated to fit buffer")]
63	CommentTruncated,
64	#[error("Password for encrypted archive not specified")]
65	MissingPassword,
66	#[error("Could not open file source for reference record")]
67	Reference,
68	#[error("Wrong password was specified")]
69	BadPassword,
70	#[error("Archive end")]
71	EndArchive,
72	#[error("Unknown error")]
73	Unknown,
74	#[error("Invalid return code: {0}")]
75	InvalidCode(u32)
76}
77
78impl From<(Code, When)> for RarError {
79	fn from(input: (Code, When)) -> Self {
80		use self::Code::*;
81		use self::When::*;
82		match (input.0, input.1) {
83			(BadData, Open) => Self::ArchiveHeader,
84			(BadData, Read) => Self::FileHeader,
85			(BadData, Process) => Self::FileChecksum,
86			(UnknownFormat, Open) => Self::UnknownEncryption,
87			(OpenError, Process) => Self::OpenVolume,
88			(UnknownFormat, _) => Self::UnknownFormat,
89			(OpenError, _) => Self::OpenArchive,
90			(NoMemory, _) => Self::OOM,
91			(BadArchive, _) => Self::BadArchive,
92			(CreateError, _) => Self::CreateFile,
93			(CloseError, _) => Self::CloseFile,
94			(ReadError, _) => Self::Read,
95			(WriteError, _) => Self::Write,
96			(SmallBuffer, _) => Self::CommentTruncated,
97			(Unknown, _) => Self::Unknown,
98			(MissingPassword, _) => Self::MissingPassword,
99			(ReferenceError, _) => Self::Reference,
100			(BadPassword, _) => Self::BadPassword,
101			(EndArchive, _) => Self::EndArchive,
102			(Success, _) => unreachable!()
103		}
104	}
105}
106
107/// Return codes from the C++ unrar function calls
108#[derive(Clone, Copy, Debug, Eq, PartialEq, enum_primitive_derive::Primitive)]
109#[repr(u8)]
110pub enum Code {
111	Success = unrar_sys::ERAR_SUCCESS as u8,
112	EndArchive = unrar_sys::ERAR_END_ARCHIVE as u8,
113	NoMemory = unrar_sys::ERAR_NO_MEMORY as u8,
114	BadData = unrar_sys::ERAR_BAD_DATA as u8,
115	BadArchive = unrar_sys::ERAR_BAD_ARCHIVE as u8,
116	UnknownFormat = unrar_sys::ERAR_UNKNOWN_FORMAT as u8,
117	OpenError = unrar_sys::ERAR_EOPEN as u8,
118	CreateError = unrar_sys::ERAR_ECREATE as u8,
119	CloseError = unrar_sys::ERAR_ECLOSE as u8,
120	ReadError = unrar_sys::ERAR_EREAD as u8,
121	WriteError = unrar_sys::ERAR_EWRITE as u8,
122	SmallBuffer = unrar_sys::ERAR_SMALL_BUF as u8,
123	Unknown = unrar_sys::ERAR_UNKNOWN as u8,
124	MissingPassword = unrar_sys::ERAR_MISSING_PASSWORD as u8,
125	ReferenceError = unrar_sys::ERAR_EREFERENCE as u8,
126	BadPassword = unrar_sys::ERAR_BAD_PASSWORD as u8
127}
128
129/// Errors that can occur around nul bytes while processing strings
130#[derive(Clone, Debug, thiserror::Error)]
131pub enum NulError {
132	#[error("nul value found at position {0}")]
133	ContainsNul(usize),
134	#[error("")]
135	MissingNulTerminator
136}
137
138impl<C> From<widestring::error::ContainsNul<C>> for NulError {
139	fn from(e: widestring::error::ContainsNul<C>) -> Self {
140		Self::ContainsNul(e.nul_position())
141	}
142}
143
144/*
145impl<C> From<widestring::error::NulError<C>> for NulError {
146	fn from(e: widestring::error::NulError<C>) -> Self {
147		match e {
148			widestring::error::NulError::MissingNulTerminator(_) => Self::MissingNulTerminator,
149			widestring::error::NulError::ContainsNul(c) => Self::ContainsNul(c.nul_position())
150		}
151	}
152}
153*/
154
155impl From<std::ffi::NulError> for NulError {
156	fn from(e: std::ffi::NulError) -> Self {
157		Self::ContainsNul(e.nul_position())
158	}
159}
160
161/*
162impl From<std::ffi::FromBytesWithNulError> for NulError {
163	fn from(e: std::ffi::FromBytesWithNulError) -> Self {
164		match e.kind {
165			std::ffi::FromBytesWithNulErrorKind::InteriorNul(pos) => Self::ContainsNul(pos),
166			std::ffi::FromBytesWithNulErrorKind::NotNulTerminated => Self::MissingNulTerminator
167		}
168	}
169}
170*/
171
172/// Codes for the phases of handling a `.rar` archive
173#[derive(Clone, Copy, Debug, Eq, PartialEq)]
174pub(crate) enum When {
175	Open,
176	Read,
177	Process
178}
179