1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Encode/decode error.

/// Wire encode/decode errors.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub enum WireError {
    /// The buffer to extract a type from is too short to do so.
    ReadBufferTooShort {
        /// Minimum required buffer length.
        expected: usize,
        /// Actual length given.
        got: usize,
    },
    /// The buffer to write the packed data into is too short.
    WriteBufferTooShort {
        /// Minimum required buffer length.
        expected: usize,
        /// Actual length given.
        got: usize,
    },
    /// Invalid enum or struct value.
    ///
    /// If this comes from an enum, consider adding a variant with `#[wire(catch_all)]` or
    /// `#[wire(alternatives = [])]`.
    InvalidValue,
    /// Failed to create an array of the correct length.
    ArrayLength,
    /// Valid UTF8 input data is required to decode to a string.
    InvalidUtf8,
}

#[cfg(feature = "std")]
impl std::error::Error for WireError {}

impl core::fmt::Display for WireError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            WireError::ReadBufferTooShort { expected, got } => write!(
                f,
                "Need at least {} read buffer bytes, got {}",
                expected, got
            ),
            WireError::WriteBufferTooShort { expected, got } => write!(
                f,
                "Need at least {} write buffer bytes, got {}",
                expected, got
            ),
            WireError::InvalidValue => f.write_str("Invalid decoded value"),
            WireError::ArrayLength => f.write_str("Incorrect array length"),
            WireError::InvalidUtf8 => f.write_str("Invalid UTF8"),
        }
    }
}