character_stream/
error.rs

1use std::io;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5/// Type to represent stream errors.
6pub enum CharacterError {
7    #[error("Failed to read bytes from the stream.")]
8    NoBytesRead,
9    #[error("An IO error occurred on bytes {:?}: {}", .bytes, .error)]
10    IoError { bytes: Vec<u8>, error: io::Error },
11
12    #[error("An error occurred on bytes {:?}: {}", .bytes, .error)]
13    Other {
14        bytes: Vec<u8>,
15        error: anyhow::Error,
16    },
17}
18
19impl CharacterError {
20    pub fn bytes(&self) -> Option<&[u8]> {
21        match self {
22            CharacterError::NoBytesRead => None,
23            CharacterError::Other { bytes, error: _ }
24            | CharacterError::IoError { bytes, error: _ } => Some(&bytes),
25        }
26    }
27}