| 1 |
use std::error::Error as StdError; |
| 2 |
use std::fmt::{self, Display, Formatter}; |
| 3 |
use std::io; |
| 4 |
|
| 5 |
use crate::block::BlockError; |
| 6 |
use crate::header::HeaderError; |
| 7 |
|
| 8 |
/// An error returned by [`Decoder`] or [`DecoderReader`] |
| 9 |
/// |
| 10 |
/// [`Decoder`]: crate::decoder::Decoder |
| 11 |
/// [`DecoderReader`]: crate::decoder::DecoderReader |
| 12 |
#[derive(Debug, Clone, PartialEq)] |
| 13 |
pub enum DecoderError { |
| 14 |
/// An error was returned by the `Header` decoder |
| 15 |
Header(HeaderError), |
| 16 |
/// An error was returned by the `Block` decoder |
| 17 |
Block(BlockError), |
| 18 |
} |
| 19 |
|
| 20 |
impl Display for DecoderError { |
| 21 |
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 22 |
match self { |
| 23 |
DecoderError::Header(err) => write!(f, "header: {}", err), |
| 24 |
DecoderError::Block(err) => write!(f, "block: {}", err), |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
impl From<HeaderError> for DecoderError { |
| 30 |
fn from(err: HeaderError) -> Self { |
| 31 |
DecoderError::Header(err) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
impl From<BlockError> for DecoderError { |
| 36 |
fn from(err: BlockError) -> Self { |
| 37 |
DecoderError::Block(err) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
impl StdError for DecoderError {} |
| 42 |
|
| 43 |
impl From<DecoderError> for io::Error { |
| 44 |
fn from(err: DecoderError) -> io::Error { |
| 45 |
match err { |
| 46 |
DecoderError::Header(err) => err.into(), |
| 47 |
DecoderError::Block(err) => err.into(), |
| 48 |
} |
| 49 |
} |
| 50 |
} |