Skip to main content

simple_archive/
error.rs

1use crate::carchive;
2use std::{ffi::CStr, io, str::Utf8Error};
3
4#[derive(thiserror::Error, Debug)]
5pub enum Error {
6    #[error("Extraction error: '{0}'")]
7    Extraction(String),
8
9    #[error(transparent)]
10    IO(#[from] std::io::Error),
11
12    #[error("Error converting to UTF8")]
13    Utf8(#[from] Utf8Error),
14
15    #[error("Unknown error: {0}")]
16    Unknown(String),
17
18    #[error("Archive has been allocated but no filter nor format has been defined")]
19    IncompleteInitialization,
20
21    #[error("Unknown filter")]
22    UnknownFilter,
23
24    #[error("Unknown format")]
25    UnknownFormat,
26
27    #[error("Error to create the archive struct, is null")]
28    NullArchive,
29}
30
31impl From<*mut carchive::archive> for Error {
32    #[allow(clippy::not_unsafe_ptr_arg_deref)]
33    fn from(input: *mut carchive::archive) -> Self {
34        let errno;
35        unsafe {
36            let error_string = carchive::archive_error_string(input);
37            if !error_string.is_null() {
38                return Error::Extraction(
39                    CStr::from_ptr(error_string).to_string_lossy().to_string(),
40                );
41            }
42
43            errno = carchive::archive_errno(input);
44        }
45
46        if errno != 0 {
47            return io::Error::from_raw_os_error(errno).into();
48        }
49
50        Error::Unknown("Cannot read error string from archive".to_owned())
51    }
52}
53
54impl From<Error> for io::Error {
55    fn from(value: Error) -> Self {
56        io::Error::new(io::ErrorKind::Other, value)
57    }
58}