Skip to main content

kimberlite_wire/
error.rs

1//! Wire protocol error types.
2
3use thiserror::Error;
4
5/// Result type for wire protocol operations.
6pub type WireResult<T> = Result<T, WireError>;
7
8/// Errors that can occur during wire protocol operations.
9#[derive(Debug, Error)]
10pub enum WireError {
11    /// Invalid magic bytes in frame header.
12    #[error("invalid magic: expected 0x56444220, got 0x{0:08x}")]
13    InvalidMagic(u32),
14
15    /// Unsupported protocol version.
16    #[error("unsupported protocol version: {0}")]
17    UnsupportedVersion(u16),
18
19    /// Payload exceeds maximum size.
20    #[error("payload too large: {size} bytes (max {max})")]
21    PayloadTooLarge { size: u32, max: u32 },
22
23    /// Checksum mismatch.
24    #[error("checksum mismatch: expected 0x{expected:08x}, got 0x{actual:08x}")]
25    ChecksumMismatch { expected: u32, actual: u32 },
26
27    /// Incomplete frame (not enough bytes).
28    #[error("incomplete frame: need {needed} bytes, have {available}")]
29    IncompleteFrame { needed: usize, available: usize },
30
31    /// Serialization error.
32    #[error("serialization error: {0}")]
33    Serialization(String),
34
35    /// Deserialization error.
36    #[error("deserialization error: {0}")]
37    Deserialization(String),
38
39    /// I/O error.
40    #[error("i/o error: {0}")]
41    Io(#[from] std::io::Error),
42}
43
44impl From<postcard::Error> for WireError {
45    fn from(e: postcard::Error) -> Self {
46        WireError::Deserialization(e.to_string())
47    }
48}