#[cfg(feature="bitcoin_support")]
use bitcoin::network::serialize;
use std::convert;
use std::error::Error;
use std::fmt;
use std::io;
use std::sync;
pub enum HammersbaldError {
InvalidOffset,
Corrupted(String),
ForwardReference,
IO(io::Error),
#[cfg(feature="bitcoin_support")]
BitcoinSerialize(serialize::Error),
Poisoned(String),
Queue(String)
}
impl Error for HammersbaldError {
fn description(&self) -> &str {
match *self {
HammersbaldError::InvalidOffset => "invalid pref",
HammersbaldError::ForwardReference => "forward reference",
HammersbaldError::Corrupted (ref s) => s.as_str(),
HammersbaldError::IO(_) => "IO Error",
#[cfg(feature="bitcoin_support")]
HammersbaldError::BitcoinSerialize(_) => "Bitcoin Serialize Error",
HammersbaldError::Poisoned(ref s) => s.as_str(),
HammersbaldError::Queue(ref s) => s.as_str()
}
}
fn cause(&self) -> Option<&Error> {
match *self {
HammersbaldError::InvalidOffset => None,
HammersbaldError::ForwardReference => None,
HammersbaldError::Corrupted (_) => None,
HammersbaldError::IO(ref e) => Some(e),
#[cfg(feature="bitcoin_support")]
HammersbaldError::BitcoinSerialize(ref e) => Some(e),
HammersbaldError::Poisoned(_) => None,
HammersbaldError::Queue(_) => None
}
}
}
impl fmt::Display for HammersbaldError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Hammersbald error: {} cause: {:?}", self.description(), self.cause())
}
}
impl fmt::Debug for HammersbaldError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(self as &fmt::Display).fmt(f)
}
}
impl convert::From<io::Error> for HammersbaldError {
fn from(err: io::Error) -> HammersbaldError {
HammersbaldError::IO(err)
}
}
impl convert::From<HammersbaldError> for io::Error {
fn from(_: HammersbaldError) -> io::Error {
io::Error::from(io::ErrorKind::UnexpectedEof)
}
}
impl<T> convert::From<sync::PoisonError<T>> for HammersbaldError {
fn from(err: sync::PoisonError<T>) -> HammersbaldError {
HammersbaldError::Poisoned(err.to_string())
}
}
impl<T> convert::From<sync::mpsc::SendError<T>> for HammersbaldError {
fn from(err: sync::mpsc::SendError<T>) -> HammersbaldError {
HammersbaldError::Queue(err.to_string())
}
}