1use std::io;
2
3use bytes::BytesMut;
4use thiserror::Error;
5
6use super::optneg::CompatibilityError;
7
8#[derive(Debug, Error)]
10pub enum ProtocolError {
11 #[error(transparent)]
13 InvalidData(#[from] InvalidData),
14 #[error(transparent)]
16 NotEnoughData(#[from] NotEnoughData),
17 #[error(transparent)]
19 CompatibilityError(#[from] CompatibilityError),
20 #[error("Received a packet too large to decode (len {0})")]
22 TooMuchData(usize),
23 #[error(transparent)]
25 CodecError(#[from] io::Error),
26}
27
28#[derive(Debug, Error)]
30#[error("{msg}")]
31pub struct InvalidData {
32 pub msg: &'static str,
34 pub offending_bytes: BytesMut,
36}
37
38impl InvalidData {
39 #[must_use]
41 pub fn new(msg: &'static str, offending_bytes: BytesMut) -> Self {
42 Self {
43 msg,
44 offending_bytes,
45 }
46 }
47}
48
49pub const STAGE_DECODING: &str = "decoding";
50#[derive(Debug, Error)]
54#[error("{stage} {item}: expected '{expected}' bytes but got only '{got}': {msg}")]
55pub struct NotEnoughData {
56 pub stage: &'static str,
58 pub item: &'static str,
60 pub msg: &'static str,
62 pub expected: usize,
64 pub got: usize,
66 pub buffer: BytesMut,
68}
69
70impl NotEnoughData {
71 #[must_use]
73 pub fn new(
74 stage: &'static str,
75 item: &'static str,
76 msg: &'static str,
77 expected: usize,
78 got: usize,
79 buffer: BytesMut,
80 ) -> Self {
81 Self {
82 stage,
83 item,
84 msg,
85 expected,
86 got,
87 buffer,
88 }
89 }
90}