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 /// Reserved bits in the security-control field were non-zero.
11 InvalidScfReserved,
12 /// The encoded MIC size is not assigned.
13 InvalidMicSize(u8),
14 /// A flood-hop byte could not be interpreted.
15 InvalidFloodHops,
16 /// A CoAP-style option nibble used an invalid extension marker.
17 InvalidOptionNibble,
18 /// An option block was missing its terminating marker.
19 MissingOptionTerminator,
20 /// Option numbers were not monotonically increasing.
21 OptionOutOfOrder,
22 /// The option stream was structurally malformed.
23 MalformedOption,
24}
25
26/// Errors returned while encoding wire-format values into caller-provided buffers.
27#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28pub enum EncodeError {
29 /// The provided buffer could not hold the encoded output.
30 BufferTooSmall,
31 /// Option numbers were encoded out of order.
32 OptionOutOfOrder,
33 /// A single option value exceeded the codec's supported length.
34 OptionValueTooLarge,
35}
36
37/// Errors returned while assembling a full packet with [`crate::PacketBuilder`].
38#[derive(Clone, Copy, Debug, PartialEq, Eq)]
39pub enum BuildError {
40 /// The destination buffer could not hold the packet.
41 BufferTooSmall,
42 /// A required source address was not supplied.
43 MissingSource,
44 /// A required destination field was not supplied.
45 MissingDestination,
46 /// A required channel identifier was not supplied.
47 MissingChannel,
48 /// A secured packet was missing its frame counter.
49 MissingFrameCounter,
50 /// A builder path that requires payload bytes was finalized without payload.
51 MissingPayload,
52 /// A MAC ACK builder was finalized without an ACK tag.
53 MissingAckTag,
54 /// Options were added in descending order.
55 OptionOutOfOrder,
56 /// Builder output failed structural validation.
57 InvalidPacket,
58}
59
60impl From<EncodeError> for BuildError {
61 fn from(value: EncodeError) -> Self {
62 match value {
63 EncodeError::BufferTooSmall => Self::BufferTooSmall,
64 EncodeError::OptionOutOfOrder => Self::OptionOutOfOrder,
65 EncodeError::OptionValueTooLarge => Self::BufferTooSmall,
66 }
67 }
68}
69
70impl fmt::Display for ParseError {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 write!(f, "{self:?}")
73 }
74}
75
76impl fmt::Display for EncodeError {
77 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78 write!(f, "{self:?}")
79 }
80}
81
82impl fmt::Display for BuildError {
83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84 write!(f, "{self:?}")
85 }
86}
87
88#[cfg(feature = "std")]
89impl std::error::Error for ParseError {}
90
91#[cfg(feature = "std")]
92impl std::error::Error for EncodeError {}
93
94#[cfg(feature = "std")]
95impl std::error::Error for BuildError {}