use core::{error::Error, fmt::Display};
use crate::{DecodeBuffer, EncodeBuffer};
#[derive(Debug)]
pub enum DecodeError {
Invalid,
Incomplete
}
impl Display for DecodeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Invalid => write!(f, "The data being decoded is not valid"),
Self::Incomplete => write!(f, "Not enough data to be able to decode"),
}
}
}
impl Error for DecodeError {}
impl From<()> for DecodeError {
fn from(_: ()) -> Self {
DecodeError::Invalid
}
}
#[derive(Debug)]
pub enum EncodeError {
Overrun,
Encoding
}
impl Display for EncodeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Encoding => write!(f, "Could not encode. Data was not valid"),
Self::Overrun => write!(f, "Ran out of space in the buffer when trying to encode"),
}
}
}
impl Error for EncodeError {}
pub trait Codec<Context>
where Self: Sized {
fn encode(self, buf: &mut impl EncodeBuffer, ctx: Context) -> Result<(), EncodeError>;
fn decode(buf: &mut impl DecodeBuffer, ctx: Context) -> Result<Self, DecodeError>;
}