httlib_protos/decoder/
error.rs

1use std::error;
2use std::fmt;
3use std::io;
4use std::convert;
5
6/// Contains error options that can be encountered while performing the decoding
7/// operations.
8#[derive(Debug, PartialEq)]
9pub enum DecoderError {
10    /// Indicates that the decoder received an invalid stream of bytes that can
11    /// not be decoded.
12    InvalidInput,
13
14    /// Indicates that the decoder encountered an I/O interruption. Interrupted
15    /// operations can typically be retried.
16    Interrupted,
17
18    /// Indicates that the buffer from which an item was supposed to be decoded
19    /// does not contain enough octets to complete the decoding.
20    InputUnderflow,
21
22    /// Indicates that the decoder encountered an invalid tag number of a key. 
23    /// A tag number must be unique per message and the value can be between `1`
24    /// and `2^29 - 1`.
25    InvalidTag,
26}
27
28impl From<io::Error> for DecoderError {
29    fn from(_err: io::Error) -> Self {
30        Self::Interrupted
31    }
32}
33
34impl From<convert::Infallible> for DecoderError { // until solved: https://github.com/rust-lang/rust/issues/64715
35    fn from(_: convert::Infallible) -> Self {
36        unreachable!()
37    }
38}
39
40impl fmt::Display for DecoderError {
41    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
42        match self {
43            Self::InvalidInput => write!(fmt, "Invalid byte stream."),
44            Self::Interrupted => write!(fmt, "Read operation interrupted."),
45            Self::InputUnderflow => write!(fmt, "Not enough bytes."),
46            Self::InvalidTag => write!(fmt, "Found tag with invalid number."),
47        }
48    }
49}
50
51impl error::Error for DecoderError {}