peat_lite/protocol/error.rs
1//! Message encoding/decoding errors.
2
3/// Errors that can occur when encoding or decoding a Peat-Lite message.
4///
5/// Marked `#[non_exhaustive]` so future protocol amendments can add
6/// variants without breaking exhaustive-match consumers.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum MessageError {
10 /// Output buffer is too small for the encoded message.
11 BufferTooSmall,
12 /// Input buffer is shorter than the minimum header size.
13 TooShort,
14 /// Magic bytes do not match.
15 InvalidMagic,
16 /// Protocol version is not supported.
17 UnsupportedVersion,
18 /// Message type byte is not recognised.
19 InvalidMessageType,
20 /// Payload exceeds `MAX_PAYLOAD_SIZE`.
21 PayloadTooLarge,
22 /// A length-prefixed field declared more bytes than remain in the
23 /// input — truncated wire encoding.
24 TruncatedField,
25 /// A length-prefixed string field is not valid UTF-8. peat-lite
26 /// declares collection names + document ids as UTF-8 by spec; non-
27 /// UTF-8 bytes indicate corruption or a non-conforming sender.
28 InvalidUtf8,
29 /// A length-prefixed field exceeded its declared maximum
30 /// (collection over 255 bytes, doc_id or body over 65535 bytes).
31 /// The wire format uses fixed-width length prefixes per field;
32 /// oversized values must be rejected at encode time so receivers
33 /// can trust the fixed widths.
34 FieldTooLarge,
35 /// A `MessageType::Document` envelope's collection name was
36 /// empty (`coll_len = 0`). Empty collection is structurally
37 /// invalid — receivers can't route the document. Distinct from
38 /// [`Self::FieldTooLarge`] (which is for length-cap violations)
39 /// and from [`Self::TruncatedField`] (which is for buffer-shorter-
40 /// than-declared cases). Round-2 of peat-lite#26 introduced this
41 /// to fix an asymmetric-variant trap where encoder and decoder
42 /// reported the same condition with different error names.
43 EmptyCollection,
44 /// A reserved or not-yet-implemented flag bit was set on a
45 /// `MessageType::Document` envelope. Today's encoder rejects any
46 /// flag with reserved bits 2–7 set, and additionally bit 1
47 /// (`DOC_FLAG_ENCRYPTED`) which is reserved for a future
48 /// per-document encryption layer not yet wired through. Lets us
49 /// add features without older encoders silently shipping invalid
50 /// frames.
51 InvalidFlags,
52 /// `MessageType::Document` envelope had `DOC_FLAG_TOMBSTONE` set
53 /// AND a non-empty body. Per the envelope spec, a tombstone is a
54 /// deletion sentinel; carrying body bytes alongside it is a
55 /// publisher contract violation that downstream consumers might
56 /// process write-then-delete races against. Rejected at encode
57 /// time so the bug doesn't reach the wire.
58 TombstoneWithBody,
59}