use std::fmt::{self, Display};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ChoiceError {
NoAlternatives,
IndexOutOfBounds {
index: usize,
len: usize,
},
EmptyPrimaryIterator,
EmptyChoice,
}
impl ChoiceError {
#[inline]
pub const fn index_out_of_bounds(index: usize, len: usize) -> Self {
ChoiceError::IndexOutOfBounds { index, len }
}
#[inline]
pub const fn is_no_alternatives(&self) -> bool {
matches!(self, ChoiceError::NoAlternatives)
}
#[inline]
pub const fn is_index_out_of_bounds(&self) -> bool {
matches!(self, ChoiceError::IndexOutOfBounds { .. })
}
#[inline]
pub const fn is_empty_primary_iterator(&self) -> bool {
matches!(self, ChoiceError::EmptyPrimaryIterator)
}
#[inline]
pub const fn is_empty_choice(&self) -> bool {
matches!(self, ChoiceError::EmptyChoice)
}
}
impl Display for ChoiceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ChoiceError::NoAlternatives => {
write!(f, "Choice operation failed: no alternatives available")
},
ChoiceError::IndexOutOfBounds { index, len } => {
write!(
f,
"Choice::remove_alternative(): index {} out of bounds for {} alternatives",
index, len
)
},
ChoiceError::EmptyPrimaryIterator => {
write!(
f,
"Choice::flatten(): primary value produced empty iterator"
)
},
ChoiceError::EmptyChoice => {
write!(f, "Choice operation failed: choice is empty")
},
}
}
}
impl std::error::Error for ChoiceError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EitherError {
ExpectedLeft,
ExpectedRight,
}
impl EitherError {
#[inline]
pub const fn is_expected_left(&self) -> bool {
matches!(self, EitherError::ExpectedLeft)
}
#[inline]
pub const fn is_expected_right(&self) -> bool {
matches!(self, EitherError::ExpectedRight)
}
}
impl Display for EitherError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EitherError::ExpectedLeft => {
write!(f, "Either::unwrap_left(): called on Right variant")
},
EitherError::ExpectedRight => {
write!(f, "Either::unwrap_right(): called on Left variant")
},
}
}
}
impl std::error::Error for EitherError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ValidatedError {
ExpectedValid,
ExpectedInvalid,
}
impl ValidatedError {
#[inline]
pub const fn is_expected_valid(&self) -> bool {
matches!(self, ValidatedError::ExpectedValid)
}
#[inline]
pub const fn is_expected_invalid(&self) -> bool {
matches!(self, ValidatedError::ExpectedInvalid)
}
}
impl Display for ValidatedError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ValidatedError::ExpectedValid => {
write!(f, "Validated::unwrap(): called on Invalid variant")
},
ValidatedError::ExpectedInvalid => {
write!(f, "Validated::unwrap_invalid(): called on Valid variant")
},
}
}
}
impl std::error::Error for ValidatedError {}