ethercrab_wire/
error.rs

1//! Encode/decode error.
2
3/// Wire encode/decode errors.
4#[derive(Debug, Copy, Clone, PartialEq, Eq)]
5#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
6pub enum WireError {
7    /// The buffer to extract a type from is too short to do so.
8    ReadBufferTooShort,
9    /// The buffer to write the packed data into is too short.
10    WriteBufferTooShort,
11    /// Invalid enum or struct value.
12    ///
13    /// If this comes from an enum, consider adding a variant with `#[wire(catch_all)]` or
14    /// `#[wire(alternatives = [])]`.
15    InvalidValue,
16    /// Failed to create an array of the correct length.
17    ArrayLength,
18    /// Valid UTF8 input data is required to decode to a string.
19    InvalidUtf8,
20}
21
22#[cfg(feature = "std")]
23impl std::error::Error for WireError {}
24
25impl core::fmt::Display for WireError {
26    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
27        match self {
28            WireError::ReadBufferTooShort => {
29                write!(f, "Read buffer too short to extract type from")
30            }
31            WireError::WriteBufferTooShort => {
32                write!(f, "Write buffer too short to pack type into")
33            }
34            WireError::InvalidValue => f.write_str("Invalid decoded value"),
35            WireError::ArrayLength => f.write_str("Incorrect array length"),
36            WireError::InvalidUtf8 => f.write_str("Invalid UTF8"),
37        }
38    }
39}