1use std::fmt;
2
3#[derive(Debug)]
5#[non_exhaustive]
6pub enum AxmlError {
7 UnexpectedEof,
9 InvalidMagic(u16),
11 InvalidChunkType(u16),
13 InvalidStringIndex(usize, usize),
15 Utf8Error(std::string::FromUtf8Error),
17 Utf16Error(std::char::DecodeUtf16Error),
19 XmlError(String),
21 SliceError(String),
23 ProtoError(prost::DecodeError),
25 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}