use num_enum::TryFromPrimitiveError;
use std::io::Error as StdIoError;
use std::num::TryFromIntError;
use std::result::Result as StdResult;
use std::string::FromUtf8Error;
use thiserror::Error;
use crate::{
module::header::ModuleVersion,
tag::{
datablock::TagSectionType,
structure::{TagStructLocation, TagStructType},
},
};
#[derive(Error, Debug)]
pub enum ModuleError {
#[error("Incorrect module magic found! Expected '0x64686F6D', found {0:#X}!")]
IncorrectMagic(u32),
#[error("Incorrect module version found!")]
IncorrectVersion(#[from] TryFromPrimitiveError<ModuleVersion>),
#[error("Module file block index must be non-negative, found {0}")]
NegativeBlockIndex(i32),
#[error("Value for is_compressed incorrect!")]
IncorrectCompressedValue,
}
#[derive(Error, Debug)]
pub enum TagError {
#[error("Incorrect magic found! Expected '0x68736375', found {0:#X}!")]
IncorrectMagic(u32),
#[error("Incorrect version found! Expected '27', found {0}!")]
IncorrectVersion(i32),
#[error("Not been loaded yet!")]
NotLoaded,
#[error("Main struct not found!")]
MainStructNotFound,
#[error("Does not contain tag info!")]
NoTagInfo,
#[error("Invalid TagStruct type encountered!")]
InvalidTagSection(#[from] TryFromPrimitiveError<TagSectionType>),
#[error("Invalid TagStruct type encountered!")]
InvalidTagStruct(#[from] TryFromPrimitiveError<TagStructType>),
#[error("Failed to convert primitive to enum")]
NumEnumError,
#[error("Recursion depth reached 3!")]
RecursionDepth,
#[error("Invalid TagStruct location encountered!")]
InvalidTagStructLocation(#[from] TryFromPrimitiveError<TagStructLocation>),
}
#[derive(Error, Debug)]
pub enum DecompressionError {
#[error("Buffer size overflow")]
BufferSizeOverflow,
#[error("Decompression failed with error code {0}")]
DecompressionFailed(i32),
}
#[derive(Error, Debug)]
pub enum Error {
#[error("Failed to read from buffer!")]
ReadError(#[from] StdIoError),
#[error("Incorrect UTF-8 encoding found when reading string!")]
Utf8ReadingError(#[from] FromUtf8Error),
#[error("Error occurred while decompressing!")]
DecompressionError(#[from] DecompressionError),
#[error("Error occurred while loading a module!")]
ModuleError(#[from] ModuleError),
#[error("Integer conversion failed!")]
TryFromIntError(#[from] TryFromIntError),
#[error("Error occurred while loading a tag!")]
TagError(#[from] TagError),
}
pub type Result<T> = StdResult<T, Error>;