Skip to main content

zaun_community_protocol/encoding/
error.rs

1//! Encoding and decoding errors.
2
3/// The input is not a canonical encoding (specification §3.1).
4#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
5pub enum DecodeError {
6    /// A fixed field, length or count needs more bytes than remain.
7    #[error("truncated: need {needed} bytes, {remaining} remain")]
8    Truncated {
9        /// Bytes the field needs.
10        needed: usize,
11        /// Bytes left in the input.
12        remaining: usize,
13    },
14    /// Bytes after the top-level object.
15    #[error("{0} trailing bytes")]
16    TrailingBytes(usize),
17    /// A `bool` byte other than `0x00` or `0x01`.
18    #[error("invalid bool byte {0}")]
19    InvalidBool(u8),
20    /// An `option` tag other than `0x00` or `0x01`.
21    #[error("invalid option tag {0}")]
22    InvalidOptionTag(u8),
23    /// A length or count above the field's limit.
24    #[error("length {len} exceeds the limit {max}")]
25    OverLimit {
26        /// The declared length or count.
27        len: usize,
28        /// The field's limit.
29        max: usize,
30    },
31    /// A `string` that is not valid UTF-8.
32    #[error("invalid UTF-8")]
33    InvalidUtf8,
34    /// An unknown `format` byte.
35    #[error("unknown format byte {0}")]
36    UnknownFormat(u8),
37    /// An unknown `suite` byte (`0x00`, `0x02`–`0xFF`).
38    #[error("unknown suite byte {0}")]
39    UnknownSuite(u8),
40    /// A field breaks a rule of its object (the field name).
41    #[error("invalid field `{0}`")]
42    Invalid(&'static str),
43}
44
45/// A value cannot be encoded canonically: it would not decode.
46#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
47pub enum EncodeError {
48    /// A length or count above the field's limit.
49    #[error("length {len} exceeds the limit {max}")]
50    OverLimit {
51        /// The value's length or count.
52        len: usize,
53        /// The field's limit.
54        max: usize,
55    },
56    /// A field breaks a rule of its object (the field name).
57    #[error("invalid field `{0}`")]
58    Invalid(&'static str),
59}