1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use std::fmt::{Display, Formatter};

/// Result type with [`Error`] as error variant.
pub type Result<T> = std::result::Result<T, Error>;

/// Error type for byte stream operations.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Error {
    /// The byte stream was not exhausted.
    StreamNotExhausted(u8),
    /// The byte stream ended prematurely.
    UnexpectedEndOfStream,
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::StreamNotExhausted(next_byte) => {
                write!(f, "byte stream not exhausted: [{next_byte:#04X?}, ..]")
            }
            Self::UnexpectedEndOfStream => write!(f, "unexpected end of stream"),
        }
    }
}

impl std::error::Error for Error {}