Skip to main content

rustorb_rtps/
error.rs

1//! Errors of the message codec.
2
3use std::fmt;
4
5use crate::types::ProtocolVersion;
6
7/// Why a message, a submessage or a parameter list could not be read.
8#[derive(Clone, Debug, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum DecodeError {
11    /// The input ended before a field could be read.
12    UnexpectedEnd,
13    /// The message does not start with `RTPS` (DDSI-RTPS 2.5, 8.3.6.3).
14    NotRtps([u8; 4]),
15    /// The major protocol version is newer than this implementation
16    /// (DDSI-RTPS 2.5, 8.3.6.3).
17    UnsupportedVersion(ProtocolVersion),
18    /// A submessage header could not be read in full, or its
19    /// `octetsToNextHeader` goes past the end of the message
20    /// (DDSI-RTPS 2.5, 8.3.4.1 rules 1 and 2).
21    BadSubmessageLength {
22        /// Offset of the submessage header from the start of the message.
23        offset: usize,
24    },
25    /// A known submessage is invalid (DDSI-RTPS 2.5, 8.3.4.1 rule 6 and
26    /// the "Validity" part of each submessage in 8.3.8).
27    InvalidSubmessage {
28        /// The submessage id.
29        id: u8,
30        /// Which rule was broken.
31        reason: &'static str,
32    },
33    /// A parameter list is malformed (DDSI-RTPS 2.5, 9.4.2.11).
34    InvalidParameterList(&'static str),
35    /// A parameter value is too short or not valid for its type.
36    InvalidParameterValue(crate::parameter::ParameterId),
37    /// A required parameter is missing.
38    MissingParameter(crate::parameter::ParameterId),
39    /// A parameter that is not understood has the must-understand bit set
40    /// (DDSI-RTPS 2.5, Table 9.6).
41    MustUnderstand(crate::parameter::ParameterId),
42    /// A serialized payload uses a representation that is not supported
43    /// here (DDSI-RTPS 2.5, clause 10).
44    UnknownRepresentation([u8; 2]),
45}
46
47impl fmt::Display for DecodeError {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        match self {
50            DecodeError::UnexpectedEnd => f.write_str("unexpected end of input"),
51            DecodeError::NotRtps(p) => write!(f, "not an RTPS message (protocol {p:02x?})"),
52            DecodeError::UnsupportedVersion(v) => {
53                write!(f, "unsupported RTPS version {}.{}", v.major, v.minor)
54            }
55            DecodeError::BadSubmessageLength { offset } => {
56                write!(f, "bad submessage length at offset {offset}")
57            }
58            DecodeError::InvalidSubmessage { id, reason } => {
59                write!(f, "invalid submessage 0x{id:02x}: {reason}")
60            }
61            DecodeError::InvalidParameterList(reason) => {
62                write!(f, "invalid parameter list: {reason}")
63            }
64            DecodeError::InvalidParameterValue(id) => {
65                write!(f, "invalid value for parameter 0x{:04x}", id.0)
66            }
67            DecodeError::MissingParameter(id) => write!(f, "missing parameter 0x{:04x}", id.0),
68            DecodeError::MustUnderstand(id) => {
69                write!(f, "parameter 0x{:04x} must be understood but is not", id.0)
70            }
71            DecodeError::UnknownRepresentation(r) => {
72                write!(f, "unknown payload representation {r:02x?}")
73            }
74        }
75    }
76}
77
78impl std::error::Error for DecodeError {}
79
80/// Why a message could not be written.
81#[derive(Clone, Debug, PartialEq, Eq)]
82#[non_exhaustive]
83pub enum EncodeError {
84    /// A submessage that is not the last one in the message is longer
85    /// than 65535 octets, so `octetsToNextHeader` cannot hold its length
86    /// (DDSI-RTPS 2.5, 9.4.5.1.3).
87    SubmessageTooLong {
88        /// Index of the submessage in the message.
89        index: usize,
90    },
91    /// A submessage other than PAD or INFO_TS has an empty body and is not
92    /// the last one in the message. Its `octetsToNextHeader` would be 0,
93    /// which means "up to the end of the message" (DDSI-RTPS 2.5,
94    /// 9.4.5.1.3).
95    EmptySubmessage {
96        /// Index of the submessage in the message.
97        index: usize,
98    },
99    /// A HeaderExtension is not the first submessage
100    /// (DDSI-RTPS 2.5, 8.3.7.3).
101    MisplacedHeaderExtension {
102        /// Index of the submessage in the message.
103        index: usize,
104    },
105    /// A parameter value is longer than a parameter length can say.
106    ParameterTooLong,
107    /// A locator list has more entries than a 32-bit count can say.
108    TooManyLocators,
109}
110
111impl fmt::Display for EncodeError {
112    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113        match self {
114            EncodeError::SubmessageTooLong { index } => {
115                write!(f, "submessage {index} is too long and is not the last one")
116            }
117            EncodeError::EmptySubmessage { index } => {
118                write!(f, "submessage {index} is empty and is not the last one")
119            }
120            EncodeError::MisplacedHeaderExtension { index } => {
121                write!(
122                    f,
123                    "HeaderExtension at index {index}; it must be the first submessage"
124                )
125            }
126            EncodeError::ParameterTooLong => f.write_str("parameter value is too long"),
127            EncodeError::TooManyLocators => f.write_str("too many locators"),
128        }
129    }
130}
131
132impl std::error::Error for EncodeError {}