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 {
8        needed: usize,
9        remaining: usize,
10    },
11    InvalidBool(i8),
12    InvalidNullableStruct(i8),
13    NegativeLength {
14        kind: &'static str,
15        length: i32,
16    },
17    LengthOverflow(&'static str),
18    LimitExceeded {
19        kind: &'static str,
20        actual: usize,
21        max: usize,
22    },
23    InvalidUtf8,
24    VarintTooLong,
25    UnsupportedVersion {
26        kind: &'static str,
27        version: i16,
28    },
29    UnsupportedCompression {
30        codec: &'static str,
31    },
32    Compression {
33        codec: &'static str,
34        reason: String,
35    },
36}
37
38impl fmt::Display for Error {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Self::UnexpectedEof { needed, remaining } => write!(
42                f,
43                "unexpected end of input: needed {needed} bytes, had {remaining}"
44            ),
45            Self::InvalidBool(value) => write!(f, "invalid boolean value {value}"),
46            Self::InvalidNullableStruct(value) => {
47                write!(f, "invalid nullable struct marker {value}")
48            }
49            Self::NegativeLength { kind, length } => {
50                write!(f, "negative {kind} length {length}")
51            }
52            Self::LengthOverflow(kind) => write!(f, "{kind} length does not fit Kafka encoding"),
53            Self::LimitExceeded { kind, actual, max } => {
54                write!(f, "{kind} limit exceeded: {actual} is greater than {max}")
55            }
56            Self::InvalidUtf8 => f.write_str("invalid UTF-8 string"),
57            Self::VarintTooLong => f.write_str("unsigned varint is too long"),
58            Self::UnsupportedVersion { kind, version } => {
59                write!(f, "unsupported {kind} version {version}")
60            }
61            Self::UnsupportedCompression { codec } => {
62                write!(f, "unsupported record batch compression codec {codec}")
63            }
64            Self::Compression { codec, reason } => {
65                write!(f, "{codec} record batch compression error: {reason}")
66            }
67        }
68    }
69}
70
71impl std::error::Error for Error {}