use std::fmt; pub type Result = std::result::Result; #[derive(Debug, thiserror::Error)] pub enum Error { #[error("unexpected end of stream: wanted {wanted} byte(s) at offset {offset}, {available} available")] Eof { offset: usize, wanted: usize, available: usize, }, #[error("string length {length} exceeds the {limit} byte limit")] StringTooLong { length: usize, limit: usize }, #[error("negative length {0}")] NegativeLength(i32), #[error("invalid utf-8 payload: {0}")] Utf8(#[from] std::str::Utf8Error), #[error("payload of {actual} byte(s) exceeds the {limit} byte frame limit")] FrameTooLarge { actual: usize, limit: usize }, #[error("collection of {actual} item(s) exceeds the {limit} item limit")] CollectionTooLarge { actual: usize, limit: usize }, #[error("expected exactly {expected} item(s), got {actual}")] ArityMismatch { expected: usize, actual: usize }, #[error("unknown message type {0}")] UnknownMessageType(u16), #[error("unknown command type {0}")] UnknownCommandType(i32), #[error("trailing {0} unread byte(s)")] TrailingBytes(usize), #[error("{0}")] Unsupported(&'static str), #[error("io: {0}")] Io(#[from] std::io::Error), } impl Error { pub fn eof(offset: usize, wanted: usize, available: usize) -> Self { Error::Eof { offset, wanted, available, } } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct DecodeContext { pub message_type: u16, pub offset: usize, } impl fmt::Display for DecodeContext { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "message {} at offset {}", self.message_type, self.offset) } }