Skip to main content

nros_serdes/
error.rs

1//! Error types for CDR serialization and deserialization.
2//!
3//! Two error enums cover the two directions: [`SerError`] for writes and
4//! [`DeserError`] for reads. Both are `Copy` and `no_std`-compatible.
5
6use core::fmt;
7
8/// Serialization error.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum SerError {
11    /// Buffer is too small to hold the serialized data.
12    BufferTooSmall,
13    /// String length exceeds `u32::MAX` (CDR uses a 4-byte length prefix).
14    StringTooLong,
15    /// Sequence length exceeds `u32::MAX` (CDR uses a 4-byte length prefix).
16    SequenceTooLong,
17}
18
19impl fmt::Display for SerError {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            SerError::BufferTooSmall => write!(f, "buffer too small"),
23            SerError::StringTooLong => write!(f, "string too long"),
24            SerError::SequenceTooLong => write!(f, "sequence too long"),
25        }
26    }
27}
28
29/// Deserialization error.
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub enum DeserError {
32    /// Unexpected end of buffer (tried to read past available data).
33    UnexpectedEof,
34    /// Invalid data encountered (e.g., boolean value other than 0 or 1).
35    InvalidData,
36    /// Invalid UTF-8 in a CDR string field.
37    InvalidUtf8,
38    /// Decoded sequence/string length exceeds the `heapless` container capacity.
39    CapacityExceeded,
40    /// The 4-byte CDR encapsulation header is missing or invalid.
41    InvalidHeader,
42}
43
44impl fmt::Display for DeserError {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            DeserError::UnexpectedEof => write!(f, "unexpected end of buffer"),
48            DeserError::InvalidData => write!(f, "invalid data"),
49            DeserError::InvalidUtf8 => write!(f, "invalid UTF-8"),
50            DeserError::CapacityExceeded => write!(f, "capacity exceeded"),
51            DeserError::InvalidHeader => write!(f, "invalid encapsulation header"),
52        }
53    }
54}