Skip to main content

bcp_wire/
error.rs

1// TODO
2#[derive(Debug, thiserror::Error)]
3pub enum WireError {
4    /// Varint encoding exceeded 10 bytes without terminating.
5    #[error("varint too long: exceeded 10-byte limit")]
6    VarintTooLong,
7
8    /// Input ended before a complete varint or header could be read.
9    #[error("unexpected end of input at offset {offset}")]
10    UnexpectedEof { offset: usize },
11
12    /// Magic number did not match "BCP\0".
13    #[error("invalid magic number: expected 0x42435000, got {found:#010X}")]
14    InvalidMagic { found: u32 },
15
16    /// Unsupported format version.
17    #[error("unsupported version {major}.{minor}")]
18    UnsupportedVersion { major: u8, minor: u8 },
19
20    /// Reserved field was non-zero.
21    #[error("reserved field at offset {offset} was {value:#04X}, expected 0x00")]
22    ReservedNonZero { offset: usize, value: u8 },
23
24    /// Block type varint decoded to a value that does not fit in a `u8`.
25    #[error("invalid block type: varint value {raw} exceeds u8 range")]
26    InvalidBlockType { raw: u64 },
27
28    /// I/O error during read or write.
29    #[error(transparent)]
30    Io(#[from] std::io::Error),
31}
32
33// NOTE Summary
34// #[derive(thiserror::Error)] — this generates the impl std::error::Error and impl Display for you.
35// Each #[error("...")] attribute becomes the Display output.
36// Without thiserror, you'd be writing ~40 lines of boilerplate trait impls by hand.
37// #[error(transparent)] on the Io variant —
38// this means when someone prints this error,
39// it delegates entirely to the inner std::io::Error's Display.
40// Combined with #[from], it means any function returning Result<T, WireError> can use the ? operator
41// on std::io calls and the conversion happens automatically.
42// The {found:#010X} syntax — that's Rust's format string for hex.
43// # adds the 0x prefix, 010 means pad to 10 characters (including the 0x),
44// X is uppercase hex. So an invalid magic 0x00001234 prints as 0x00001234.
45// Good for debugging binary formats.
46// Why each variant carries context — UnexpectedEof { offset }
47// tells you where in the byte stream things went wrong.
48// This is critical when debugging binary payloads.
49// The offset is the byte position from the start of the input where the read failed.