Skip to main content

bibeam_protocol/
error.rs

1#![forbid(unsafe_code)]
2//! Protocol-layer error type.
3//!
4//! [`ProtocolError`] surfaces the two bad-prefix cases the codec checks
5//! before invoking postcard ([`ProtocolError::BadMagic`] and
6//! [`ProtocolError::BadVersion`]) as first-class variants, alongside
7//! transparent passthroughs for the underlying `postcard::Error` and
8//! [`bibeam_core::Error`].
9//!
10//! Lifting bad-magic / bad-version out of the generic
11//! `postcard::Error::DeserializeBadEncoding` bucket lets callers (the
12//! transport layer, the coordinator, observability code) match on the
13//! root cause without string-sniffing.
14
15use thiserror::Error;
16
17/// Protocol-layer error returned by [`crate::decode`] and by callers
18/// that wrap codec failures.
19#[derive(Debug, Error)]
20pub enum ProtocolError {
21    /// Underlying postcard serialise / deserialise failure.
22    #[error("postcard codec error: {0}")]
23    Codec(#[from] postcard::Error),
24    /// Underlying core-layer error.
25    #[error("core error: {0}")]
26    Core(#[from] bibeam_core::Error),
27    /// First four bytes did not match the [`crate::MAGIC`] constant.
28    #[error("invalid magic bytes (expected BIBM)")]
29    BadMagic,
30    /// Version byte did not match the [`crate::VERSION`] constant.
31    #[error("unsupported wire version: got {got}, expected {expected}")]
32    BadVersion {
33        /// Version byte observed on the wire.
34        got: u8,
35        /// Version byte this implementation speaks.
36        expected: u8,
37    },
38}