Skip to main content

le_stream/
error.rs

1use core::fmt::{Debug, Display, Formatter};
2
3/// Result type with [`Error`] as error variant.
4pub type Result<T> = core::result::Result<T, Error<T>>;
5
6/// Error type for byte stream operations.
7#[derive(Clone, Debug, Eq, Hash, PartialEq)]
8pub enum Error<T> {
9    /// The byte stream was not exhausted.
10    StreamNotExhausted {
11        /// The instance that was being parsed.
12        instance: T,
13        /// The next byte in the stream.
14        next_byte: u8,
15    },
16    /// The byte stream ended prematurely.
17    UnexpectedEndOfStream,
18}
19
20impl<T> Display for Error<T> {
21    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
22        match self {
23            Self::StreamNotExhausted { next_byte, .. } => {
24                write!(f, "byte stream not exhausted: [{next_byte:#04X?}, ..]")
25            }
26            Self::UnexpectedEndOfStream => write!(f, "unexpected end of stream"),
27        }
28    }
29}
30
31impl<T> core::error::Error for Error<T> where T: Debug {}