use fehler::throws;
#[derive(Debug, PartialEq)]
pub enum AssembleFailure<E> {
Incomplete,
Error(E),
}
impl<E> From<E> for AssembleFailure<E> {
#[inline]
fn from(error: E) -> Self {
Self::Error(error)
}
}
pub trait AssembleFrom<P>
where
Self: Sized,
{
type Error;
#[throws(AssembleFailure<Self::Error>)]
fn assemble_from(parts: &mut Vec<P>) -> Self;
}
pub trait AssembleInto<C>
where
Self: Sized,
{
type Error;
#[throws(AssembleFailure<Self::Error>)]
fn assemble_into(parts: &mut Vec<Self>) -> C;
}
impl<P, C> AssembleInto<C> for P
where
C: AssembleFrom<P>,
{
type Error = <C as AssembleFrom<Self>>::Error;
#[inline]
#[throws(AssembleFailure<Self::Error>)]
fn assemble_into(parts: &mut Vec<Self>) -> C {
C::assemble_from(parts)?
}
}
pub trait DisassembleFrom<C>
where
Self: Sized,
{
type Error;
#[throws(Self::Error)]
fn disassemble_from(composite: C) -> Vec<Self>;
}
pub trait DisassembleInto<P> {
type Error;
#[throws(Self::Error)]
fn disassemble_into(self) -> Vec<P>;
}
impl<P, C> DisassembleInto<P> for C
where
P: DisassembleFrom<C>,
{
type Error = <P as DisassembleFrom<Self>>::Error;
#[inline]
#[throws(Self::Error)]
fn disassemble_into(self) -> Vec<P> {
P::disassemble_from(self)?
}
}