Skip to main content

kafrust_protocol/
error.rs

1use core::fmt;
2
3pub type Result<T> = core::result::Result<T, Error>;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum Error {
7    UnexpectedEof { needed: usize, remaining: usize },
8    InvalidBool(i8),
9    NegativeLength { kind: &'static str, length: i32 },
10    LengthOverflow(&'static str),
11    InvalidUtf8,
12    VarintTooLong,
13    UnsupportedVersion { kind: &'static str, version: i16 },
14}
15
16impl fmt::Display for Error {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            Self::UnexpectedEof { needed, remaining } => write!(
20                f,
21                "unexpected end of input: needed {needed} bytes, had {remaining}"
22            ),
23            Self::InvalidBool(value) => write!(f, "invalid boolean value {value}"),
24            Self::NegativeLength { kind, length } => {
25                write!(f, "negative {kind} length {length}")
26            }
27            Self::LengthOverflow(kind) => write!(f, "{kind} length does not fit Kafka encoding"),
28            Self::InvalidUtf8 => f.write_str("invalid UTF-8 string"),
29            Self::VarintTooLong => f.write_str("unsigned varint is too long"),
30            Self::UnsupportedVersion { kind, version } => {
31                write!(f, "unsupported {kind} version {version}")
32            }
33        }
34    }
35}
36
37impl std::error::Error for Error {}