scroll.server/crates/titan/src/error.rs
WiseDev d25a6de423 move all crates into crates/, glob members
pure move, no code touched. readme and protocol.md paths fixed up.
2026-08-23 08:15:45 +03:00

50 lines
1.7 KiB
Rust

use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[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("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)
}
}