collab-common 0.0.7

Code shared by collab's client and server
Documentation
use core::fmt::Display;
use std::error::Error;

use serde::{Deserialize, Serialize};

use crate::encode::ShouldCompress;

/// TODO: docs
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Either<L, R> {
    /// TODO: docs
    Left(L),

    /// TODO: docs
    Right(R),
}

impl<L, R> Either<L, R> {
    /// Converts the `Either` into a `Result`.
    #[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(),
        }
    }
}