pyaxml_rs/
error.rs

1use std::fmt;
2
3/// Error type for AXML and ARSC parsing and manipulation.
4#[derive(Debug)]
5#[non_exhaustive]
6pub enum AxmlError {
7    /// Unexpected end of input while reading a required field.
8    UnexpectedEof,
9    /// Invalid magic bytes for AXML or ARSC format.
10    InvalidMagic(u16),
11    /// Invalid chunk type identifier.
12    InvalidChunkType(u16),
13    /// String index out of bounds (requested index, valid count).
14    InvalidStringIndex(usize, usize),
15    /// UTF-8 decoding error from the string pool.
16    Utf8Error(std::string::FromUtf8Error),
17    /// UTF-16 decoding error from the string pool.
18    Utf16Error(std::char::DecodeUtf16Error),
19    /// XML parsing or generation error.
20    XmlError(String),
21    /// Slice bounds or format error.
22    SliceError(String),
23    /// Protobuf message decoding error.
24    ProtoError(prost::DecodeError),
25    /// File I/O error.
26    IoError(std::io::Error),
27}
28
29impl fmt::Display for AxmlError {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            AxmlError::UnexpectedEof => write!(f, "Unexpected end of file"),
33            AxmlError::InvalidMagic(t) => write!(f, "Invalid file magic: 0x{:04x}", t),
34            AxmlError::InvalidChunkType(t) => write!(f, "Invalid chunk type: 0x{:04x}", t),
35            AxmlError::InvalidStringIndex(idx, len) => {
36                write!(f, "String index {} out of range (pool size: {})", idx, len)
37            }
38            AxmlError::Utf8Error(e) => write!(f, "UTF-8 decode error: {}", e),
39            AxmlError::Utf16Error(e) => write!(f, "UTF-16 decode error: {}", e),
40            AxmlError::XmlError(s) => write!(f, "XML error: {}", s),
41            AxmlError::SliceError(s) => write!(f, "Slice error: {}", s),
42            AxmlError::ProtoError(e) => write!(f, "Protobuf decode error: {}", e),
43            AxmlError::IoError(e) => write!(f, "I/O error: {}", e),
44        }
45    }
46}
47
48impl std::error::Error for AxmlError {}
49
50impl From<std::string::FromUtf8Error> for AxmlError {
51    fn from(e: std::string::FromUtf8Error) -> Self {
52        AxmlError::Utf8Error(e)
53    }
54}
55
56impl From<prost::DecodeError> for AxmlError {
57    fn from(e: prost::DecodeError) -> Self {
58        AxmlError::ProtoError(e)
59    }
60}
61
62impl From<std::char::DecodeUtf16Error> for AxmlError {
63    fn from(e: std::char::DecodeUtf16Error) -> Self {
64        AxmlError::Utf16Error(e)
65    }
66}
67
68impl From<std::io::Error> for AxmlError {
69    fn from(e: std::io::Error) -> Self {
70        AxmlError::IoError(e)
71    }
72}