use alloc::boxed::Box;
use alloc::string::{String, ToString};
use core::error::Error;
use core::fmt::Debug;
use azathoth_utils::errors::AzUtilErrorCode;
#[cfg(feature = "std")]
use dataparser_core::DataParseError;
#[derive(Debug)]
pub enum DonutError {
Unknown {
e: String,
},
Generic {
e: Box<dyn Error + Send + Sync + 'static>,
},
Unsupported,
InvalidFormat,
InvalidEngine,
InvalidEntropy,
InvalidServer,
InvalidParameter,
InvalidParameterStr(String),
InvalidHeader,
ParseFailed,
NotFound(String),
SizeMismatch,
HashMismatch,
HeaderMismatch,
VersionMismatch,
ApiResolutionFailure,
ApiResolutionFailure2(u32),
CompressionFailure,
Io(String),
GoblinError(String),
ModuleError,
InstanceError,
NetworkFailure,
DecompressionFailure,
#[cfg(feature = "std")]
DataParseError(String),
#[cfg(feature = "std")]
SerializationError(String),
UnexpectedEof,
UnsupportedSignature(String),
DotnetError,
BuildError(String),
BadAlignment,
AllocFailed,
Other(String),
Azathoth(String),
CryptoError,
}
impl DonutError {
pub fn as_u32(&self) -> u32 {
match self {
DonutError::Unknown { .. } => 88,
DonutError::Generic { .. } => 89,
DonutError::Other(_) => 90,
DonutError::Unsupported => 91,
DonutError::InvalidFormat => 1,
DonutError::InvalidEngine => 2,
DonutError::InvalidEntropy => 3,
DonutError::InvalidServer => 4,
DonutError::InvalidParameter => 5,
DonutError::InvalidParameterStr(_) => 6,
DonutError::InvalidHeader => 7,
DonutError::ParseFailed => 8,
DonutError::NotFound(_) => 9,
DonutError::SizeMismatch => 10,
DonutError::HashMismatch => 11,
DonutError::HeaderMismatch => 12,
DonutError::VersionMismatch => 13,
DonutError::ApiResolutionFailure => 14,
DonutError::CompressionFailure => 15,
DonutError::Io(_) => 16,
DonutError::GoblinError(_) => 17,
DonutError::ModuleError => 18,
DonutError::InstanceError => 19,
DonutError::NetworkFailure => 20,
DonutError::DecompressionFailure => 21,
DonutError::BadAlignment => 22,
DonutError::AllocFailed => 23,
DonutError::Azathoth(_) => 27,
DonutError::CryptoError => 28,
#[cfg(feature = "std")]
DonutError::DataParseError(_) => 29,
DonutError::UnexpectedEof => 30,
DonutError::UnsupportedSignature(_) => 31,
DonutError::DotnetError => 32,
DonutError::BuildError(_) => 33,
#[cfg(feature = "std")]
DonutError::SerializationError(_) => 34,
DonutError::ApiResolutionFailure2(e) => pack16(35, *e as u16)
}
}
pub fn as_str(&self) -> &'static str {
match self {
DonutError::Unknown { .. } => "Unknown Error",
DonutError::Generic { .. } => "Generic Error",
DonutError::Other(_) => "Other Error",
DonutError::Unsupported => "Unsupported Value",
DonutError::ParseFailed => "Failed to parse",
DonutError::InvalidFormat => "Invalid format",
DonutError::InvalidParameter => "Invalid parameter",
DonutError::InvalidEngine => "Invalid engine",
DonutError::InvalidEntropy => "Invalid entropy",
DonutError::InvalidServer => "Invalid server",
DonutError::NotFound(_) => "File not found",
DonutError::SizeMismatch => "Size mismatch",
DonutError::HashMismatch => "Hash mismatch",
DonutError::HeaderMismatch => "Header mismatch",
DonutError::VersionMismatch => "Version mismatch",
DonutError::ApiResolutionFailure => "API resolution failure",
DonutError::CompressionFailure => "Compression failure",
DonutError::Io(_) => "IO error",
DonutError::GoblinError(_) => "Goblin error",
DonutError::ModuleError => "Module error",
DonutError::InstanceError => "Instance error",
DonutError::NetworkFailure => "Network failure",
DonutError::DecompressionFailure => "Decompression failure",
#[cfg(feature = "std")]
DonutError::DataParseError(_) => "Data parse error",
DonutError::UnexpectedEof => "Unexpected EOF",
DonutError::UnsupportedSignature(_) => "Unsupported function signature",
DonutError::DotnetError => "Dotnet error",
DonutError::BuildError(_) => "Build error",
DonutError::InvalidHeader => "Invalid header",
DonutError::BadAlignment => "Bad alignment",
DonutError::AllocFailed => "Alloc Failed",
DonutError::Azathoth(_) => "Azathoth Error" ,
#[cfg(feature = "std")]
DonutError::SerializationError(_) => "Serialization error",
DonutError::CryptoError => "Crypto Error" ,
DonutError::InvalidParameterStr(_) => "Invalid parameter",
DonutError::ApiResolutionFailure2(_) => "API resolution failure (2)",
}
}
}
pub type DonutResult<T> = Result<T, DonutError>;
#[cfg(feature = "std")]
impl core::fmt::Display for DonutError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
DonutError::Unknown { e } => write!(f, "Unknown Error: {e}"),
DonutError::Generic { e } => write!(f, "Generic Error: {e}"),
DonutError::Other(e) => write!(f, "Other Error: {e}"),
DonutError::Unsupported => write!(f, "Unsupported Value!"),
DonutError::ParseFailed => write!(f, "Failed to parse!"),
DonutError::InvalidFormat => write!(f, "Invalid format!"),
DonutError::InvalidParameter => write!(f, "Invalid parameter!"),
DonutError::InvalidEngine => write!(f, "Invalid engine!"),
DonutError::InvalidEntropy => write!(f, "Invalid entropy!"),
DonutError::InvalidServer => write!(f, "Invalid server!"),
DonutError::NotFound(e) => write!(f, "Object not found: {e}"),
DonutError::SizeMismatch => write!(f, "Size Mismatch!"),
DonutError::HashMismatch => write!(f, "Hash Mismatch!"),
DonutError::HeaderMismatch => write!(f, "Header Mismatch!"),
DonutError::VersionMismatch => write!(f, "Version Mismatch!"),
DonutError::ApiResolutionFailure => write!(f, "Api Resolution Failure!"),
DonutError::CompressionFailure => write!(f, "Compression Failure!"),
DonutError::Io(e) => write!(f, "Io Error: {e}"),
DonutError::GoblinError(e) => write!(f, "Goblin Error: {e}"),
DonutError::ModuleError => write!(f, "Module Error"),
DonutError::InstanceError => write!(f, "Instance Error"),
DonutError::NetworkFailure => write!(f, "Network Failure"),
DonutError::DecompressionFailure => write!(f, "Decompression Failure"),
DonutError::DataParseError(e) => write!(f, "Data Parse Error: {e}"),
DonutError::UnexpectedEof => write!(f, "Unexpected EOF"),
DonutError::UnsupportedSignature(e) => write!(f, "Unsupported function signature: {e}"),
DonutError::DotnetError => write!(f, "Dotnet Error"),
DonutError::BuildError(e) => write!(f, "Build Error: {e}"),
DonutError::InvalidHeader => write!(f, "Invalid header"),
DonutError::BadAlignment => write!(f, "Bad alignment"),
DonutError::AllocFailed => write!(f, "Alloc Failed"),
DonutError::SerializationError(e) => write!(f, "Serialization Error: {e}"),
DonutError::Azathoth(e) => write!(f, "Azathoth Error: {e}"),
DonutError::CryptoError => write!(f, "Crypto Error"),
DonutError::InvalidParameterStr(e) => write!(f, "Invalid parameter: {e}"),
DonutError::ApiResolutionFailure2(e) => write!(f, "Api Resolution Failure: {e}"),
}
}
}
#[cfg(feature = "std")]
impl Error for DonutError {}
#[cfg(feature = "std")]
impl From<std::io::Error> for DonutError {
fn from(e: std::io::Error) -> Self {
DonutError::Io(e.to_string())
}
}
#[cfg(feature = "std")]
impl From<DonutError> for std::io::Error {
fn from(e: DonutError) -> Self {
std::io::Error::other(e)
}
}
#[cfg(feature = "std")]
impl From<serde::de::value::Error> for DonutError {
fn from(e: serde::de::value::Error) -> Self {
Self::SerializationError(e.to_string())
}
}
#[cfg(feature = "std")]
impl From<goblin::error::Error> for DonutError {
fn from(e: goblin::error::Error) -> Self {
DonutError::GoblinError(e.to_string())
}
}
#[cfg(feature = "std")]
impl From<DataParseError> for DonutError {
fn from(value: DataParseError) -> Self {
Self::DataParseError(value.to_string())
}
}
impl From<AzUtilErrorCode> for DonutError {
fn from(value: AzUtilErrorCode) -> Self {
Self::Azathoth(value.to_string())
}
}
#[inline(always)]
const fn pack16(hi: u16, lo: u16) -> u32 {
((hi as u32) << 16) | lo as u32
}