1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use core::{fmt, str};

/// Decoding errors.
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum Error {
    /// Decoding has (unexpectedly) reached the end of the input slice.
    EndOfInput,
    /// Data item to decode is not a valid `char`.
    InvalidChar(u32),
    /// Decoding a string failed because it is invalid UTF-8.
    Utf8(str::Utf8Error),
    /// A numeric value exceeds its value range.
    Overflow(u64, &'static str),
    /// An unexpected type was encountered.
    TypeMismatch(u8, &'static str),
    /// An unknown enum variant was encountered.
    UnknownVariant(u32),
    /// A value was missing at the specified index.
    MissingValue(u32, &'static str),
    /// Generic error message.
    Message(&'static str)
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::EndOfInput         => f.write_str("end of input bytes"),
            Error::InvalidChar(n)     => write!(f, "invalid char: {:#x?}", n),
            Error::Utf8(e)            => write!(f, "invalid utf-8: {}", e),
            Error::Overflow(n, m)     => write!(f, "{}: {} overflows target type", m, n),
            Error::TypeMismatch(t, m) => write!(f, "type mismatch: {:#x?}, {}", t, m),
            Error::UnknownVariant(n)  => write!(f, "unknown enum variant {}", n),
            Error::MissingValue(n, s) => write!(f, "missing value at index {} for {}", n, s),
            Error::Message(m)         => write!(f, "{}", m)
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Utf8(e) => Some(e),
            | Error::EndOfInput
            | Error::InvalidChar(_)
            | Error::Overflow(..)
            | Error::TypeMismatch(..)
            | Error::UnknownVariant(_)
            | Error::MissingValue(..)
            | Error::Message(_)
            => None
        }
    }
}

impl From<str::Utf8Error> for Error {
    fn from(e: str::Utf8Error) -> Self {
        Error::Utf8(e)
    }
}