minicbor_io/
error.rs

1use core::convert::Infallible;
2use std::{fmt, io};
3
4/// Possible read/write errors.
5#[derive(Debug)]
6#[non_exhaustive]
7pub enum Error {
8    /// An I/O error occured.
9    Io(io::Error),
10    /// A decoding error occured.
11    Decode(minicbor::decode::Error),
12    /// An encoding error occured.
13    Encode(minicbor::encode::Error<Infallible>),
14    /// The length preceding the CBOR value is not valid.
15    InvalidLen
16}
17
18impl fmt::Display for Error {
19    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20        match self {
21            Error::Io(e) => write!(f, "i/o error: {e}"),
22            Error::Decode(e) => write!(f, "decode error: {e}"),
23            Error::Encode(e) => write!(f, "encode error: {e}"),
24            Error::InvalidLen => f.write_str("invalid length")
25        }
26    }
27}
28
29impl core::error::Error for Error {
30    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
31        match self {
32            Error::Io(e) => Some(e),
33            Error::Decode(e) => Some(e),
34            Error::Encode(e) => Some(e),
35            Error::InvalidLen => None
36        }
37    }
38}
39
40impl From<io::Error> for Error {
41    fn from(e: io::Error) -> Self {
42        Error::Io(e)
43    }
44}
45
46impl From<minicbor::encode::Error<Infallible>> for Error {
47    fn from(e: minicbor::encode::Error<Infallible>) -> Self {
48        Error::Encode(e)
49    }
50}