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