bcp_types/error.rs
1use bcp_wire::WireError;
2
3/// Errors that can occur when encoding or decoding typed block bodies.
4///
5/// These are higher-level than [`WireError`] — they deal with semantic
6/// validation of block fields rather than raw byte framing. A `TypeError`
7/// can wrap an underlying `WireError` when the problem originates in
8/// varint or length-prefix parsing within a block body.
9///
10/// # Error hierarchy
11///
12/// ```text
13/// ┌─────────────────────────────────────────────────────┐
14/// │ TypeError (this crate) │
15/// │ ├── wraps WireError for low-level parse failures │
16/// │ ├── UnknownFieldWireType for bad TLV wire types │
17/// │ ├── MissingRequiredField for incomplete blocks │
18/// │ └── InvalidEnumValue for out-of-range enum bytes │
19/// └─────────────────────────────────────────────────────┘
20/// ```
21#[derive(Debug, thiserror::Error)]
22pub enum TypeError {
23 /// A required field was not present in the block body.
24 ///
25 /// Each block type defines which fields are mandatory. If the decoder
26 /// reaches the end of the body without encountering a required field ID,
27 /// this error is returned with the field name for diagnostics.
28 #[error("missing required field: {field}")]
29 MissingRequiredField { field: &'static str },
30
31 /// A field's wire type byte did not match any known [`FieldWireType`].
32 ///
33 /// The TLV encoding uses wire types 0 (varint), 1 (bytes), and 2 (nested).
34 /// Any other value indicates data corruption or a version mismatch.
35 #[error("unknown field wire type: {value}")]
36 UnknownFieldWireType { value: u64 },
37
38 /// An enum field contained a byte value outside its defined range.
39 ///
40 /// For example, a `Role` field with value `0x09` when the max defined
41 /// variant is `0x04`. The enum name and raw value are captured for
42 /// diagnostics.
43 #[error("invalid {enum_name} value: {value:#04X}")]
44 InvalidEnumValue { enum_name: &'static str, value: u8 },
45
46 /// An underlying wire-level error occurred while parsing within a body.
47 ///
48 /// This typically surfaces when a varint inside the block body is
49 /// malformed or the body bytes are truncated mid-field.
50 #[error(transparent)]
51 Wire(#[from] WireError),
52}