byte_transcoder/
reader_error.rs

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
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::fmt::{Debug, Formatter};
use std::{error, fmt};

pub type ByteReaderResult<T> = Result<T, ByteReaderError>;

#[expect(clippy::module_name_repetitions)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ByteReaderError {
    NotEnoughBytes {
        index_offset: usize,
        buffer_length: usize,
    },
    SliceConversionFailure,
    InvalidUtf8,
}

impl error::Error for ByteReaderError {}

impl fmt::Display for ByteReaderError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            ByteReaderError::NotEnoughBytes {
                index_offset,
                buffer_length,
            } => write!(
                f,
                "Attempted to read past the end of the buffer. Index Offset: '{index_offset}', Buffer Length: '{buffer_length}'.",
            ),
            ByteReaderError::SliceConversionFailure => write!(
                f,
                "Failed to convert slice to a fixed-size array."
            ),
            ByteReaderError::InvalidUtf8 => write!(
                f,
                "Attempted to read invalid UTF-8 bytes."
            ),
        }
    }
}