rust_lcm_codec/
error.rs

1//! Errors and their conversions relevant to the LCM
2//! codec and the generated code which uses it.
3
4/// Catch-all error for anything that can go wrong encoding
5/// or decoding an LCM message.
6#[derive(Debug, PartialEq)]
7pub enum CodecError<RE, WE> {
8    /// Error decoding (reading) an LCM message
9    DecodeError(DecodeError<RE>),
10    /// Error encoding (writing) an LCM message
11    EncodeError(EncodeError<WE>),
12}
13
14impl<RE, WE> From<DecodeError<RE>> for CodecError<RE, WE> {
15    #[inline]
16    fn from(e: DecodeError<RE>) -> Self {
17        CodecError::DecodeError(e)
18    }
19}
20
21impl<RE, WE> From<DecodeFingerprintError<RE>> for CodecError<RE, WE> {
22    #[inline]
23    fn from(e: DecodeFingerprintError<RE>) -> Self {
24        CodecError::DecodeError(DecodeError::DecodeFingerprintError(e))
25    }
26}
27
28impl<RE, WE> From<DecodeValueError<RE>> for CodecError<RE, WE> {
29    #[inline]
30    fn from(e: DecodeValueError<RE>) -> Self {
31        CodecError::DecodeError(DecodeError::DecodeValueError(e))
32    }
33}
34
35impl<RE, WE> From<EncodeError<WE>> for CodecError<RE, WE> {
36    #[inline]
37    fn from(e: EncodeError<WE>) -> Self {
38        CodecError::EncodeError(e)
39    }
40}
41
42impl<RE, WE> From<EncodeFingerprintError<WE>> for CodecError<RE, WE> {
43    #[inline]
44    fn from(e: EncodeFingerprintError<WE>) -> Self {
45        CodecError::EncodeError(EncodeError::EncodeFingerprintError(e))
46    }
47}
48
49impl<RE, WE> From<EncodeValueError<WE>> for CodecError<RE, WE> {
50    #[inline]
51    fn from(e: EncodeValueError<WE>) -> Self {
52        CodecError::EncodeError(EncodeError::EncodeValueError(e))
53    }
54}
55
56/// The errors that can occur when decoding an LCM message.
57#[derive(Debug, PartialEq)]
58pub enum DecodeError<E> {
59    /// Error decoding fingerprint prior to message body
60    DecodeFingerprintError(DecodeFingerprintError<E>),
61    /// Error decoding a value in the message body
62    DecodeValueError(DecodeValueError<E>),
63}
64
65impl<E> From<DecodeFingerprintError<E>> for DecodeError<E> {
66    #[inline]
67    fn from(e: DecodeFingerprintError<E>) -> Self {
68        DecodeError::DecodeFingerprintError(e)
69    }
70}
71
72impl<E> From<DecodeValueError<E>> for DecodeError<E> {
73    #[inline]
74    fn from(e: DecodeValueError<E>) -> Self {
75        DecodeError::DecodeValueError(e)
76    }
77}
78/// The errors that can occur when decoding the LCM type hash / fingerprint
79/// for a message.
80#[derive(Debug, PartialEq)]
81pub enum DecodeFingerprintError<E> {
82    /// The fingerprint value found did not match the expected value for the
83    /// message type of interest.
84    InvalidFingerprint(u64),
85    /// The underlying StreamingReader encountered an error.
86    ReaderError(E),
87}
88
89impl<E> From<E> for DecodeFingerprintError<E> {
90    #[inline]
91    fn from(e: E) -> Self {
92        DecodeFingerprintError::ReaderError(e)
93    }
94}
95
96/// The errors that can occur when decoding a value in the body
97/// of an LCM message.
98#[derive(Debug, PartialEq)]
99pub enum DecodeValueError<E> {
100    /// The user attempted to read more or fewer items
101    /// out of an array than the array contained.
102    ArrayLengthMismatch(&'static str),
103    /// The value attempted to be decoded was invalid
104    /// in some way.
105    InvalidValue(&'static str),
106    /// The underlying StreamingReader encountered an error.
107    ReaderError(E),
108}
109
110impl<E> From<E> for DecodeValueError<E> {
111    #[inline]
112    fn from(e: E) -> Self {
113        DecodeValueError::ReaderError(e)
114    }
115}
116
117/// The errors that can occur when encoding an LCM message.
118#[derive(Debug, PartialEq)]
119pub enum EncodeError<E> {
120    /// Error encoding fingerprint prior to message body
121    EncodeFingerprintError(EncodeFingerprintError<E>),
122    /// Error encoding a value in the message body
123    EncodeValueError(EncodeValueError<E>),
124}
125
126impl<E> From<EncodeFingerprintError<E>> for EncodeError<E> {
127    #[inline]
128    fn from(e: EncodeFingerprintError<E>) -> Self {
129        EncodeError::EncodeFingerprintError(e)
130    }
131}
132
133impl<E> From<EncodeValueError<E>> for EncodeError<E> {
134    #[inline]
135    fn from(e: EncodeValueError<E>) -> Self {
136        EncodeError::EncodeValueError(e)
137    }
138}
139
140/// The errors that can occur when encoding the LCM type hash / fingerprint
141/// for a message.
142#[derive(Debug, PartialEq)]
143pub enum EncodeFingerprintError<E> {
144    /// The underlying StreamingWriter encountered an error.
145    WriterError(E),
146}
147
148impl<E> From<E> for EncodeFingerprintError<E> {
149    #[inline]
150    fn from(e: E) -> Self {
151        EncodeFingerprintError::WriterError(e)
152    }
153}
154
155/// The errors that can occur when encoding a value in the body
156/// of an LCM message.
157#[derive(Debug, PartialEq, Eq)]
158pub enum EncodeValueError<E> {
159    /// The user attempted to write more or fewer items
160    /// into an array than the array contained.
161    ArrayLengthMismatch(&'static str),
162    /// The value attempted to be encoded was invalid
163    /// in some way.
164    InvalidValue(&'static str),
165    /// The underlying StreamingWriter encountered an error.
166    WriterError(E),
167}
168
169impl<E> From<E> for EncodeValueError<E> {
170    #[inline]
171    fn from(e: E) -> Self {
172        EncodeValueError::WriterError(e)
173    }
174}