use std::{borrow::Cow, fmt::Display};
#[repr(u8)]
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MechanicsErrorKind {
Execution = 1,
QueueFull = 2,
QueueTimeout = 3,
RunTimeout = 4,
PoolClosed = 5,
WorkerUnavailable = 6,
Canceled = 7,
Panic = 8,
RuntimePool = 9,
}
impl MechanicsErrorKind {
pub fn as_str(self) -> &'static str {
match self {
Self::Execution => "MechanicsError::Execution",
Self::QueueFull => "MechanicsError::QueueFull",
Self::QueueTimeout => "MechanicsError::QueueTimeout",
Self::RunTimeout => "MechanicsError::RunTimeout",
Self::PoolClosed => "MechanicsError::PoolClosed",
Self::WorkerUnavailable => "MechanicsError::WorkerUnavailable",
Self::Canceled => "MechanicsError::Canceled",
Self::Panic => "MechanicsError::Panic",
Self::RuntimePool => "MechanicsError::RuntimePool",
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum MechanicsError {
Execution(Cow<'static, str>),
QueueFull(Cow<'static, str>),
QueueTimeout(Cow<'static, str>),
RunTimeout(Cow<'static, str>),
PoolClosed(Cow<'static, str>),
WorkerUnavailable(Cow<'static, str>),
Canceled(Cow<'static, str>),
Panic(Cow<'static, str>),
RuntimePool(Cow<'static, str>),
}
impl MechanicsError {
pub fn execution<M: Into<Cow<'static, str>>>(msg: M) -> Self {
Self::Execution(msg.into())
}
pub fn runtime_pool<M: Into<Cow<'static, str>>>(msg: M) -> Self {
Self::RuntimePool(msg.into())
}
pub fn queue_full<M: Into<Cow<'static, str>>>(msg: M) -> Self {
Self::QueueFull(msg.into())
}
pub fn queue_timeout<M: Into<Cow<'static, str>>>(msg: M) -> Self {
Self::QueueTimeout(msg.into())
}
pub fn run_timeout<M: Into<Cow<'static, str>>>(msg: M) -> Self {
Self::RunTimeout(msg.into())
}
pub fn pool_closed<M: Into<Cow<'static, str>>>(msg: M) -> Self {
Self::PoolClosed(msg.into())
}
pub fn worker_unavailable<M: Into<Cow<'static, str>>>(msg: M) -> Self {
Self::WorkerUnavailable(msg.into())
}
pub fn canceled<M: Into<Cow<'static, str>>>(msg: M) -> Self {
Self::Canceled(msg.into())
}
pub fn worker_panic<M: Into<Cow<'static, str>>>(msg: M) -> Self {
Self::Panic(msg.into())
}
pub fn msg(&self) -> &str {
match self {
Self::Execution(msg) => msg.as_ref(),
Self::QueueFull(msg) => msg.as_ref(),
Self::QueueTimeout(msg) => msg.as_ref(),
Self::RunTimeout(msg) => msg.as_ref(),
Self::PoolClosed(msg) => msg.as_ref(),
Self::WorkerUnavailable(msg) => msg.as_ref(),
Self::Canceled(msg) => msg.as_ref(),
Self::Panic(msg) => msg.as_ref(),
Self::RuntimePool(msg) => msg.as_ref(),
}
}
pub fn kind(&self) -> MechanicsErrorKind {
match &self {
Self::Execution(_) => MechanicsErrorKind::Execution,
Self::QueueFull(_) => MechanicsErrorKind::QueueFull,
Self::QueueTimeout(_) => MechanicsErrorKind::QueueTimeout,
Self::RunTimeout(_) => MechanicsErrorKind::RunTimeout,
Self::PoolClosed(_) => MechanicsErrorKind::PoolClosed,
Self::WorkerUnavailable(_) => MechanicsErrorKind::WorkerUnavailable,
Self::Canceled(_) => MechanicsErrorKind::Canceled,
Self::Panic(_) => MechanicsErrorKind::Panic,
Self::RuntimePool(_) => MechanicsErrorKind::RuntimePool,
}
}
}
impl Display for MechanicsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.kind().as_str(), self.msg())
}
}
impl std::error::Error for MechanicsError {}