1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::io;

use bytes::BytesMut;
use thiserror::Error;

use super::optneg::CompatibilityError;

/// Encapsulating error for the different de-/encoding problems
#[derive(Debug, Error)]
pub enum ProtocolError {
    /// Data that could not be interpreted
    #[error(transparent)]
    InvalidData(#[from] InvalidData),
    /// Clearly not enough data was present
    #[error(transparent)]
    NotEnoughData(#[from] NotEnoughData),
    /// If we have a protocol compatibility issue
    #[error(transparent)]
    CompatibilityError(#[from] CompatibilityError),
    /// To much data was received to make sense
    #[error("Received a packet too large to decode (len {0})")]
    TooMuchData(usize),
    /// An io error from the underlying codec implementation
    #[error(transparent)]
    CodecError(#[from] io::Error),
}

/// Error when receiving bogus data from the other end
#[derive(Debug, Error)]
#[error("{msg}")]
pub struct InvalidData {
    /// A human readable message
    pub msg: &'static str,
    /// The data that was invalid
    pub offending_bytes: BytesMut,
}

impl InvalidData {
    /// Create a new `InvalidData` error
    #[must_use]
    pub fn new(msg: &'static str, offending_bytes: BytesMut) -> Self {
        Self {
            msg,
            offending_bytes,
        }
    }
}

pub const STAGE_DECODING: &str = "decoding";
// pub const STAGE_ENCODING: &str = "encoding";

/// Raised when definitely more data is necessary
#[derive(Debug, Error)]
#[error("{stage} {item}: expected '{expected}' bytes but got only '{got}': {msg}")]
pub struct NotEnoughData {
    /// The stage at which we are missing data
    pub stage: &'static str,
    /// The item that is missing data to wrok
    pub item: &'static str,
    /// Human readable message
    pub msg: &'static str,
    /// How many bytes where expected
    pub expected: usize,
    /// How many bytes where available
    pub got: usize,
    /// The problematic bytes
    pub buffer: BytesMut,
}

impl NotEnoughData {
    /// Create a new `NotEnoughData` error
    #[must_use]
    pub fn new(
        stage: &'static str,
        item: &'static str,
        msg: &'static str,
        expected: usize,
        got: usize,
        buffer: BytesMut,
    ) -> Self {
        Self {
            stage,
            item,
            msg,
            expected,
            got,
            buffer,
        }
    }
}