use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum DecodeError {
#[error("unexpected end of input: needed {needed} more byte(s)")]
UnexpectedEof {
needed: usize,
},
#[error("VarInt/VarLong is too long")]
VarIntTooLong,
#[error("negative length {0}")]
NegativeLength(i64),
#[error("length {len} exceeds limit {limit}")]
LengthTooLarge {
len: usize,
limit: usize,
},
#[error("invalid UTF-8 in string")]
InvalidUtf8,
#[error("invalid NBT tag type {0}")]
InvalidNbtTag(u8),
#[error("NBT nesting deeper than {0}")]
NbtTooDeep(usize),
#[error("NBT list of TAG_End with non-zero length")]
NbtEndList,
#[error("invalid value: {0}")]
Invalid(&'static str),
#[error("{0} trailing byte(s) after packet")]
TrailingBytes(usize),
#[error("unknown packet id 0x{0:02x}")]
UnknownPacketId(i32),
#[error("packet id mismatch: expected 0x{expected:02x}, got 0x{actual:02x}")]
IdMismatch {
expected: i32,
actual: i32,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum EncodeError {
#[error("length {len} does not fit the length prefix (max {max})")]
LengthOverflow {
len: usize,
max: usize,
},
#[error("field `{field}`: expected {expected} element(s), got {actual}")]
CountMismatch {
field: &'static str,
expected: usize,
actual: usize,
},
#[error("field `{0}` does not match its discriminant")]
SwitchMismatch(&'static str),
#[error("field `{0}` is out of range for its encoding")]
OutOfRange(&'static str),
#[error("invalid value: {0}")]
Invalid(&'static str),
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum FrameError {
#[error("bad frame length prefix")]
BadLength,
#[error("frame of {len} bytes exceeds maximum {max}")]
FrameTooLarge {
len: usize,
max: usize,
},
#[error("decompressed size {len} exceeds maximum {max}")]
DecompressedTooLarge {
len: usize,
max: usize,
},
#[error("bad compressed packet: {0}")]
BadCompression(&'static str),
#[error(transparent)]
Decode(#[from] DecodeError),
#[error(transparent)]
Io(#[from] std::io::Error),
}