use concurrent_queue::PushError;
use std::any::Any;
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
#[error("Process has been halted")]
pub struct HaltedError;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
pub enum RecvError {
#[error("Couldn't receive because the process has been halted")]
Halted,
#[error("Couldn't receive becuase the channel is closed and empty")]
ClosedAndEmpty,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
pub enum TryRecvError {
#[error("Couldn't receive because the process has been halted")]
Halted,
#[error("Couldn't receive because the channel is empty")]
Empty,
#[error("Couldn't receive becuase the channel is closed and empty")]
ClosedAndEmpty,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
pub enum TrySendError<M> {
#[error("Couldn't send message because Channel is closed")]
Closed(M),
#[error("Couldn't send message because Channel is full")]
Full(M),
}
impl<M> From<PushError<M>> for TrySendError<M> {
fn from(e: PushError<M>) -> Self {
match e {
PushError::Full(msg) => Self::Full(msg),
PushError::Closed(msg) => Self::Closed(msg),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
pub struct SendError<M>(pub M);
#[derive(Clone, PartialEq, Eq, Hash, Error)]
pub enum TrySpawnError<T> {
#[error("Couldn't spawn process because the actor has exited")]
Exited(T),
#[error("Couldn't spawn process because the given inbox-type is incorrect")]
IncorrectType(T),
}
impl<T> std::fmt::Debug for TrySpawnError<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Exited(_) => f.debug_tuple("Exited").finish(),
Self::IncorrectType(_) => f.debug_tuple("IncorrectType").finish(),
}
}
}
#[derive(Clone, PartialEq, Eq, Hash, Error)]
#[error("Couldn't spawn process because the channel has exited")]
pub struct SpawnError<T>(pub T);
impl<T> std::fmt::Debug for SpawnError<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("SpawnError").finish()
}
}
#[derive(Debug, Error)]
pub enum ExitError {
#[error("Process has exited because of a panic")]
Panic(Box<dyn Any + Send>),
#[error("Process has exited because it was aborted")]
Abort,
}
impl ExitError {
pub fn is_panic(&self) -> bool {
match self {
ExitError::Panic(_) => true,
ExitError::Abort => false,
}
}
pub fn is_abort(&self) -> bool {
match self {
ExitError::Panic(_) => false,
ExitError::Abort => true,
}
}
}
impl From<tokio::task::JoinError> for ExitError {
fn from(e: tokio::task::JoinError) -> Self {
match e.try_into_panic() {
Ok(panic) => ExitError::Panic(panic),
Err(_) => ExitError::Abort,
}
}
}