use core::fmt::Display;
use std::error::Error;
use serde::{Deserialize, Serialize};
use crate::encode::ShouldCompress;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Either<L, R> {
Left(L),
Right(R),
}
impl<L, R> Either<L, R> {
#[inline]
pub fn into_result(self) -> Result<R, L> {
match self {
Either::Left(l) => Err(l),
Either::Right(r) => Ok(r),
}
}
}
impl<L: Display, R: Display> Display for Either<L, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Either::Left(l) => l.fmt(f),
Either::Right(r) => r.fmt(f),
}
}
}
impl<L: Error, R: Error> Error for Either<L, R> {}
impl<L, R> ShouldCompress for Either<L, R>
where
L: ShouldCompress,
R: ShouldCompress,
{
#[inline]
fn should_compress(&self) -> bool {
match self {
Either::Left(l) => l.should_compress(),
Either::Right(r) => r.should_compress(),
}
}
}