2024-02-20 14:33:21 +01:00
|
|
|
use core::fmt::{Display, Formatter};
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
use std::error::Error;
|
2024-02-26 11:41:42 +01:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
use std::sync::mpsc;
|
2024-02-20 14:33:21 +01:00
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
use crate::ComponentId;
|
|
|
|
|
|
|
|
/// Generic channel ID type.
|
|
|
|
pub type ChannelId = u32;
|
|
|
|
|
2024-02-20 14:33:21 +01:00
|
|
|
/// Generic error type for sending something via a message queue.
|
2024-04-04 15:18:53 +02:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2024-02-20 14:33:21 +01:00
|
|
|
pub enum GenericSendError {
|
|
|
|
RxDisconnected,
|
|
|
|
QueueFull(Option<u32>),
|
2024-04-04 15:18:53 +02:00
|
|
|
TargetDoesNotExist(ComponentId),
|
2024-02-20 14:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for GenericSendError {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
|
|
|
match self {
|
|
|
|
GenericSendError::RxDisconnected => {
|
|
|
|
write!(f, "rx side has disconnected")
|
|
|
|
}
|
|
|
|
GenericSendError::QueueFull(max_cap) => {
|
|
|
|
write!(f, "queue with max capacity of {max_cap:?} is full")
|
|
|
|
}
|
2024-04-04 15:18:53 +02:00
|
|
|
GenericSendError::TargetDoesNotExist(target) => {
|
|
|
|
write!(f, "target queue with ID {target} does not exist")
|
|
|
|
}
|
2024-02-20 14:33:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl Error for GenericSendError {}
|
|
|
|
|
|
|
|
/// Generic error type for sending something via a message queue.
|
2024-04-04 15:18:53 +02:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum GenericReceiveError {
|
2024-02-20 14:33:21 +01:00
|
|
|
Empty,
|
2024-04-04 15:18:53 +02:00
|
|
|
TxDisconnected(Option<ComponentId>),
|
2024-02-20 14:33:21 +01:00
|
|
|
}
|
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
impl Display for GenericReceiveError {
|
2024-02-20 14:33:21 +01:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
|
|
|
match self {
|
2024-04-04 15:18:53 +02:00
|
|
|
Self::TxDisconnected(channel_id) => {
|
|
|
|
write!(f, "tx side with id {channel_id:?} has disconnected")
|
2024-02-20 14:33:21 +01:00
|
|
|
}
|
|
|
|
Self::Empty => {
|
|
|
|
write!(f, "nothing to receive")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
2024-04-04 15:18:53 +02:00
|
|
|
impl Error for GenericReceiveError {}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum GenericTargetedMessagingError {
|
|
|
|
Send(GenericSendError),
|
|
|
|
Receive(GenericReceiveError),
|
|
|
|
}
|
|
|
|
impl From<GenericSendError> for GenericTargetedMessagingError {
|
|
|
|
fn from(value: GenericSendError) -> Self {
|
|
|
|
Self::Send(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<GenericReceiveError> for GenericTargetedMessagingError {
|
|
|
|
fn from(value: GenericReceiveError) -> Self {
|
|
|
|
Self::Receive(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for GenericTargetedMessagingError {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Send(err) => write!(f, "generic targeted messaging error: {}", err),
|
|
|
|
Self::Receive(err) => write!(f, "generic targeted messaging error: {}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl Error for GenericTargetedMessagingError {
|
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
|
|
match self {
|
|
|
|
GenericTargetedMessagingError::Send(send) => Some(send),
|
|
|
|
GenericTargetedMessagingError::Receive(receive) => Some(receive),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-26 11:41:42 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl<T> From<mpsc::SendError<T>> for GenericSendError {
|
|
|
|
fn from(_: mpsc::SendError<T>) -> Self {
|
|
|
|
GenericSendError::RxDisconnected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl<T> From<mpsc::TrySendError<T>> for GenericSendError {
|
|
|
|
fn from(err: mpsc::TrySendError<T>) -> Self {
|
|
|
|
match err {
|
|
|
|
mpsc::TrySendError::Full(_) => GenericSendError::QueueFull(None),
|
|
|
|
mpsc::TrySendError::Disconnected(_) => GenericSendError::RxDisconnected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "crossbeam")]
|
|
|
|
impl<T> From<crossbeam_channel::SendError<T>> for GenericSendError {
|
|
|
|
fn from(_: crossbeam_channel::SendError<T>) -> Self {
|
|
|
|
GenericSendError::RxDisconnected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "crossbeam")]
|
|
|
|
impl<T> From<crossbeam_channel::TrySendError<T>> for GenericSendError {
|
|
|
|
fn from(err: crossbeam_channel::TrySendError<T>) -> Self {
|
|
|
|
match err {
|
|
|
|
crossbeam_channel::TrySendError::Full(_) => GenericSendError::QueueFull(None),
|
|
|
|
crossbeam_channel::TrySendError::Disconnected(_) => GenericSendError::RxDisconnected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|