use crate::envelope::Envelope;
use crate::message::Msg;
use crate::system::STRING_INTERNER;
use crate::{distributor::Distributor, message::BastionMessage};
use futures::channel::mpsc::TrySendError;
use std::fmt::Debug;
use std::time::Duration;
use thiserror::Error;
#[derive(Debug)]
pub enum ReceiveError {
Timeout(Duration),
Other,
}
#[derive(Error, Debug)]
pub enum SendError {
#[error("couldn't send message. Channel Disconnected.")]
Disconnected(Msg),
#[error("couldn't send message. Channel is Full.")]
Full(Msg),
#[error("couldn't send a message I should have not sent. {0}")]
Other(anyhow::Error),
#[error("No available Distributor matching {0}")]
NoDistributor(String),
#[error("Distributor has 0 Recipients")]
EmptyRecipient,
}
impl From<TrySendError<Envelope>> for SendError {
fn from(tse: TrySendError<Envelope>) -> Self {
let is_disconnected = tse.is_disconnected();
match tse.into_inner().msg {
BastionMessage::Message(msg) => {
if is_disconnected {
Self::Disconnected(msg)
} else {
Self::Full(msg)
}
}
other => Self::Other(anyhow::anyhow!("{:?}", other)),
}
}
}
impl From<Distributor> for SendError {
fn from(distributor: Distributor) -> Self {
Self::NoDistributor(STRING_INTERNER.resolve(distributor.interned()).to_string())
}
}