umsh_core/
error.rs

1use core::fmt;
2
3/// Errors returned while parsing on-wire UMSH structures.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum ParseError {
6    /// The input ended before the expected structure was complete.
7    Truncated,
8    /// The frame-control version bits do not match the supported protocol.
9    InvalidVersion(u8),
10    /// The reserved bit in the frame-control field was non-zero.
11    InvalidFcfReserved,
12    /// Reserved bits in the security-control field were non-zero.
13    InvalidScfReserved,
14    /// The encoded MIC size is not assigned.
15    InvalidMicSize(u8),
16    /// A flood-hop byte could not be interpreted.
17    InvalidFloodHops,
18    /// A CoAP-style option nibble used an invalid extension marker.
19    InvalidOptionNibble,
20    /// Option numbers were not monotonically increasing.
21    OptionOutOfOrder,
22    /// The option stream was structurally malformed.
23    MalformedOption,
24}
25
26/// Errors returned while parsing a textual UMSH address.
27#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28pub enum AddressParseError {
29    /// The length matches neither the 44-character base58 form nor the
30    /// 64-character base16 form.
31    InvalidLength,
32    /// A character was outside the expected alphabet.
33    InvalidCharacter,
34    /// The base58 value does not fit in 32 bytes.
35    Overflow,
36}
37
38/// Errors returned while encoding wire-format values into caller-provided buffers.
39#[derive(Clone, Copy, Debug, PartialEq, Eq)]
40pub enum EncodeError {
41    /// The provided buffer could not hold the encoded output.
42    BufferTooSmall,
43    /// Option numbers were encoded out of order.
44    OptionOutOfOrder,
45    /// A single option value exceeded the codec's supported length.
46    OptionValueTooLarge,
47}
48
49/// Errors returned while assembling a full packet with [`crate::PacketBuilder`].
50#[derive(Clone, Copy, Debug, PartialEq, Eq)]
51pub enum BuildError {
52    /// The destination buffer could not hold the packet.
53    BufferTooSmall,
54    /// A required source address was not supplied.
55    MissingSource,
56    /// A required destination field was not supplied.
57    MissingDestination,
58    /// A required channel identifier was not supplied.
59    MissingChannel,
60    /// A secured packet was missing its frame counter.
61    MissingFrameCounter,
62    /// A builder path that requires payload bytes was finalized without payload.
63    MissingPayload,
64    /// A MAC ACK builder was finalized without an ACK tag.
65    MissingAckTag,
66    /// Options were added in descending order.
67    OptionOutOfOrder,
68    /// Builder output failed structural validation.
69    InvalidPacket,
70}
71
72impl From<EncodeError> for BuildError {
73    fn from(value: EncodeError) -> Self {
74        match value {
75            EncodeError::BufferTooSmall => Self::BufferTooSmall,
76            EncodeError::OptionOutOfOrder => Self::OptionOutOfOrder,
77            EncodeError::OptionValueTooLarge => Self::BufferTooSmall,
78        }
79    }
80}
81
82impl fmt::Display for ParseError {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        write!(f, "{self:?}")
85    }
86}
87
88impl fmt::Display for AddressParseError {
89    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90        write!(f, "{self:?}")
91    }
92}
93
94impl fmt::Display for EncodeError {
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        write!(f, "{self:?}")
97    }
98}
99
100impl fmt::Display for BuildError {
101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102        write!(f, "{self:?}")
103    }
104}
105
106#[cfg(feature = "std")]
107impl std::error::Error for ParseError {}
108
109#[cfg(feature = "std")]
110impl std::error::Error for AddressParseError {}
111
112#[cfg(feature = "std")]
113impl std::error::Error for EncodeError {}
114
115#[cfg(feature = "std")]
116impl std::error::Error for BuildError {}