use std::fmt::Display;
#[cfg(feature = "remote")]
use serde::{Deserialize, Serialize};
#[derive(Debug)]
#[cfg_attr(feature = "remote", derive(Serialize, Deserialize))]
pub enum Message<Input> {
Task(Input),
TaskMut(Input),
Ping,
Stop,
}
#[derive(Debug)]
#[cfg_attr(feature = "remote", derive(Serialize, Deserialize))]
pub enum Reply<Output> {
Accepted,
Task(Output),
}
pub type MsgResult<Output, Error> = Result<Reply<Output>, MsgError<Error>>;
#[allow(missing_docs)]
#[derive(Debug)]
#[cfg_attr(feature = "remote", derive(Serialize, Deserialize))]
pub enum MsgError<Error> {
Send(String),
Recv(String),
Task(Error),
NotAllowed,
}
impl<E> Display for MsgError<E>
where
E: Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MsgError::Send(ctx) => write!(f, "failed to send message: {ctx}"),
MsgError::Recv(ctx) => write!(f, "failed to receive message: {ctx}"),
MsgError::Task(err) => write!(f, "task failed: {err}"),
MsgError::NotAllowed => write!(f, "message not allowed"),
}
}
}
impl<E> std::error::Error for MsgError<E> where E: Display + core::fmt::Debug {}