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}
24
25impl From<CoreParseError> for ParseError {
26    fn from(value: CoreParseError) -> Self {
27        Self::Core(value)
28    }
29}
30
31impl fmt::Display for ParseError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        write!(f, "{self:?}")
34    }
35}
36
37/// Error returned when encoding text payloads.
38#[derive(Clone, Copy, Debug, PartialEq, Eq)]
39pub enum EncodeError {
40    /// Propagated `umsh-core` encoding error.
41    Core(CoreEncodeError),
42    /// The destination buffer was too small for the encoded output.
43    BufferTooSmall,
44    /// The provided field combination is structurally invalid.
45    InvalidField,
46}
47
48impl From<CoreEncodeError> for EncodeError {
49    fn from(value: CoreEncodeError) -> Self {
50        Self::Core(value)
51    }
52}
53
54impl fmt::Display for EncodeError {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        write!(f, "{self:?}")
57    }
58}
59
60/// Error returned when sending a text payload through a transport wrapper.
61#[derive(Clone, Debug, PartialEq, Eq)]
62pub enum TextSendError<E> {
63    /// Text-payload encoding failed before the transport was called.
64    Encode(EncodeError),
65    /// The underlying transport send failed.
66    Transport(E),
67}
68
69impl<E> From<EncodeError> for TextSendError<E> {
70    fn from(value: EncodeError) -> Self {
71        Self::Encode(value)
72    }
73}
74
75impl<E> fmt::Display for TextSendError<E>
76where
77    E: fmt::Debug,
78{
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        write!(f, "{self:?}")
81    }
82}
83
84#[cfg(feature = "std")]
85impl std::error::Error for ParseError {}
86
87#[cfg(feature = "std")]
88impl std::error::Error for EncodeError {}
89
90#[cfg(feature = "std")]
91impl<E> std::error::Error for TextSendError<E> where E: fmt::Debug + fmt::Display + 'static {}