1use thiserror::Error;
2
3#[derive(Debug, Error)]
5#[non_exhaustive]
6pub enum ProtocolError {
7 #[error("unexpected end of buffer: needed {needed} more bytes")]
9 UnexpectedEof { needed: usize },
10
11 #[error("invalid value: {0}")]
13 InvalidValue(&'static str),
14
15 #[error("invalid UTF-8 in string field")]
17 InvalidUtf8(#[source] std::str::Utf8Error),
18
19 #[error("varint exceeds {max} bytes")]
21 VarintTooLong { max: usize },
22
23 #[error("unsupported API version {version} for api key {api_key}")]
25 UnsupportedVersion { api_key: i16, version: i16 },
26
27 #[error("schema mismatch: {0}")]
29 SchemaMismatch(&'static str),
30}
31
32#[cfg(test)]
33mod tests {
34 use super::*;
35 use assert2::assert;
36
37 #[test]
38 fn display_is_useful() {
39 let e = ProtocolError::UnexpectedEof { needed: 4 };
40 assert!(e.to_string() == "unexpected end of buffer: needed 4 more bytes");
41 }
42}