2018-10-01 15:58:11 +08:00
|
|
|
use std::{error, fmt};
|
2018-06-01 10:10:00 +02:00
|
|
|
|
2018-08-09 14:51:09 +02:00
|
|
|
/// Error that can happen when encoding some bytes into a multihash.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum EncodeError {
|
|
|
|
/// The requested hash algorithm isn't supported by this library.
|
2018-06-01 10:10:00 +02:00
|
|
|
UnsupportedType,
|
|
|
|
}
|
|
|
|
|
2018-08-09 14:51:09 +02:00
|
|
|
impl fmt::Display for EncodeError {
|
|
|
|
#[inline]
|
2018-06-01 10:10:00 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-08-09 14:51:09 +02:00
|
|
|
match *self {
|
|
|
|
EncodeError::UnsupportedType => write!(f, "This type is not supported yet"),
|
|
|
|
}
|
2018-06-01 10:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 15:58:11 +08:00
|
|
|
impl error::Error for EncodeError {}
|
2018-08-09 14:51:09 +02:00
|
|
|
|
|
|
|
/// Error that can happen when decoding some bytes.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum DecodeError {
|
|
|
|
/// The input doesn't have a correct length.
|
|
|
|
BadInputLength,
|
|
|
|
/// The code of the hashing algorithm is incorrect.
|
|
|
|
UnknownCode,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for DecodeError {
|
|
|
|
#[inline]
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-06-01 10:10:00 +02:00
|
|
|
match *self {
|
2018-10-01 15:58:11 +08:00
|
|
|
DecodeError::BadInputLength => write!(f, "Not matching input length"),
|
2018-08-09 14:51:09 +02:00
|
|
|
DecodeError::UnknownCode => write!(f, "Found unknown code"),
|
2018-06-01 10:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-09 14:51:09 +02:00
|
|
|
|
2018-10-01 15:58:11 +08:00
|
|
|
impl error::Error for DecodeError {}
|
2018-08-09 14:51:09 +02:00
|
|
|
|
|
|
|
/// Error that can happen when decoding some bytes.
|
|
|
|
///
|
|
|
|
/// Same as `DecodeError`, but allows retreiving the data whose decoding was attempted.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct DecodeOwnedError {
|
|
|
|
/// The error.
|
|
|
|
pub error: DecodeError,
|
|
|
|
/// The data whose decoding was attempted.
|
|
|
|
pub data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for DecodeOwnedError {
|
|
|
|
#[inline]
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 15:58:11 +08:00
|
|
|
impl error::Error for DecodeOwnedError {}
|