1#[derive(Debug)]
5pub enum Error {
6 #[cfg(feature = "std")]
8 Io(std::io::Error),
9 #[cfg(not(feature = "std"))]
11 Io,
12 Utf8(::std::str::Utf8Error),
14 Deprecated(&'static str),
16 UnknownWireType(u8),
18 Varint,
20 #[cfg(feature = "std")]
22 Message(String),
23 Map(u8),
25 UnexpectedEndOfBuffer,
27 OutputBufferTooSmall,
29}
30
31pub type Result<T> = ::std::result::Result<T, Error>;
33
34#[cfg(feature = "std")]
35impl From<Error> for std::io::Error {
36 fn from(val: Error) -> Self {
37 match val {
38 Error::Io(x) => x,
39 Error::Utf8(x) => std::io::Error::new(
40 std::io::ErrorKind::InvalidData,
41 x,
42 ),
43 x => std::io::Error::new(std::io::ErrorKind::Other, x),
44 }
45 }
46}
47
48#[cfg(feature = "std")]
49impl From<std::io::Error> for Error {
50 fn from(e: std::io::Error) -> Error {
51 Error::Io(e)
52 }
53}
54
55impl From<::std::str::Utf8Error> for Error {
56 fn from(e: ::std::str::Utf8Error) -> Error {
57 Error::Utf8(e)
58 }
59}
60
61#[cfg(feature = "std")]
62impl std::error::Error for Error {
63 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
64 match self {
65 Error::Io(e) => Some(e),
66 Error::Utf8(e) => Some(e),
67 _ => None,
68 }
69 }
70}
71
72impl core::fmt::Display for Error {
73 fn fmt(
74 &self,
75 f: &mut core::fmt::Formatter,
76 ) -> core::fmt::Result {
77 match self {
78 #[cfg(feature = "std")]
79 Error::Io(e) => write!(f, "{}", e),
80 #[cfg(not(feature = "std"))]
81 Error::Io => write!(f, "IO error"),
82 Error::Utf8(e) => write!(f, "{}", e),
83 Error::Deprecated(feature) => write!(
84 f,
85 "Feature '{}' has been deprecated",
86 feature
87 ),
88 Error::UnknownWireType(e) => {
89 write!(
90 f,
91 "Unknown wire type '{}', must be less than 6",
92 e
93 )
94 }
95 Error::Varint => write!(f, "Cannot decode varint"),
96 #[cfg(feature = "std")]
97 Error::Message(msg) => write!(
98 f,
99 "Error while parsing message: {}",
100 msg
101 ),
102 Error::Map(tag) => write!(
103 f,
104 "Unexpected map tag: '{}', expecting 1 or 2",
105 tag
106 ),
107 Error::UnexpectedEndOfBuffer => write!(f, "Unexpected end of buffer"),
108 Error::OutputBufferTooSmall => write!(f, "Output buffer too small"),
109 }
110 }
111}