use std::error;
use std::fmt;
use std::io;
use super::Result;
#[derive(Debug)]
pub enum Error {
IoError(std::io::Error),
ParseError(&'static str),
Unsupported(&'static str),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Error::IoError(ref err) => err.fmt(f),
&Error::ParseError(ref msg) => write!(f, "Malformed stream encountered: {}", msg),
&Error::Unsupported(ref codec) => {
write!(f, "Unsupported codec encountered: {}", codec)
}
}
}
}
impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::IoError(ref err) => Some(err),
Error::ParseError(_) => None,
Error::Unsupported(_) => None,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::IoError(err)
}
}
pub fn parse_error<T>(desc: &'static str) -> Result<T> {
Err(Error::ParseError(desc))
}
pub fn unsupported_error<T>(codec: &'static str) -> Result<T> {
Err(Error::Unsupported(codec))
}