nrc_protobuf/
error.rs

1//! Protobuf error type
2
3use std::error::Error;
4use std::fmt;
5use std::io;
6use std::str;
7
8use wire_format::WireType;
9
10/// `Result` alias for `ProtobufError`
11pub type ProtobufResult<T> = Result<T, ProtobufError>;
12
13/// Enum values added here for diagnostic purposes.
14/// Users should not depend on specific values.
15#[derive(Debug)]
16pub enum WireError {
17    /// Could not read complete message because stream is EOF
18    UnexpectedEof,
19    /// Wrong wire type for given field
20    UnexpectedWireType(WireType),
21    /// Incorrect tag value
22    IncorrectTag(u32),
23    /// Malformed map field
24    IncompleteMap,
25    /// Malformed varint
26    IncorrectVarint,
27    /// String is not valid UTD-8
28    Utf8Error,
29    /// Enum value is unknown
30    InvalidEnumValue(i32),
31    /// Message is too nested
32    OverRecursionLimit,
33    /// Could not read complete message because stream is EOF
34    TruncatedMessage,
35    /// Other error
36    Other,
37}
38
39/// Generic protobuf error
40#[derive(Debug)]
41pub enum ProtobufError {
42    /// I/O error when reading or writing
43    IoError(io::Error),
44    /// Malformed input
45    WireError(WireError),
46    /// Protocol contains a string which is not valid UTF-8 string
47    Utf8(str::Utf8Error),
48    /// Not all required fields set
49    MessageNotInitialized {
50        /// Message name
51        message: &'static str,
52    },
53}
54
55impl ProtobufError {
56    /// Create message not initialized error.
57    #[doc(hidden)]
58    pub fn message_not_initialized(message: &'static str) -> ProtobufError {
59        ProtobufError::MessageNotInitialized { message: message }
60    }
61}
62
63impl fmt::Display for ProtobufError {
64    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65        fmt::Debug::fmt(self, f)
66    }
67}
68
69impl Error for ProtobufError {
70    fn description(&self) -> &str {
71        match self {
72            // not sure that cause should be included in message
73            &ProtobufError::IoError(ref e) => e.description(),
74            &ProtobufError::WireError(ref e) => match *e {
75                WireError::Utf8Error => "invalid UTF-8 sequence",
76                WireError::UnexpectedWireType(..) => "unexpected wire type",
77                WireError::InvalidEnumValue(..) => "invalid enum value",
78                WireError::IncorrectTag(..) => "incorrect tag",
79                WireError::IncorrectVarint => "incorrect varint",
80                WireError::IncompleteMap => "incomplete map",
81                WireError::UnexpectedEof => "unexpected EOF",
82                WireError::OverRecursionLimit => "over recursion limit",
83                WireError::TruncatedMessage => "truncated message",
84                WireError::Other => "other error",
85            },
86            &ProtobufError::Utf8(ref e) => &e.description(),
87            &ProtobufError::MessageNotInitialized { .. } => "not all message fields set",
88        }
89    }
90
91    fn cause(&self) -> Option<&Error> {
92        match self {
93            &ProtobufError::IoError(ref e) => Some(e),
94            &ProtobufError::Utf8(ref e) => Some(e),
95            &ProtobufError::WireError(..) => None,
96            &ProtobufError::MessageNotInitialized { .. } => None,
97        }
98    }
99}
100
101impl From<io::Error> for ProtobufError {
102    fn from(err: io::Error) -> Self {
103        ProtobufError::IoError(err)
104    }
105}
106
107impl From<str::Utf8Error> for ProtobufError {
108    fn from(err: str::Utf8Error) -> Self {
109        ProtobufError::Utf8(err)
110    }
111}
112
113impl From<ProtobufError> for io::Error {
114    fn from(err: ProtobufError) -> Self {
115        match err {
116            ProtobufError::IoError(e) => e,
117            ProtobufError::WireError(e) => {
118                io::Error::new(io::ErrorKind::InvalidData, ProtobufError::WireError(e))
119            }
120            ProtobufError::MessageNotInitialized { message: msg } => io::Error::new(
121                io::ErrorKind::InvalidInput,
122                ProtobufError::MessageNotInitialized { message: msg },
123            ),
124            e => io::Error::new(io::ErrorKind::Other, Box::new(e)),
125        }
126    }
127}