tiny_multihash/
error.rs

1#[cfg(feature = "std")]
2use std::io::Error as IoError;
3use unsigned_varint::decode::Error as DecodeError;
4#[cfg(feature = "std")]
5use unsigned_varint::io::ReadError;
6
7/// Multihash error.
8#[derive(Debug)]
9pub enum Error {
10    /// Io error.
11    #[cfg(feature = "std")]
12    Io(IoError),
13    /// Unsupported multihash code.
14    UnsupportedCode(u64),
15    /// Invalid multihash size.
16    InvalidSize(u64),
17    /// Invalid varint.
18    Varint(DecodeError),
19}
20
21impl core::fmt::Display for Error {
22    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
23        match self {
24            #[cfg(feature = "std")]
25            Self::Io(err) => write!(f, "{}", err),
26            Self::UnsupportedCode(code) => write!(f, "Unsupported multihash code {}.", code),
27            Self::InvalidSize(size) => write!(f, "Invalid multihash size {}.", size),
28            Self::Varint(err) => write!(f, "{}", err),
29        }
30    }
31}
32
33#[cfg(feature = "std")]
34impl std::error::Error for Error {}
35
36#[cfg(feature = "std")]
37impl From<IoError> for Error {
38    fn from(err: IoError) -> Self {
39        Self::Io(err)
40    }
41}
42
43#[cfg(feature = "std")]
44impl From<ReadError> for Error {
45    fn from(err: ReadError) -> Self {
46        match err {
47            ReadError::Io(err) => Self::Io(err),
48            ReadError::Decode(err) => Self::Varint(err),
49            _ => unreachable!(),
50        }
51    }
52}
53
54/// Multihash result.
55pub type Result<T> = core::result::Result<T, Error>;