2016-02-03 14:47:14 +00:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt::Error as FmtError;
|
|
|
|
use std::clone::Clone;
|
|
|
|
use std::fmt::{Debug, Display, Formatter};
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
pub enum LinkErrorKind {
|
2016-02-03 18:59:01 +00:00
|
|
|
EntryHeaderReadError,
|
|
|
|
EntryHeaderWriteError,
|
|
|
|
ExistingLinkTypeWrong,
|
2016-02-21 13:48:21 +00:00
|
|
|
LinkTargetDoesNotExist,
|
|
|
|
InternalConversionError,
|
2016-02-23 10:52:31 +00:00
|
|
|
InvalidUri,
|
2016-02-03 14:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn link_error_type_as_str(e: &LinkErrorKind) -> &'static str {
|
|
|
|
match e {
|
2016-02-03 18:59:01 +00:00
|
|
|
&LinkErrorKind::EntryHeaderReadError
|
|
|
|
=> "Error while reading an entry header",
|
|
|
|
|
|
|
|
&LinkErrorKind::EntryHeaderWriteError
|
|
|
|
=> "Error while writing an entry header",
|
|
|
|
|
|
|
|
&LinkErrorKind::ExistingLinkTypeWrong
|
|
|
|
=> "Existing link entry has wrong type",
|
2016-02-21 13:48:21 +00:00
|
|
|
|
|
|
|
&LinkErrorKind::LinkTargetDoesNotExist
|
|
|
|
=> "Link target does not exist in the store",
|
|
|
|
|
|
|
|
&LinkErrorKind::InternalConversionError
|
|
|
|
=> "Error while converting values internally",
|
2016-02-23 10:52:31 +00:00
|
|
|
|
|
|
|
&LinkErrorKind::InvalidUri
|
|
|
|
=> "URI is not valid",
|
2016-02-03 14:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for LinkErrorKind {
|
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
|
|
|
try!(write!(fmt, "{}", link_error_type_as_str(self)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct LinkError {
|
|
|
|
kind: LinkErrorKind,
|
|
|
|
cause: Option<Box<Error>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LinkError {
|
|
|
|
|
|
|
|
pub fn new(errtype: LinkErrorKind, cause: Option<Box<Error>>) -> LinkError {
|
|
|
|
LinkError {
|
|
|
|
kind: errtype,
|
|
|
|
cause: cause,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for LinkError {
|
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
|
|
|
try!(write!(fmt, "[{}]", link_error_type_as_str(&self.kind)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for LinkError {
|
|
|
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
link_error_type_as_str(&self.kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&Error> {
|
|
|
|
self.cause.as_ref().map(|e| &**e)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|