use std::{
error::Error,
fmt::{self, Display},
};
#[derive(Debug)]
pub enum PacketBuildError {
BodyTooBig(usize),
}
impl Error for PacketBuildError {}
impl Display for PacketBuildError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let error_msg = match self {
Self::BodyTooBig(size) => format!(
"Body size '{}' bytes exceeds the maximum of 4086 bytes.",
size
),
};
write!(f, "{}", error_msg)
}
}
#[derive(Debug)]
pub enum PacketDeserialiseError {
InsuficientSize(i32),
WrongSize { expected: i32, got: i32 },
}
impl Error for PacketDeserialiseError {}
impl Display for PacketDeserialiseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let error_msg = match self {
Self::InsuficientSize(size) => format!(
"Insuficient size '{}' bytes to build RCON Packet, that has a minimum size of 14 bytes.",
size
),
Self::WrongSize { expected, got } => format!(
"Packet has a size attribute of '{}' bytes. However it actually has '{}' bytes.",
expected, got),
};
write!(f, "{}", error_msg)
}
}
#[derive(Debug)]
pub struct PacketTypeError(pub i32);
impl Error for PacketTypeError {}
impl Display for PacketTypeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
let Self(value) = self;
write!(f, "'{value}' represents an invalid packet type")
}
}