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