use std::{collections::BTreeMap, fmt::Debug};
use derive_more::{Display, Error, IsVariant};
#[derive(Clone, Error)]
#[non_exhaustive]
pub struct StartError {
pub errors: Vec<StartGroupError>,
}
impl StartError {
pub(crate) fn single(group: String, reason: String) -> Self {
Self {
errors: vec![StartGroupError { group, reason }],
}
}
pub(crate) fn multiple(errors: Vec<StartGroupError>) -> Self {
Self { errors }
}
}
fn group_errors(errors: Vec<StartGroupError>) -> BTreeMap<String, Vec<String>> {
let mut group_errors = BTreeMap::<String, Vec<String>>::new();
for error in errors {
group_errors
.entry(error.group)
.or_default()
.push(error.reason);
}
for errors in group_errors.values_mut() {
errors.sort();
}
group_errors
}
impl Debug for StartError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if f.alternate() {
let mut s = f.debug_struct("StartError");
s.field("errors", &self.errors);
s.finish()
} else {
write!(f, "failed to start\n\n")?;
for (group, errors) in group_errors(self.errors.clone()) {
writeln!(f, "{group}:")?;
for error in errors {
writeln!(f, "- {error}")?;
}
writeln!(f)?;
}
Ok(())
}
}
}
impl std::fmt::Display for StartError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "failed to start: ")?;
let mut i = 1;
for (group, errors) in group_errors(self.errors.clone()) {
for error in errors {
write!(f, "{i}. {error} ({group}); ")?;
i += 1;
}
}
Ok(())
}
}
#[derive(Clone, Debug, Display, Error)]
#[non_exhaustive]
#[display("error from group {group}: {reason}")]
pub struct StartGroupError {
pub group: String,
pub reason: String,
}
#[derive(Clone, Debug, Display, Error)]
#[display("mailbox closed")]
pub struct SendError<T>(#[error(not(source))] pub T);
impl<T> SendError<T> {
#[inline]
pub fn into_inner(self) -> T {
self.0
}
#[inline]
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> SendError<U> {
SendError(f(self.0))
}
}
#[derive(Clone, Debug, Display, Error)]
pub enum TrySendError<T> {
#[display("mailbox full")]
Full(#[error(not(source))] T),
#[display("mailbox closed")]
Closed(#[error(not(source))] T),
}
impl<T> TrySendError<T> {
#[inline]
pub fn into_inner(self) -> T {
match self {
Self::Closed(inner) => inner,
Self::Full(inner) => inner,
}
}
#[inline]
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> TrySendError<U> {
match self {
Self::Full(inner) => TrySendError::Full(f(inner)),
Self::Closed(inner) => TrySendError::Closed(f(inner)),
}
}
#[inline]
pub fn is_full(&self) -> bool {
matches!(self, Self::Full(_))
}
#[inline]
pub fn is_closed(&self) -> bool {
matches!(self, Self::Closed(_))
}
}
impl<T> From<SendError<T>> for TrySendError<T> {
#[inline]
fn from(err: SendError<T>) -> Self {
TrySendError::Closed(err.0)
}
}
#[derive(Debug, Clone, IsVariant, Display, Error)]
pub enum RequestError {
#[display("request failed")]
Failed,
#[display("request ignored")]
Ignored,
}
#[derive(Debug, Clone, Display, Error)]
pub enum TryRecvError {
#[display("mailbox empty")]
Empty,
#[display("mailbox closed")]
Closed,
}
impl TryRecvError {
#[inline]
pub fn is_empty(&self) -> bool {
matches!(self, Self::Empty)
}
#[inline]
pub fn is_closed(&self) -> bool {
matches!(self, Self::Closed)
}
}