jimage_rs/
error.rs

1use snafu::prelude::*;
2use std::array::TryFromSliceError;
3use std::path::PathBuf;
4
5/// Represents errors that can occur while working with JImage files.
6#[derive(Debug, Snafu)]
7#[snafu(visibility(pub))]
8pub enum JImageError {
9    #[snafu(display("File is not a valid jimage. Found magic: {magic:02x?}"))]
10    Magic {
11        magic: [u8; 4],
12        context: &'static str,
13    },
14    #[snafu(display("Unsupported jimage version: {major_version}.{minor_version}"))]
15    Version {
16        major_version: u16,
17        minor_version: u16,
18    },
19    #[snafu(display("Failed read from slice: [{from}..{to}]"))]
20    RawRead { from: usize, to: usize },
21    #[snafu(display(
22        "Utf8 Error: {source}. Invalid data (lossy): '{}'",
23        String::from_utf8_lossy(invalid_data)
24    ))]
25    Utf8 {
26        #[snafu(source)]
27        source: std::str::Utf8Error,
28        invalid_data: Vec<u8>,
29    },
30    #[snafu(display("I/O error for path '{path}': {source}", path = path.display()))]
31    Io {
32        #[snafu(source)]
33        source: std::io::Error,
34        path: PathBuf,
35    },
36    #[snafu(display("Failed during zlib decompression: {source}"))]
37    Decompression {
38        #[snafu(source)]
39        source: std::io::Error,
40    },
41    #[snafu(display("Unsupported decompressor: {decompressor_name}"))]
42    UnsupportedDecompressor { decompressor_name: String },
43    #[snafu(display("Internal error: {value}"))]
44    Internal { value: String },
45}
46
47/// Type alias for a Result type that uses JImageError for error handling.
48pub type Result<T> = std::result::Result<T, JImageError>;
49
50impl From<TryFromSliceError> for JImageError {
51    fn from(value: TryFromSliceError) -> Self {
52        JImageError::Internal {
53            value: value.to_string(),
54        }
55    }
56}