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