1use std::fmt;
2
3pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12 UnexpectedEof {
14 needed: usize,
16 remaining: usize,
18 },
19 InvalidBoolean(u8),
21 InvalidByteOrder(u8),
23 StringNotTerminated,
25 InvalidUtf8,
27 InvalidUtf16,
29 InvalidWcharLength(u8),
31 OddWstringLength(u32),
33 WcharNotSupported,
35 LengthTooLarge {
37 len: u32,
39 remaining: usize,
41 },
42 LengthOverflow(usize),
44 EmptyEncapsulation,
46 InvalidEnumValue(u32),
48 InvalidTypeCode(&'static str),
53 InvalidValue(&'static str),
57}
58
59impl fmt::Display for Error {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 match self {
62 Error::UnexpectedEof { needed, remaining } => write!(
63 f,
64 "unexpected end of CDR stream: needed {needed} bytes, {remaining} left"
65 ),
66 Error::InvalidBoolean(v) => write!(f, "invalid boolean octet {v:#04x}"),
67 Error::InvalidByteOrder(v) => write!(f, "invalid byte order flag {v:#04x}"),
68 Error::StringNotTerminated => f.write_str("string is not NUL-terminated"),
69 Error::InvalidUtf8 => f.write_str("string is not valid UTF-8"),
70 Error::InvalidUtf16 => f.write_str("wide string is not valid UTF-16"),
71 Error::InvalidWcharLength(n) => write!(f, "invalid wchar octet length {n}"),
72 Error::OddWstringLength(n) => write!(f, "odd wstring octet length {n}"),
73 Error::WcharNotSupported => f.write_str("wchar is not supported by GIOP 1.0"),
74 Error::LengthTooLarge { len, remaining } => write!(
75 f,
76 "length {len} is larger than the {remaining} bytes left in the stream"
77 ),
78 Error::LengthOverflow(len) => {
79 write!(f, "length {len} does not fit in an unsigned long")
80 }
81 Error::EmptyEncapsulation => f.write_str("encapsulation has no byte order octet"),
82 Error::InvalidEnumValue(v) => write!(f, "invalid enum value {v}"),
83 Error::InvalidTypeCode(why) => write!(f, "invalid TypeCode: {why}"),
84 Error::InvalidValue(why) => write!(f, "invalid valuetype: {why}"),
85 }
86 }
87}
88
89impl std::error::Error for Error {}