1use core::{error::Error, fmt::Display};
2
3use crate::{DecodeBuffer, EncodeBuffer};
4
5
6#[derive(Debug)]
7pub enum DecodeError {
8 Invalid,
9 Incomplete
10}
11
12impl Display for DecodeError {
13 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
14 match self {
15 Self::Invalid => write!(f, "The data being decoded is not valid"),
16 Self::Incomplete => write!(f, "Not enough data to be able to decode"),
17 }
18 }
19}
20
21impl Error for DecodeError {}
22
23impl From<()> for DecodeError {
24 fn from(_: ()) -> Self {
25 DecodeError::Invalid
26 }
27}
28
29#[derive(Debug)]
30pub enum EncodeError {
31 Overrun,
32 Encoding
33}
34
35impl Display for EncodeError {
36 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
37 match self {
38 Self::Encoding => write!(f, "Could not encode. Data was not valid"),
39 Self::Overrun => write!(f, "Ran out of space in the buffer when trying to encode"),
40 }
41 }
42}
43
44impl Error for EncodeError {}
45
46pub trait Codec<Context>
47where Self: Sized {
48 fn encode(self, buf: &mut impl EncodeBuffer, ctx: Context) -> Result<(), EncodeError>;
49 fn decode(buf: &mut impl DecodeBuffer, ctx: Context) -> Result<Self, DecodeError>;
50}