ansi_color_codec/
error.rs

1use core::fmt::{self, Display, Formatter};
2
3/// Encoding and decoding errors.
4#[derive(Clone, Debug, Eq, PartialEq)]
5pub enum Error {
6    /// The input byte stream terminated prematurely.
7    ByteStreamTerminatedUnexpectedly,
8    /// An invalid color code value has been encountered.
9    InvalidCodeValue,
10    /// The prefix of the color code number was invalid.
11    InvalidNumberPrefix(u8),
12    /// An invalid start byte has been encountered.
13    InvalidStartByte(u8),
14    /// The second color code block is missing.
15    MissingSecondColorCodeBlock,
16    /// No digits for the color code number were found.
17    NoCodeDigitsFound,
18    /// Too many digits for the color code have been encountered.
19    TooManyCodeDigits {
20        /// Number of digits that have been processed.
21        at_least: u8,
22        /// Number of digits that were expected.
23        max: u8,
24    },
25    /// An unexpected byte has been encountered.
26    UnexpectedByte(u8),
27    /// The given color code value was out of bounds.
28    ValueOutOfBounds(u8),
29}
30
31impl Display for Error {
32    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
33        match self {
34            Self::ByteStreamTerminatedUnexpectedly => {
35                write!(f, "byte stream terminated unexpectedly")
36            }
37            Self::InvalidCodeValue => write!(f, "invalid code value"),
38            Self::InvalidNumberPrefix(prefix) => write!(f, "invalid number prefix: {prefix}"),
39            Self::InvalidStartByte(byte) => write!(f, "invalid start byte: {byte:?}"),
40            Self::MissingSecondColorCodeBlock => write!(f, "missing second code block"),
41            Self::NoCodeDigitsFound => write!(f, "no code digits found"),
42            Self::TooManyCodeDigits { at_least, max } => {
43                write!(f, "too many code digits found: {at_least}+ / {max}")
44            }
45            Self::UnexpectedByte(byte) => write!(f, "unexpected byte: {byte:?}"),
46            Self::ValueOutOfBounds(value) => write!(f, "value out of bounds: {value}"),
47        }
48    }
49}
50
51impl core::error::Error for Error {}