umsh_text/
error.rs

1use core::fmt;
2
3use umsh_core::{EncodeError as CoreEncodeError, PacketType, ParseError as CoreParseError};
4
5/// Error returned when parsing or validating text payloads.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum ParseError {
8    /// Propagated `umsh-core` parse error.
9    Core(CoreParseError),
10    /// A field that must be valid UTF-8 was not valid UTF-8.
11    InvalidUtf8,
12    /// The leading payload-type byte did not identify a text payload.
13    InvalidPayloadType(u8),
14    /// The payload type is known, but it is invalid for the given packet type.
15    PayloadTypeNotAllowed {
16        payload_type: u8,
17        packet_type: PacketType,
18    },
19    /// The text-message type byte is not one of the registered values.
20    InvalidMessageType(u8),
21    /// An option payload or fixed-width field had an invalid encoding.
22    InvalidOptionValue,
23    /// A recognized option carrying identity, sequencing, or reference
24    /// semantics appeared more than once.
25    DuplicateOption(u16),
26}
27
28impl From<CoreParseError> for ParseError {
29    fn from(value: CoreParseError) -> Self {
30        Self::Core(value)
31    }
32}
33
34impl fmt::Display for ParseError {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        write!(f, "{self:?}")
37    }
38}
39
40/// Error returned when encoding text payloads.
41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
42pub enum EncodeError {
43    /// Propagated `umsh-core` encoding error.
44    Core(CoreEncodeError),
45    /// The destination buffer was too small for the encoded output.
46    BufferTooSmall,
47    /// The provided field combination is structurally invalid.
48    InvalidField,
49}
50
51impl From<CoreEncodeError> for EncodeError {
52    fn from(value: CoreEncodeError) -> Self {
53        Self::Core(value)
54    }
55}
56
57impl fmt::Display for EncodeError {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        write!(f, "{self:?}")
60    }
61}
62
63/// Error returned when sending a text payload through a transport wrapper.
64#[derive(Clone, Debug, PartialEq, Eq)]
65pub enum TextSendError<E> {
66    /// Text-payload encoding failed before the transport was called.
67    Encode(EncodeError),
68    /// The underlying transport send failed.
69    Transport(E),
70}
71
72impl<E> From<EncodeError> for TextSendError<E> {
73    fn from(value: EncodeError) -> Self {
74        Self::Encode(value)
75    }
76}
77
78impl<E> fmt::Display for TextSendError<E>
79where
80    E: fmt::Debug,
81{
82    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83        write!(f, "{self:?}")
84    }
85}
86
87#[cfg(feature = "std")]
88impl std::error::Error for ParseError {}
89
90#[cfg(feature = "std")]
91impl std::error::Error for EncodeError {}
92
93#[cfg(feature = "std")]
94impl<E> std::error::Error for TextSendError<E> where E: fmt::Debug + fmt::Display + 'static {}