cdr_encoding/
error.rs

1use std::fmt::Display;
2
3use serde::{de, ser};
4
5pub type Result<T> = std::result::Result<T, Error>;
6
7/// Several things can go wrong during CDR (de)serialization
8#[derive(Debug, thiserror::Error)]
9pub enum Error {
10  /// Wrapper for error string
11  #[error("{0}")]
12  Message(String),
13
14  /// Wrapper for `std::io::Error`
15  #[error("io::Error: {0}")]
16  Io(#[from] std::io::Error),
17
18  /// CDR is not self-describing format, cannot deserialize \'Any\' type.
19  /// This is an indication of trying something that is not possible with CDR.
20  #[error("CDR is not self-describing format, cannot deserialize \'Any\' type: {0}")]
21  NotSelfDescribingFormat(String),
22
23  /// Serialization must know sequence length before serialization.
24  /// This is an indication of trying something that is not possible with CDR.
25  #[error("CDR serialization requires sequence length to be specified at the start.")]
26  SequenceLengthUnknown,
27
28  /// Unexpected end of input
29  #[error("unexpected end of input")]
30  Eof,
31
32  /// Bad encoding of Boolean value
33  #[error("Expected 0 or 1 as Boolean, got: {0}")]
34  BadBoolean(u8),
35
36  /// Bad Unicode codepoint
37  #[error("Bad Unicode character code: {0}")]
38  BadChar(u32), // invalid Unicode codepoint
39
40  /// Bad discriminant (variant tag) in `Option`
41  #[error("Option value must have discriminant 0 or 1, read: {0}")]
42  BadOption(u32), // Option variant tag (discriminant) is not 0 or 1
43
44  /// String was not valid UTF-8
45  #[error("UTF-8 error: {0}")]
46  BadUTF8(std::str::Utf8Error),
47}
48
49impl ser::Error for Error {
50  fn custom<T: Display>(msg: T) -> Self {
51    Self::Message(msg.to_string())
52  }
53}
54
55impl de::Error for Error {
56  fn custom<T: Display>(msg: T) -> Self {
57    Self::Message(msg.to_string())
58  }
59}