1use std::fmt::Display;
2
3use num_enum::TryFromPrimitiveError;
4
5use crate::files::bundle_file::CompressionType;
6
7#[derive(Debug)]
8pub enum Error {
9 IoError(std::io::Error),
10 ParseIntError(std::num::ParseIntError),
11 TryFromSliceError(std::array::TryFromSliceError),
12
13 FeatureDisabled(&'static str),
14 Unimplemented(&'static str),
15
16 InvalidRevision(String),
17 InvalidCompressionFlag(u32),
18 InvalidEndianness,
19 TypeTreeNotFound,
20 UnknownSignature,
21 InvalidValue(String),
22 DecompressionError(String),
23 NoUnityCNKey,
24
25 Message(String)
26}
27
28impl Display for Error {
29 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30 match self {
31 Self::IoError(e) => e.fmt(f),
32 Self::ParseIntError(e) => e.fmt(f),
33 Self::TryFromSliceError(e) => e.fmt(f),
34
35 Self::FeatureDisabled(name) => write!(f, "Feature disabled: {name}"),
36 Self::Unimplemented(reason) => write!(f, "Unimplemented: {reason}"),
37
38 Self::InvalidRevision(rev) => write!(f, "Invalid revision: {rev}"),
39 Self::InvalidCompressionFlag(flag) => write!(f, "Invalid compression flag: {flag}"),
40 Self::InvalidEndianness => f.write_str("Invalid endianness"),
41 Self::TypeTreeNotFound => f.write_str("Unable to find type tree"),
42 Self::UnknownSignature => f.write_str("Unknown signature"),
43 Self::InvalidValue(reason) => write!(f, "Invalid value: {reason}"),
44 Self::DecompressionError(reason) => write!(f, "Decompression error: {reason}"),
45 Self::NoUnityCNKey => f.write_str("UnityCN decryption key was not provided"),
46
47 Self::Message(reason) => f.write_str(&reason)
48 }
49 }
50}
51
52impl std::error::Error for Error {}
53
54impl From<std::io::Error> for Error {
55 fn from(e: std::io::Error) -> Self {
56 Self::IoError(e)
57 }
58}
59
60impl From<std::num::ParseIntError> for Error {
61 fn from(e: std::num::ParseIntError) -> Self {
62 Self::ParseIntError(e)
63 }
64}
65
66impl From<TryFromPrimitiveError<CompressionType>> for Error {
67 fn from(e: TryFromPrimitiveError<CompressionType>) -> Self {
68 Self::InvalidCompressionFlag(e.number)
69 }
70}
71
72impl From<std::array::TryFromSliceError> for Error {
73 fn from(e: std::array::TryFromSliceError) -> Self {
74 Self::TryFromSliceError(e)
75 }
76}
77
78#[cfg(feature = "lzma")]
79impl From<lzma_rs::error::Error> for Error {
80 fn from(e: lzma_rs::error::Error) -> Self {
81 Self::DecompressionError(e.to_string())
82 }
83}
84
85#[cfg(feature = "lz4")]
86impl From<lz4_flex::block::DecompressError> for Error {
87 fn from(e: lz4_flex::block::DecompressError) -> Self {
88 Self::DecompressionError(e.to_string())
89 }
90}
91
92#[cfg(feature = "serde")]
93impl serde::de::Error for Error {
94 fn custom<T: Display>(msg: T) -> Self {
95 Error::Message(msg.to_string())
96 }
97}