1#[derive(Debug)]
5pub enum Error {
6 #[cfg(feature = "std")]
8 Io(std::io::Error),
9 #[cfg(not(feature = "std"))]
11 Io,
12 Utf8(::core::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> = ::core::result::Result<T, Error>;
33
34#[cfg(feature = "std")]
35impl Into<std::io::Error> for Error {
36 fn into(self) -> ::std::io::Error {
37 match self {
38 Error::Io(x) => x,
39 Error::Utf8(x) => std::io::Error::new(std::io::ErrorKind::InvalidData, x),
40 x => std::io::Error::new(std::io::ErrorKind::Other, x),
41 }
42 }
43}
44
45#[cfg(feature = "std")]
46impl From<std::io::Error> for Error {
47 fn from(e: std::io::Error) -> Error {
48 Error::Io(e)
49 }
50}
51
52impl From<::core::str::Utf8Error> for Error {
53 fn from(e: ::core::str::Utf8Error) -> Error {
54 Error::Utf8(e)
55 }
56}
57
58#[cfg(feature = "std")]
59impl std::error::Error for Error {
60 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
61 match self {
62 Error::Io(e) => Some(e),
63 Error::Utf8(e) => Some(e),
64 _ => None,
65 }
66 }
67}
68
69impl core::fmt::Display for Error {
70 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
71 match self {
72 #[cfg(feature = "std")]
73 Error::Io(e) => write!(f, "{}", e),
74 #[cfg(not(feature = "std"))]
75 Error::Io => write!(f, "IO error"),
76 Error::Utf8(e) => write!(f, "{}", e),
77 Error::Deprecated(feature) => write!(f, "Feature '{}' has been deprecated", feature),
78 Error::UnknownWireType(e) => {
79 write!(f, "Unknown wire type '{}', must be less than 6", e)
80 }
81 Error::Varint => write!(f, "Cannot decode varint"),
82 #[cfg(feature = "std")]
83 Error::Message(msg) => write!(f, "Error while parsing message: {}", msg),
84 Error::Map(tag) => write!(f, "Unexpected map tag: '{}', expecting 1 or 2", tag),
85 Error::UnexpectedEndOfBuffer => write!(f, "Unexpected end of buffer"),
86 Error::OutputBufferTooSmall => write!(f, "Output buffer too small"),
87 }
88 }
89}