use core::error::Error;
use core::fmt::{self, Debug, Display};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PipeError<ProducerError, ConsumerError> {
Producer(ProducerError),
Consumer(ConsumerError),
}
impl<ProducerError, ConsumerError> Display for PipeError<ProducerError, ConsumerError> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PipeError::Producer(_) => {
write!(
f,
"Failed to pipe a producer into a consumer, because the producer emitted an error",
)
}
PipeError::Consumer(_) => {
write!(
f,
"Failed to pipe a producer into a consumer, because the consumer emitted an error",
)
}
}
}
}
impl<ProducerError, ConsumerError> Error for PipeError<ProducerError, ConsumerError>
where
ProducerError: 'static + Error,
ConsumerError: 'static + Error,
{
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
PipeError::Producer(err) => Some(err),
PipeError::Consumer(err) => Some(err),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConsumeAtLeastError<E> {
pub count: usize,
pub reason: E,
}
#[cfg(feature = "std")]
impl<E> Error for ConsumeAtLeastError<E>
where
E: 'static + Error,
{
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.reason)
}
}
impl<E> Display for ConsumeAtLeastError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"The consumer failed to consume sufficiently many items, it only consumed {} items",
self.count
)
}
}
impl<E> ConsumeAtLeastError<E> {
pub fn into_reason(self) -> E {
self.reason
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProduceAtLeastError<F, E> {
pub count: usize,
pub reason: Result<F, E>,
}
#[cfg(feature = "std")]
impl<F, E> Error for ProduceAtLeastError<F, E>
where
F: Debug,
E: 'static + Error,
{
fn source(&self) -> Option<&(dyn Error + 'static)> {
match &self.reason {
Ok(_) => None,
Err(err) => Some(err),
}
}
}
impl<F, E> Display for ProduceAtLeastError<F, E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.reason {
Ok(_) => {
write!(f, "The producer was unable to produce sufficiently many items due to emitting its final value; it stopped after producing {} items", self.count)
}
Err(_) => {
write!(f, "The producer was unable to produce sufficiently many items due to an error; it stopped after producing {} items", self.count)
}
}
}
}
impl<F, E> ProduceAtLeastError<F, E> {
pub fn into_reason(self) -> Result<F, E> {
self.reason
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ExpectedFinalError<Item, Error> {
Item(Item),
Error(Error),
}
#[cfg(feature = "std")]
impl<F, E> Error for ExpectedFinalError<F, E>
where
F: Debug,
E: 'static + Error,
{
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self{
ExpectedFinalError::Item(_) => None,
ExpectedFinalError::Error(err) => Some(err),
}
}
}
impl<F, E> Display for ExpectedFinalError<F, E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ExpectedFinalError::Item(_) => {
write!(f, "A function expected a final value, but got a regular item instead")
}
ExpectedFinalError::Error(_) => {
write!(f, "A function expected a final value, but got an error instead")
}
}
}
}