use std::io;
use bytes::BytesMut;
use thiserror::Error;
use super::optneg::CompatibilityError;
#[derive(Debug, Error)]
pub enum ProtocolError {
#[error(transparent)]
InvalidData(#[from] InvalidData),
#[error(transparent)]
NotEnoughData(#[from] NotEnoughData),
#[error(transparent)]
CompatibilityError(#[from] CompatibilityError),
#[error("Received a packet too large to decode (len {0})")]
TooMuchData(usize),
#[error(transparent)]
CodecError(#[from] io::Error),
}
#[derive(Debug, Error)]
#[error("{msg}")]
pub struct InvalidData {
pub msg: &'static str,
pub offending_bytes: BytesMut,
}
impl InvalidData {
#[must_use]
pub fn new(msg: &'static str, offending_bytes: BytesMut) -> Self {
Self {
msg,
offending_bytes,
}
}
}
pub const STAGE_DECODING: &str = "decoding";
#[derive(Debug, Error)]
#[error("{stage} {item}: expected '{expected}' bytes but got only '{got}': {msg}")]
pub struct NotEnoughData {
pub stage: &'static str,
pub item: &'static str,
pub msg: &'static str,
pub expected: usize,
pub got: usize,
pub buffer: BytesMut,
}
impl NotEnoughData {
#[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,
}
}
}