use derive_more::{From, IsVariant, TryUnwrap, Unwrap};
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, From, IsVariant, Unwrap, TryUnwrap)]
#[unwrap(ref, ref_mut)]
#[try_unwrap(ref, ref_mut)]
#[non_exhaustive]
pub enum Expected<'a, T> {
One(T),
OneOf(&'a [T]),
}
impl<'a, T> Expected<'a, T> {
#[inline]
pub const fn one(expected: T) -> Self {
Self::One(expected)
}
#[inline]
pub const fn one_of(expected: &'a [T]) -> Self {
Self::OneOf(expected)
}
}
impl<T: core::fmt::Display> core::fmt::Display for Expected<'_, T> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::One(expected) => write!(f, "expected '{expected}'"),
Self::OneOf(expected) => {
write!(f, "expected one of: ")?;
for (i, exp) in expected.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "'{}'", exp)?;
}
Ok(())
}
}
}
}