Skip to main content

rustorb_cdr/
error.rs

1use std::fmt;
2
3/// Result type of CDR operations.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Errors raised while encoding or decoding CDR.
7///
8/// A decoding error corresponds to `CORBA::MARSHAL` at the ORB level.
9#[derive(Debug, Clone, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12    /// The stream ended before a value was complete.
13    UnexpectedEof {
14        /// Bytes the value needed.
15        needed: usize,
16        /// Bytes left in the stream.
17        remaining: usize,
18    },
19    /// A boolean octet was neither `0` nor `1`.
20    InvalidBoolean(u8),
21    /// A byte order flag was neither `0` nor `1`.
22    InvalidByteOrder(u8),
23    /// A `string` did not end with a NUL octet.
24    StringNotTerminated,
25    /// A `string` was not valid UTF-8.
26    InvalidUtf8,
27    /// A `wchar` or `wstring` was not valid UTF-16.
28    InvalidUtf16,
29    /// A GIOP 1.2 `wchar` had an octet length other than 2 or 4 (with BOM).
30    InvalidWcharLength(u8),
31    /// A GIOP 1.2 `wstring` had an odd octet length.
32    OddWstringLength(u32),
33    /// `wchar` and `wstring` cannot be used with this encoding (GIOP 1.0).
34    WcharNotSupported,
35    /// A sequence or string length is larger than the bytes left.
36    LengthTooLarge {
37        /// The declared length.
38        len: u32,
39        /// Bytes left in the stream.
40        remaining: usize,
41    },
42    /// A length does not fit in an unsigned long.
43    LengthOverflow(usize),
44    /// An encapsulation had no byte order octet.
45    EmptyEncapsulation,
46    /// An enum or union discriminator had an unknown value.
47    InvalidEnumValue(u32),
48    /// A TypeCode was malformed: an unknown kind, an indirection that
49    /// points nowhere, invalid parameters, or nesting too deep. On the
50    /// writing side, a TypeCode that cannot be written (an unresolved
51    /// recursive placeholder).
52    InvalidTypeCode(&'static str),
53    /// A valuetype was malformed: a bad value tag, chunk or end tag, or an
54    /// indirection that points nowhere. On the writing side, an
55    /// `end_value` without its `begin_value`.
56    InvalidValue(&'static str),
57    /// An XCDR header was malformed: an unknown encapsulation identifier,
58    /// a DHEADER, EMHEADER or parameter length past the end of the data
59    /// that holds it, or a length too large for its header. On the
60    /// writing side, a member ID too large for its header.
61    InvalidXcdr(&'static str),
62}
63
64impl fmt::Display for Error {
65    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66        match self {
67            Error::UnexpectedEof { needed, remaining } => write!(
68                f,
69                "unexpected end of CDR stream: needed {needed} bytes, {remaining} left"
70            ),
71            Error::InvalidBoolean(v) => write!(f, "invalid boolean octet {v:#04x}"),
72            Error::InvalidByteOrder(v) => write!(f, "invalid byte order flag {v:#04x}"),
73            Error::StringNotTerminated => f.write_str("string is not NUL-terminated"),
74            Error::InvalidUtf8 => f.write_str("string is not valid UTF-8"),
75            Error::InvalidUtf16 => f.write_str("wide string is not valid UTF-16"),
76            Error::InvalidWcharLength(n) => write!(f, "invalid wchar octet length {n}"),
77            Error::OddWstringLength(n) => write!(f, "odd wstring octet length {n}"),
78            Error::WcharNotSupported => f.write_str("wchar is not supported by this encoding"),
79            Error::LengthTooLarge { len, remaining } => write!(
80                f,
81                "length {len} is larger than the {remaining} bytes left in the stream"
82            ),
83            Error::LengthOverflow(len) => {
84                write!(f, "length {len} does not fit in an unsigned long")
85            }
86            Error::EmptyEncapsulation => f.write_str("encapsulation has no byte order octet"),
87            Error::InvalidEnumValue(v) => write!(f, "invalid enum value {v}"),
88            Error::InvalidTypeCode(why) => write!(f, "invalid TypeCode: {why}"),
89            Error::InvalidValue(why) => write!(f, "invalid valuetype: {why}"),
90            Error::InvalidXcdr(why) => write!(f, "invalid XCDR: {why}"),
91        }
92    }
93}
94
95impl std::error::Error for Error {}