use alloc::string::String;
use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum MythicError {
Serialize = 1,
Deserialize = 2,
Base64 = 3,
Utf8 = 4,
InvalidPacket = 5,
InvalidUuid = 6,
UuidMismatch = 7,
Crypto = 8,
Timeout = 9,
ConnectionFailed = 10,
DnsFailed = 11,
TlsFailed = 12,
HttpStatus(u16) = 13,
ServerError(u16) = 14,
AuthFailed = 15,
ServerRejected = 16,
NotCheckedIn = 17,
PayloadTooLarge = 18,
KeyExchangeFailed = 19,
RateLimited = 20,
CommandNotFound = 21,
InvalidTaskData = 22,
TaskTimeout = 23,
ResourceExhausted = 24,
PermissionDenied = 25,
ProcessFailed = 26,
IoFailed = 27,
Transport(String) = 28,
Protocol(String) = 29,
Task(String) = 30,
Runtime(String) = 31,
}
impl MythicError {
pub const fn code(&self) -> u8 {
match self {
Self::Serialize => 1,
Self::Deserialize => 2,
Self::Base64 => 3,
Self::Utf8 => 4,
Self::InvalidPacket => 5,
Self::InvalidUuid => 6,
Self::UuidMismatch => 7,
Self::Crypto => 8,
Self::Timeout => 9,
Self::ConnectionFailed => 10,
Self::DnsFailed => 11,
Self::TlsFailed => 12,
Self::HttpStatus(_) => 13,
Self::ServerError(_) => 14,
Self::AuthFailed => 15,
Self::ServerRejected => 16,
Self::NotCheckedIn => 17,
Self::PayloadTooLarge => 18,
Self::KeyExchangeFailed => 19,
Self::RateLimited => 20,
Self::CommandNotFound => 21,
Self::InvalidTaskData => 22,
Self::TaskTimeout => 23,
Self::ResourceExhausted => 24,
Self::PermissionDenied => 25,
Self::ProcessFailed => 26,
Self::IoFailed => 27,
Self::Transport(_) => 28,
Self::Protocol(_) => 29,
Self::Task(_) => 30,
Self::Runtime(_) => 31,
}
}
pub fn transport<E: fmt::Display>(e: E) -> Self {
Self::Transport(alloc::format!("{e}"))
}
pub fn protocol<E: fmt::Display>(e: E) -> Self {
Self::Protocol(alloc::format!("{e}"))
}
pub fn task<E: fmt::Display>(e: E) -> Self {
Self::Task(alloc::format!("{e}"))
}
pub fn runtime<E: fmt::Display>(e: E) -> Self {
Self::Runtime(alloc::format!("{e}"))
}
}
impl fmt::Display for MythicError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.code())
}
}
pub type MythicResult<T> = Result<T, MythicError>;