parity_multihash/
errors.rs

1use std::{error, fmt};
2
3/// Error that can happen when encoding some bytes into a multihash.
4#[derive(Debug, Copy, Clone, PartialEq, Eq)]
5pub enum EncodeError {
6    /// The requested hash algorithm isn't supported by this library.
7    UnsupportedType,
8    /// The input length is too large for the hash algorithm.
9    UnsupportedInputLength,
10}
11
12impl fmt::Display for EncodeError {
13    #[inline]
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        match *self {
16            EncodeError::UnsupportedType => write!(f, "This type is not supported yet"),
17            EncodeError::UnsupportedInputLength => write!(
18                f,
19                "The length of the input for the given hash is not yet supported"
20            ),
21        }
22    }
23}
24
25impl error::Error for EncodeError {}
26
27/// Error that can happen when decoding some bytes.
28#[derive(Debug, Copy, Clone, PartialEq, Eq)]
29pub enum DecodeError {
30    /// The input doesn't have a correct length.
31    BadInputLength,
32    /// The code of the hashing algorithm is incorrect.
33    UnknownCode,
34}
35
36impl fmt::Display for DecodeError {
37    #[inline]
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match *self {
40            DecodeError::BadInputLength => write!(f, "Not matching input length"),
41            DecodeError::UnknownCode => write!(f, "Found unknown code"),
42        }
43    }
44}
45
46impl error::Error for DecodeError {}
47
48/// Error that can happen when decoding some bytes.
49///
50/// Same as `DecodeError`, but allows retreiving the data whose decoding was attempted.
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct DecodeOwnedError {
53    /// The error.
54    pub error: DecodeError,
55    /// The data whose decoding was attempted.
56    pub data: Vec<u8>,
57}
58
59impl fmt::Display for DecodeOwnedError {
60    #[inline]
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        write!(f, "{}", self.error)
63    }
64}
65
66impl error::Error for DecodeOwnedError {}