1use std::fmt;
4
5use crate::types::ProtocolVersion;
6
7#[derive(Clone, Debug, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum DecodeError {
11 UnexpectedEnd,
13 NotRtps([u8; 4]),
15 UnsupportedVersion(ProtocolVersion),
18 BadSubmessageLength {
22 offset: usize,
24 },
25 InvalidSubmessage {
28 id: u8,
30 reason: &'static str,
32 },
33 InvalidParameterList(&'static str),
35 InvalidParameterValue(crate::parameter::ParameterId),
37 MissingParameter(crate::parameter::ParameterId),
39 MustUnderstand(crate::parameter::ParameterId),
42 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#[derive(Clone, Debug, PartialEq, Eq)]
82#[non_exhaustive]
83pub enum EncodeError {
84 SubmessageTooLong {
88 index: usize,
90 },
91 EmptySubmessage {
96 index: usize,
98 },
99 MisplacedHeaderExtension {
102 index: usize,
104 },
105 ParameterTooLong,
107 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 {}